By default, Magento adjusts the attribute position when we create product attributes. But, systematically, the same position will get apply for all categories.
So if we wanted to display attributes position differently for categories wise, then this article is your solution.
First, you have to create text type Category Attributes
which you need to set differently category wise (Please verify product attribute code and category attribute code).
In the text type attributes, you have to set the require position value as shown in below screenshot.
After creating all the category attributes and setting value in all categories, you have to move forword in the next step:
Override the Magento\Catalog\Model\Layer\Category\FilterableAttributeList file.
For that, you need to use virtualType in di.xml in your module (You can create a new module or use any existing module. Create only di.xml in that module).
1 2 3 4 5 |
<virtualType name="categoryFilterList" type="[Vendor Name]\[Module Name]\Model\Layer\FilterList"> <arguments> <argument name="filterableAttributes" xsi:type="object">Magento\Catalog\Model\Layer\Category\FilterableAttributeList</argument> </arguments> </virtualType> |
After override FilterList, put below code in that file for change the Filter Position:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php namespace [Vendor Name]\[Module Name]\Model\Layer; /** * Override FilterList Class */ class FilterList extends \Magento\Catalog\Model\Layer\FilterList { protected $_categoryFactory; public function __construct(\Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry) { $this->_registry = $registry; $this->_categoryFactory = $categoryFactory; parent::__construct($context); } public function getFilters(\Magento\Catalog\Model\Layer $layer) { $arr = array(); $new = array(); $value = array(); if (!count($this->filters)) { $this->filters = [$this->objectManager->create($this->filterTypes[self::CATEGORY_FILTER], ['layer' => $layer]), ]; $i = 1; $arraycheck = array(); $inarraycheck = array(); $valuesCol = array(); $allready = array(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $currentcategory = $this->_registry->registry('current_category'); $categoryId = $currentcategory->getId(); $categoryData = $this->_categoryFactory->create()->load($categoryId); $getData = $categoryData->getData(); foreach ($getData as $key => $value) { $new[] = $key; } foreach ($this->filterableAttributes->getList() as $attribute) { $code = $attribute->getAttributeCode(); if (in_array($code, $new)) { if (!empty($getData[$code])) { $valuesCol[] = $posVal = $getData[$code]; } } } foreach ($this->filterableAttributes->getList() as $attribute) { $code = $attribute->getAttributeCode(); if (in_array($code, $new)) { if (!empty($getData[$code])) { $posVal = $getData[$code]; array_push($inarraycheck, $attribute->getAttributeCode()); if (array_key_exists($posVal, $this->filters)) { $arraycheck[] = $this->createAttributeFilter($attribute, $layer); $this->filters[$posVal] = $this->createAttributeFilter($attribute, $layer); } else { $this->filters[$posVal] = $this->createAttributeFilter($attribute, $layer); } } else { $this->filters[$i] = $this->createAttributeFilter($attribute, $layer); } } else { if (in_array($i, $valuesCol)) { $allready[] = $this->createAttributeFilter($attribute, $layer); } else { $this->filters[$i] = $this->createAttributeFilter($attribute, $layer); } } $i++; if (!empty($arraycheck)) { foreach ($arraycheck as $key => $value) { if (!in_array($attribute->getAttributeCode(), $inarraycheck)) { $this->filters[$i] = $value; } $i++; $arraycheck = array(); } } if (!empty($allready)) { foreach ($allready as $key1 => $value1) { $this->filters[$i] = $value1; } $i++; $allready = array(); } } ksort($this->filters); } return $this->filters; } } ?> |
After doing all the steps run below commands and check output on front end.
You will see layered navigation filters are displayed differently in each category.
1 2 |
php bin/magento setup:di:compile php bin/magento cache:flush |
[crayon-63e0a4a1edf09797409144/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.