Magento 2.2 - проблема сортировки пользовательских продуктов


У меня есть переопределение toolbar.php для пользовательского атрибута сортировки.

<?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;
        }
    }

Выберите атрибут created_at в интерфейсе, он отображает хороший результат.

Но после выбора атрибута stock он отображается ниже ошибки.

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

Я заметил, что это из-за запроса соединения. В magento 2.1.x это было хорошо. Проблема возникает в Magento 2.2.

Другой момент:

В Magento 2.2 страница со списком продуктов по умолчанию перезагружается дважды. Я пробовал это с помощью echo и он отображается 2 раза. Так что я надеюсь, что это может быть проблемой.

Пожалуйста, помогите!

Author: Jigar Dhaduk, 2017-11-17

1 answers

В вашем состоянии просто удалите псевдоним csi и попробуйте, это для меня работа.

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());
}
 4
Author: Bhupesh, 2018-02-08 05:51:32