Pergunta

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?

Foi útil?

Solução

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;
      }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top