0

I have created a module that removes default Category product listings sorting orders and adds new custom ones. How do I go about setting one of the custom sortiong options as a Default one? Right now the "default" one has to be the first one in dropdown :(

Plugin/Catalog/Block/Toolbar.php

<?php
namespace Hapex\CustomSortOrder\Plugin\Catalog\Block;

use Hapex\CustomSortOrder\Helper\Data as DataHelper;

class Toolbar
{
    public function __construct(DataHelper $helperData)
    {
        $this->helperData = $helperData;
    }

    /**
     * @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 $toolbar, \Closure $proceed, $collection)
    {
        $this->_collection = $collection;
        $result = $proceed($collection);
        switch ($this->helperData->isEnabled()) {
            case true:
                $currentOrder = $toolbar->getCurrentOrder();
                $currentDirection = $toolbar->getCurrentDirection();

                if ($currentOrder) {
                    switch ($currentOrder) {

                        case 'qty_asc':
                            $this->_collection->setOrder('quantity_and_stock_status', "asc");
                        break;

                        case 'qty_desc':
                            $this->_collection->setOrder('quantity_and_stock_status', "desc");
                        break;

                        case 'price_asc':
                            $this->_collection->setOrder('price', "asc");
                        break;
                        case 'price_desc':
                            $this->_collection->setOrder('price', "desc");
                        break;

                        case 'name_asc':
                            $this->_collection->setOrder('name', "asc");
                        break;
                        case 'name_desc':
                            $this->_collection->setOrder('name', "desc");
                        break;

                        default:
                            $this->_collection->setOrder($currentOrder, $currentDirection);
                        break;

                    }
                }
                break;
        }
        //var_dump((string) $this->_collection->getSelect()); You can use this to get a list of all the available sort fields
        return $result;
    }
}

Plugin/Catalog/Model/Config.php

<?php
namespace Hapex\CustomSortOrder\Plugin\Catalog\Model;

use Hapex\CustomSortOrder\Helper\Data as DataHelper;

class Config
{
    public function __construct(DataHelper $helperData)
    {
        $this->helperData = $helperData;
    }

    /**
     * Adding custom options and changing labels
     *
     * @param \Magento\Catalog\Model\Config $catalogConfig
     * @param [] $options
     * @return []
     */
    public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
    {
        switch ($this->helperData->isEnabled()) {
            case true:
                //Remove default sorting options
                unset($options['position']);
                unset($options['name']);
                unset($options['price']);
                unset($options['quantity_and_stock_status']);

                //Change label of default sorting options if needed
                //$options['position'] = __('Relevance');

                //New sorting options
                $customOptions = [];
                $customOptions['price_desc'] = __('Price: High to Low');
                $customOptions['price_asc'] = __('Price: Low to High');
                $customOptions['qty_desc'] = __('Quantity: High to Low');
                $customOptions['qty_asc'] = __('Quantity: Low to High');
                $customOptions['name_asc'] = __('Product Name: A to Z');
                $customOptions['name_desc'] = __('Product Name: Z to A');

                $options = array_merge($options, $customOptions);
                break;
        }
        return $options;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.