2

I have tried to implement a custom sorting plugin in Magento. Namely, with the options:

Price (Low > High)

Price (High > Low)

Name (A - Z)

Name (Z - A)

Now, I have the plugin installed and working, with code very similar to the examples in similar posts such as Magento 2 How to add custom sort by option.

Sorting by price works brilliantly, however, I'm struggling to get the correct join, in order to get the sorting by Name working. Take the code below in my plugins Toolbar.php

<?php
namespace Vendor\ModuleName\Plugin\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 $toolbar,
        \Closure $proceed,
        $collection
    ) {
        $this->_collection = $collection;
        $currentOrder = $toolbar->getCurrentOrder();
        $currentDirection = $toolbar->getCurrentDirection();
        $result = $proceed($collection);

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

            case 'price_desc':
                $this->_collection
                    ->getSelect()
                    ->order('price_index.min_price DESC');
            break;

            case 'price_asc':
                $this->_collection
                    ->getSelect()
                    ->order('price_index.min_price ASC');
            break;

            case 'name_az':
                $this->_collection
                    ->getSelect()
                    ->order('e.name ASC');
            break;

            case 'name_za':
                $this->_collection
                    ->getSelect()
                    ->order('e.name DESC');
            break;

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

            }
        }

        return $result;
    }
}

As mentioned, sorting by price works fine, but when trying to sort by name, I get an error something along the lines of,

there is no column name on e

I understand that I need to perform a more complex join query to get the product name from the product_entity_varchar table but can't for the life of me seem to get it working or fully understand the syntax.

Some help would be much appreciated!

1 Answer 1

0

Use the setOrder method for sorting and pass the attribute name for a loaded attribute (e.g. name). This should work for you:

case 'name_az':
    $this->_collection
       ->setOrder('name', 'ASC');
break;

case 'name_za':
    $this->_collection
        ->setOrder('name', 'DESC');
break;
2
  • This worked perfectly thank you @HelgeB 🙂
    – Rick West
    Apr 8, 2019 at 21:45
  • Great to hear that it works for you. It would be good if you would accept the answer, that may help others with a similar problem to find the solution.
    – HelgeB
    Apr 9, 2019 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.