0

I'm using Magento 2, and on the category pages, on the left, where are listed all the subcategories, if I click on any of them, I get a link ending in: ?cat=id

If I try to follow the same path from the main menu, I get: /subcategoryname.html

Would it be possible to have it like that also on the layered navigation? I've been loking into magento files, and found this: /vendor/magento/module-layered-navigation/view/frontend/templates/layer/filter.phtml

with this part of code: <a href="<?= $block->escapeUrl($filterItem->getUrl()) ?>">

working out that link, it would be fine editing this, and how? or it would work better another way?

0

If you tried using category url instead of ?cat=catId then layered navigation not working properly. If you don't care layered nav, then try following way:

VendorName/ModuleName/etc/frontend/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Layer\Filter\Item">
        <plugin name="VendorName_ModuleName::layered_filter_item" type="VendorName\ModuleName\Plugin\Catalog\Model\Layer\Filter\Item" sortOrder="1"/>
    </type>
</config>

VendorName/ModuleName/Plugin/Catalog/Model/Layer/Filter/Item.php


namespace VendorName\ModuleName\Plugin\Catalog\Model\Layer\Filter;


class Item
{
    /**
     * @var \Magento\Catalog\Api\Data\CategoryInterfaceFactory
     */
    private $categoryFactory;

    /**
     * Item constructor.
     * @param \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory
     */
    public function __construct(
        \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory
    ) {
        $this->categoryFactory = $categoryFactory;
    }

    /**
     * Get filter item url
     *
     * @return string
     */
    public function aroundGetUrl(
        \Magento\Catalog\Model\Layer\Filter\Item $subject,
        \Closure $proceed
    ) {
        if($subject->getFilter()->getRequestVar() == 'cat') {
            $catId = $subject->getValue();
            $category = $this->categoryFactory->create()->load($catId);
            return $category->getUrl();
        }

        return $proceed();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.