0

I want to snuffle the products on list page but unable to do that. Here is the code.

app/code/Am/Suffle/etc/frontend/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="Magento\Catalog\Model\Config">
        <plugin disabled="false" name="Am_Suffle_Plugin_Magento_Catalog_Model_Config" sortOrder="10" type="Am\Suffle\Plugin\Magento\Catalog\Model\Config"/>
    </type>
    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin disabled="false" name="Am_Suffle_Magictoolbar_Plugin_Magento_Catalog_Block_Product_ProductList_Toolbar" sortOrder="10" type="Am\Suffle\Plugin\Magento\Catalog\Block\Product\ProductList\Toolbar"/>
    </type>
</config>

app/code/Am/Suffle/Plugin/Magento/Catalog/Model/Config.php

<?php 

namespace Am\Suffle\Plugin\Magento\Catalog\Model;

use Magento\Store\Model\StoreManagerInterface;

class Config
{

    /**
     * Adding custom options and changing labels
     *
     * @param \Magento\Catalog\Model\Config $catalogConfig
     * @param [] $options
     * @return []
     */

    public function afterGetAttributeUsedForSortByArray(
        \Magento\Catalog\Model\Config $subject,
        $options
    ) {

        //New sorting options
        $customOption['ai_magic'] = __(' Magic');

        //Merge default sorting options with custom options
        $options = array_merge($customOption, $options);

        return $options;
    }
}

app/code/Am/Suffle/Plugin/Magento/Catalog/Block/Product/ProductList/Toolbar.php

<?php
namespace Am\Suffle\Plugin\Magento\Catalog\Block\Product\ProductList;

class Toolbar
{

    /**
     * Plugin
     *
     * @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
     * @param \Closure $proceed
     * @param \Magento\Framework\Data\Collection $collection
     * @return \Magento\Catalog\Block\Product\ProductList\Toolbar
     */
    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        $currentOrder = $subject->getCurrentOrder();
        $result = $proceed($collection);
        $entity_ids =  array();
        foreach($collection as $product){
            $entity_ids[] = $product->getId();
        }

        shuffle($entity_ids);
        if ($currentOrder) {
            if ($currentOrder == 'ai_magic') {
                // $subject->getCollection()->clear();
                $collection->clear();
                // $collection->addAttributeToFilter('entity_id', $entity_ids)
;
                $subject->getCollection()->getSelect()->order(new \Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $entity_ids).')'));
            } 
        }

        return $subject;
    }

}

Thanks in advance.

1 Answer 1

1

Try following code:

app/code/Am/Suffle/Plugin/Magento/Catalog/Block/Product/ProductList/Toolbar.php

<?php
namespace Am\Suffle\Plugin\Magento\Catalog\Block\Product\ProductList;

class Toolbar
{
    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        $currentOrder = $subject->getCurrentOrder();
        $result = $proceed($collection);

        if ($currentOrder) {
            if ($currentOrder == 'ai_magic') {
                $subject->getCollection()
                    ->getSelect()
                    ->reset(\Magento\Framework\DB\Select::ORDER)
                    ->orderRand();
            }
        }

        return $subject;
    }   
}

Note: To implement random products functionality on the category page refresh, you need to disable FPC cache.

  • php bin/magento cache:disable full_page

Please note that, this is temporary solution, will update answer.

1
  • Thanks for your answer. Can you please let me know why we can not use that order filter ? I have checked with M1.9, it worked fine. Dec 30, 2017 at 17:47

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.