0

I want to sort my products by attributes : that is working fine but I want to apply two sort together , like sort by name asc and sort by attributes asc also

How can I implement this ?.

Based on answer added below code in toolbar.php (where hour is input value attributes, i want to show all products with hour value as 320), but it seems not working

namespace VendoreName\ModuleName\Plugin\Product\ProductList;

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

        $subject->getCollection()->addFieldToFilter('hour','320'); 
        return $result;
    }
}
5
  • hour is product Attribute ?? – Msquare Apr 4 '20 at 17:02
  • Please print this whole collection and see Your attribute is there or not ?? You need to filert attribute like other way. – Msquare Apr 4 '20 at 17:03
  • print_R( $collection); ->> give memory error – user1799722 Apr 4 '20 at 17:20
  • Use print_r($collection->getData()); to print collection – Msquare Apr 5 '20 at 3:08
  • For attribute filter try this link magento.stackexchange.com/questions/226897/… – Msquare Apr 6 '20 at 5:05
0

I add two option in sort by drop down for product price (high to low) and (low to hight).

enter image description here

app/code/VendoreName/ModuleName/etc

di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

    <!-- for add option in sort by drop down -->
    <type name="Magento\Catalog\Model\Config">
        <plugin name="addOptions" type="VendoreName\ModuleName\Plugin\Model\Config" />
    </type>

   <!-- for do process for sort by option vise  -->
    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="addPriceFilter" type="VendoreName\ModuleName\Plugin\Product\ProductList\Toolbar" />
    </type>

</config>

app/code/VendoreName/ModuleName/Plugin/Model

Config.php

<?php

namespace VendoreName\ModuleName\Plugin\Model;

use Magento\Store\Model\StoreManagerInterface;

class Config
{
    protected $_storeManager;

    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->_storeManager = $storeManager;   
    }

    public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
    {
        $store = $this->_storeManager->getStore();
        $currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();

        //Remove specific default sorting options
        //unset($options['position']);
        //unset($options['name']);
        unset($options['price']); // here i remove price option

        //Changing label
        //$customOption['position'] = __('Relevance');

        //New sorting options
        $customOption['price_desc'] = __($currencySymbol.' (High -> Low)');
        $customOption['price_asc'] = __($currencySymbol.' (Low -> High)');

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

        return $options;
    }
}

app/code/VendoreName/ModuleName/Plugin/Product/ProductList

Toolbar.php

<?php

namespace VendoreName\ModuleName\Plugin\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 == 'price_desc') {
                $subject->getCollection()->setOrder('price', 'desc');
            } elseif ($currentOrder == 'price_asc') {
                $subject->getCollection()->setOrder('price', 'asc');
            }
        }
        return $result;
    }
}

here we can apply our custom filter also like custom attribute value or some other product value. by using addFieldToFilter().

I Hope This Helps You.

5
  • Please check and update me. – Msquare Mar 30 '20 at 15:10
  • tried below code but it seems not working class Toolbar { public function aroundSetCollection( \Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed, $collection ) { $currentOrder = $subject->getCurrentOrder(); $result = $proceed($collection); $subject->getCollection()->addFieldToFilter('hour','320'); return $result; } } – user1799722 Apr 2 '20 at 20:43
  • hour is text attribute – user1799722 Apr 2 '20 at 20:43
  • you must follow above Toolbar.php in which you need to check your parameter if condition. – Msquare Apr 3 '20 at 6:20
  • please add your file in question for more details – Msquare Apr 3 '20 at 6:21

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.