skip to Main Content

I have a category with some subcategories:

- Lamps
-- Hanging lamps
-- Wall lamps
-- Floor lamps

When clicking on one of the three subcategories in the Layered Navigation, the product listing is filtered to the products of that specific category. I don’t want it to filter, but I want the subcategories in the Layered Navigation to actually link to the category.

Is this a Magento 2 setting, or does this require custom changes? If so, can anybody help me get started? I’ve done some searching, but was only able to find a similar question for Magento 1.

2

Answers


  1. You need to use a custom plugin class. Where you can change the core behavior of getUrl method in MagentoCatalogModelLayerFilterItem class. Because this getUrl method is responsible for generating all filter URLs.

    app/code/Vendor/Module/etc/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="MagentoCatalogModelLayerFilterItem">
            <plugin disabled="false" name="Vendor_Module_Plugin_Magento_Catalog_Model_Layer_Filter_Item" sortOrder="10" type="VendorModulePluginMagentoCatalogModelLayerFilterItem"/>
        </type>
    </config>
    

    app/code/Vendor/Module/Plugin/Magento/Catalog/Model/Layer/Filter/Item.php

    <?php
    namespace VendorModulePluginMagentoCatalogModelLayerFilter;
    
    class Item
    {
        protected $categoryHelper;
        protected $categoryRepository;
    
        public function __construct(
            MagentoCatalogHelperCategory $categoryHelper,
            MagentoCatalogModelCategoryRepository $categoryRepository
        ) {
            $this->categoryHelper = $categoryHelper;
            $this->categoryRepository = $categoryRepository;
        }
    
        public function afterGetUrl(
            MagentoCatalogModelLayerFilterItem $subject,
            $result
        ) {
            // custom url for category filter
            if ($subject->getFilter()->getRequestVar() == 'cat') {
                $categoryId = $subject->getValue();
                $categoryObj = $this->categoryRepository->get($categoryId);
                return $this->categoryHelper->getCategoryUrl($categoryObj);
            }
            
            return $result;
        }
    }
    

    Here after plugin method (afterGetUrl) is used. Which runs after the main method (getUrl).

    If you want to know more about plugin class. You can check here.

    Login or Signup to reply.
  2. I am working on magento 2.3.3 – 2.3.4 and this solution does not work for me,
    but it was very helpful. I need to change the code little bit.

    here is my fix:

        <?php namespace VendorModulePluginMagentoCatalogModelLayerFilter;
    
           class Item
           {
               protected $categoryHelper;
               protected $categoryRepository;
    
               public function __construct(
                   MagentoCatalogHelperCategory $categoryHelper,
                   MagentoCatalogModelCategoryRepository $categoryRepository
               ) {
                   $this->categoryHelper = $categoryHelper;
                   $this->categoryRepository = $categoryRepository;
               }
    
               public function afterGetUrl(
                   MagentoCatalogModelLayerFilterItem $subject, $result
               ) {
                   // custom url for category filter
                   if (strtolower($subject->getFilter()->getRequestVar()) === 'cat') {
                       $categoryId = $subject->getValue();
                       $categoryObj = $this->categoryRepository->get($categoryId);
                       return $this->categoryHelper->getCategoryUrl($categoryObj);
                   }
    
                   return $result;
               }
           }
    

    I hope it will be helpful as it was for me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search