2

I have override toolbar.php for custom sorting attribute.

<?php
    namespace Vendor\Module\Block\Product\ProductList;
    class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
    {
        protected $_template = 'Magento_Catalog::product/list/toolbar.phtml'; 

        public function setCollection($collection)
        {
            $this->_collection = $collection;

            $this->_collection->setCurPage($this->getCurrentPage());

            $limit = (int) $this->getLimit();
            if ($limit) {
                $this->_collection->setPageSize($limit);
            }
            if ($this->getCurrentOrder()) {
                if ($this->getCurrentOrder() == 'stock') {
                    $this->_collection->getSelect()
                        ->joinLeft(
                                'cataloginventory_stock_item AS csi',
                                'e.entity_id = csi.product_id',
                                'csi.qty AS quantity'
                        )
                    ->group('e.entity_id')
                    ->order('quantity ' .$this->getCurrentDirectionReverse());
                } elseif ($this->getCurrentOrder() == 'created_at') {
                    $this->_collection
                       ->setOrder(
                            $this->getCurrentOrder(),
                            $this->getCurrentDirectionReverse()
                    );
               } else {
                    $this->_collection
                         ->setOrder(
                               $this->getCurrentOrder(),
                               $this->getCurrentDirection()
                         );
                }
            }
            return $this->_collection;
        }
    }

Select created_at attribute in frontend, it display good result.

But after select stock attribute, it display below error.

[2017-11-17 04:28:36] main.CRITICAL: You cannot define a correlation name 'csi' more than once [] []

I noticed that it is because of join query. In magento 2.1.x, it was good. Issue is occuring in Magento 2.2.

Other point:

In Magento 2.2, product listing page is reloads twice by default. I have tried it by echo and it display 2 times. So I hope it may be issue.

Please help!

1
  • Hello, is there anyone to help? Commented Nov 24, 2017 at 9:45

1 Answer 1

4

In your stock condition just remove alias name csi and try, It's work for me.

if ($this->getCurrentOrder() == 'stock') {
    $this->_collection->getSelect()
        ->joinLeft(
                'cataloginventory_stock_item',
                'e.entity_id = cataloginventory_stock_item .product_id',
                'cataloginventory_stock_item .qty AS quantity'
        )
    ->group('e.entity_id')
    ->order('quantity ' .$this->getCurrentDirectionReverse());
}
1

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.