0

I have created an attribute "bestsellers" integer, and I have assigned values to that attribute in many products, only numbers.

I have enable the attribute to sort by in the front end, but, when I try to sort by bestsellers, they are not correctly ordered by bestsellers attribute, some products with value = 0 are showing between the first.

Should I do something else?, somethink like reindex?, or is cache issue.?

I've print the sql and test it on my mysql and the order is correct, but in the front end doesn't.

  • Please add your code here whatever you tried. – Supravat M Jul 12 '17 at 15:38
  • I have no code yet. I just have created the attribute 'Bestsellers' in the admin panel, and enable it to 'sort by'. then I have assigned values to some products. all in the admin panel – jacr1614 Jul 12 '17 at 15:59
0

In default magento sort by will be work as ascending order, so the zero(0) value products are displayed first.

Please follow the below steps, Copy the file from path: app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php to app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php

Replace the function,

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

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

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }
        return $this;
    }

to

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

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

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }

        Mage::app()->getFrontController()->getRequest()->getControllerName();

        if ($this->getCurrentOrder()) 
        {           
            if(($this->getCurrentOrder())=='your attribute code') 
            {
                if($this->getCurrentDirection() == 'asc') { $dir ='desc';}
                if($this->getCurrentDirection() == 'desc') { $dir ='asc';}
                $this->_collection->setOrder('your attribute code',$dir);
            }
            else {
                $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
            }   
        }

        return $this;
    }

Hope this will help you!

Your Answer

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

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