0

Here is my code -

Code of di.xml file -

<?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">
        <plugin name="LayerPlugin" type="Vishal\ConfigPrice\Plugin\Model\Layer" sortOrder="1"/>
    </type>
</config>

Code of Model Plugin -

<?php

namespace Vishal\ConfigPrice\Plugin\Model;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;

class Layer
{
    /**
     * Product collections array
     *
     * @var array
     */
    protected $_productCollections = [];

    /**
     * Key which can be used for load/save aggregation data
     *
     * @var string
     */
    protected $_stateKey = null;

    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $registry = null;

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * Catalog product
     *
     * @var \Magento\Catalog\Model\ResourceModel\Product
     */
    protected $_catalogProduct;

    /**
     * Attribute collection factory
     *
     * @var AttributeCollectionFactory
     */
    protected $_attributeCollectionFactory;

    /**
     * Layer state factory
     *
     * @var \Magento\Catalog\Model\Layer\StateFactory
     */
    protected $_layerStateFactory;

    /**
     * @var \Magento\Catalog\Model\Layer\ItemCollectionProviderInterface
     */
    protected $collectionProvider;

    /**
     * @var \Magento\Catalog\Model\Layer\Category\StateKey
     */
    protected $stateKeyGenerator;

    /**
     * @var \Magento\Catalog\Model\Layer\Category\CollectionFilter
     */
    protected $collectionFilter;

    /**
     * @var CategoryRepositoryInterface
     */
    protected $categoryRepository;

    /**
     * @param Layer\ContextInterface $context
     * @param Layer\StateFactory $layerStateFactory
     * @param AttributeCollectionFactory $attributeCollectionFactory
     * @param \Magento\Catalog\Model\ResourceModel\Product $catalogProduct
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Registry $registry
     * @param CategoryRepositoryInterface $categoryRepository
     * @param array $data
     */
    public function __construct(
        \Magento\Catalog\Model\Layer\StateFactory $layerStateFactory,
        AttributeCollectionFactory $attributeCollectionFactory,
        \Magento\Catalog\Model\ResourceModel\Product $catalogProduct,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Registry $registry,
        CategoryRepositoryInterface $categoryRepository,
        array $data = []
    ) {
        $this->_layerStateFactory = $layerStateFactory;
        $this->_attributeCollectionFactory = $attributeCollectionFactory;
        $this->_catalogProduct = $catalogProduct;
        $this->_storeManager = $storeManager;
        $this->registry = $registry;
        $this->categoryRepository = $categoryRepository; 

    }

    public function afterGetProductCollection(\Magento\Catalog\Model\Layer $subject, $result)
    {
        $result->setOrder('rand()');
        // echo "==-->>>==<pre>"; print_r($result->getData()); die;
        return $result;
    }
}

But no luck !!! Please help me....

0

Replace the below code with your model plugin ,

<?php
    
    namespace Vishal\ConfigPrice\Plugin\Model;
    
    use Magento\Catalog\Api\CategoryRepositoryInterface;
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
    
    class Layer
    {
        /**
         * Product collections array
         *
         * @var array
         */
        protected $_productCollections = [];
    
        /**
         * Key which can be used for load/save aggregation data
         *
         * @var string
         */
        protected $_stateKey = null;
    
        /**
         * Core registry
         *
         * @var \Magento\Framework\Registry
         */
        protected $registry = null;
    
        /**
         * Store manager
         *
         * @var \Magento\Store\Model\StoreManagerInterface
         */
        protected $_storeManager;
    
        /**
         * Catalog product
         *
         * @var \Magento\Catalog\Model\ResourceModel\Product
         */
        protected $_catalogProduct;
    
        /**
         * Attribute collection factory
         *
         * @var AttributeCollectionFactory
         */
        protected $_attributeCollectionFactory;
    
        /**
         * Layer state factory
         *
         * @var \Magento\Catalog\Model\Layer\StateFactory
         */
        protected $_layerStateFactory;
    
        /**
         * @var \Magento\Catalog\Model\Layer\ItemCollectionProviderInterface
         */
        protected $collectionProvider;
    
        /**
         * @var \Magento\Catalog\Model\Layer\Category\StateKey
         */
        protected $stateKeyGenerator;
    
        /**
         * @var \Magento\Catalog\Model\Layer\Category\CollectionFilter
         */
        protected $collectionFilter;
    
        /**
         * @var CategoryRepositoryInterface
         */
        protected $categoryRepository;
    
        /**
         * @param Layer\ContextInterface $context
         * @param Layer\StateFactory $layerStateFactory
         * @param AttributeCollectionFactory $attributeCollectionFactory
         * @param \Magento\Catalog\Model\ResourceModel\Product $catalogProduct
         * @param \Magento\Store\Model\StoreManagerInterface $storeManager
         * @param \Magento\Framework\Registry $registry
         * @param CategoryRepositoryInterface $categoryRepository
         * @param array $data
         */
        public function __construct(
            \Magento\Catalog\Model\Layer\StateFactory $layerStateFactory,
            AttributeCollectionFactory $attributeCollectionFactory,
            \Magento\Catalog\Model\ResourceModel\Product $catalogProduct,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\Registry $registry,
            CategoryRepositoryInterface $categoryRepository,
            array $data = []
        ) {
            $this->_layerStateFactory = $layerStateFactory;
            $this->_attributeCollectionFactory = $attributeCollectionFactory;
            $this->_catalogProduct = $catalogProduct;
            $this->_storeManager = $storeManager;
            $this->registry = $registry;
            $this->categoryRepository = $categoryRepository; 
    
        }
    
        public function afterGetProductCollection(\Magento\Catalog\Model\Layer $subject, $result)
        {
            $result->getSelect()->orderRand();
            return $result;
        }
    }

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.