0

I have created a custom bestsellers sorting option, the query working fine but setOrder() and order() is not working, i want to sorting the values using qty_ordered

<?php

namespace Vendor\Module\Block\Product\ProductList;

class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
    public function setCollection($collection)
    {  
         $this->_collection = $collection;
       
        if($this->getCurrentOrder() == "bestseller")
        {
           $this->_collection->getSelect()->joinLeft(
                'sales_order_item',
                'e.entity_id = sales_order_item.product_id',
                array('qty_ordered' => 'SUM(sales_order_item.qty_ordered)'))
                ->group('e.entity_id')
                 ->order('qty_ordered ' . $this->getCurrentDirectionReverse());
              
        }

           


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

        // $limit = (int)$this->getLimit();
        // if($limit)
        // {
        //     $this->_collection->setPageSize($limit);
        // }
        // if($this->getCurrentOrder())
        // {
        //     $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        // }
        
        return $this->_collection;
    }

    public function getCurrentDirectionReverse()
    {
        if($this->getCurrentDirection() == 'asc')
        {
            return 'desc';
        }
        elseif($this->getCurrentDirection() == 'desc')
        {
            return 'asc';
        }
        else
        {
            return $this->getCurrentDirection();
        }
    }

}

order() asc or desc is not working

2
  • Try removing order from the select, and then: $this->_collection->setOrder('qty_ordered', $this->getCurrentDirectionReverse()); Commented Aug 23, 2021 at 10:56
  • $this->_collection->setOrder('qty_ordered', $this->getCurrentDirectionReverse()); not working
    – Ali
    Commented Aug 24, 2021 at 4:58

0

Your Answer

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

Browse other questions tagged or ask your own question.