2

I want to override the Magento\Catalog\Model\Layer\Category\FilterableAttributeList model file. I have created the module. This is my di.xml file.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Layer\Category\FilterableAttributeList" type="Pos\AdvancedLayeredNavigation\Model\Layer\Category\FilterableAttributeList" />
</config>

This is my model file,

<?php
namespace Pos\AdvancedLayeredNavigation\Model\Layer\Category;
class FilterableAttributeList extends Magento\Catalog\Model\Layer\Category\FilterableAttributeList
{
 public function getList()
    {
     /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */
        $collection = $this->collectionFactory->create();
        $collection->setItemObjectClass(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
            ->addStoreLabel($this->storeManager->getStore()->getId())
            ->setOrder('position', 'DESC');
        $collection = $this->_prepareAttributeCollection($collection);
        $collection->load();

        return $collection;
    }
}

Can you guys help me!!!! Is there any way to overwrite this file?

3
  • we can override model using plugin also
    – Vishnunath
    Jun 13, 2018 at 10:34
  • You need to use plugin or observer. You can't override this module. Jun 13, 2018 at 10:35
  • @RohanHapani, Can you just guide me how to use plugin for this model? This function (getList()) use for the attribute sorting on the category page.In the magento default sort order is 'ASC' and i want only change order to DESC. Jun 13, 2018 at 10:41

1 Answer 1

3

etc/di.xml

<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\Category\FilterableAttributeList">
        <plugin name="infinity_product_gallery" type="Namespace\Module\Plugin\FilterableAttributeList" />
        </type>
    </config>

Namespace\Module\Plugin\FilterableAttributeList.php

<?php
    namespace Namespace\Module\Plugin;

    class FilterableAttributeList 
    {

        protected $collectionFactory;


        protected $storeManager;

        /**
         * FilterableAttributeList constructor
         *
         * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory
         * @param \Magento\Store\Model\StoreManagerInterface $storeManager
         */
        public function __construct(
            \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory,
            \Magento\Store\Model\StoreManagerInterface $storeManager
        ) {
            $this->collectionFactory = $collectionFactory;
            $this->storeManager = $storeManager;
        }
     public function afterGetList(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList $subject, $collectionOld)
        {
         /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */
            $collection = $this->collectionFactory->create();
            $collection->setItemObjectClass(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
                ->addStoreLabel($this->storeManager->getStore()->getId())
                ->setOrder('position', 'DESC');
            $collection = $this->_prepareAttributeCollection($collection);
            $collection->load();

            return $collection;
        }

      protected function _prepareAttributeCollection($collection)
      {
         $collection->addIsFilterableFilter();
         return $collection;
      }
    }
10
  • Where i create the plugin file? can you please give me the file name and path? Jun 13, 2018 at 11:36
  • 1
    I am update answer . But this write in namespace + name class Jun 13, 2018 at 11:38
  • you can create plugin file in a new Folder in module root named Plugin and file name will be FilterableAttributeList.php
    – Vivek
    Jun 13, 2018 at 11:39
  • Is this code is correct if i create this file in the plugin folder? Jun 13, 2018 at 11:42
  • Yes, just then fix the namespace Jun 13, 2018 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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