Magento Custom Sort Option

How to add custom collation in Magento. I want to add Best Sellers, Top and Exclusive in addition to sorting by price. Please, help

+4
source share
3 answers

For Best Sellers

haneged to code/local/Mage/Catalog/Block/Product/List/Toolbar.php method setCollection to

 public function setCollection($collection) { parent::setCollection($collection); if ($this->getCurrentOrder()) { if($this->getCurrentOrder() == 'saleability') { $this->getCollection()->getSelect() ->joinLeft('sales_flat_order_item AS sfoi', 'e.entity_id = sfoi.product_id', 'SUM(sfoi.qty_ordered) AS ordered_qty') ->group('e.entity_id')->order('ordered_qty' . $this->getCurrentDirectionReverse()); } else { $this->getCollection() ->setOrder($this->getCurrentOrder(), $this->getCurrentDirection()); } } return $this; } 

After setCollection, I added this method:

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

And finally, I changed mehod setDefaultOrder to

 public function setDefaultOrder($field) { if (isset($this->_availableOrder[$field])) { $this->_availableOrder = array( 'name' => $this->__('Name'), 'price' => $this->__('Price'), 'position' => $this->__('Position'), 'saleability' => $this->__('Saleability'), ); $this->_orderField = $field; } return $this; } 

for the best

http://www.fontis.com.au/blog/magento/sort-products-rating

try the code above.

to add a date

Magento - Sort by date added

I do not contact any of the above links for any work or care, it is just for the purposes of knowledge and to solve your problem.

Hope this helps you.

+8
source

Thanks for your reply, Anuj, it was the best work module that I could find so far.

Just add an extra bit to your code to prevent paging caused by 'group by'

Copy '/lib/varien/data/collection/Db.php' to "local / varien / data / collection / Db.php".

Change getSize function to

 public function getSize() { if (is_null($this->_totalRecords)) { $sql = $this->getSelectCountSql(); //$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams); //============================>change behave of fetchOne to fetchAll //got array of all COUNT(DISTINCT e.entity_id), then sum $result = $this->getConnection()->fetchAll($sql, $this->_bindParams); foreach ($result as $row) {//echo'<pre>'; print_r($row); $this->_totalRecords += reset($row); } } return intval($this->_totalRecords); } 

Hope this can help anyone.


update It is also necessary to update the filter section, otherwise just show 1 element on the entire filter. and the price filter will not be accurate.

What do you need to do to change core / mage / catalog / model / layer / filter / attribute.php and price.php

attribute.php getCount () at the bottom

  $countArr = array(); //print_r($connection->fetchall($select)); foreach ($connection->fetchall($select) as $single) { if (isset($countArr[$single['value']])) { $countArr[$single['value']] += $single['count']; } else { $countArr[$single['value']] = $single['count']; } } //print_r($countArr);//exit; return $countArr; //return $connection->fetchPairs($select); 

Price.php getMaxPrice

 $maxPrice = 0; foreach ($connection->fetchall($select) as $value) { if (reset($value) > $maxPrice) { $maxPrice = reset($value); } } return $maxPrice; 

If you have the same problem and are looking for a question, you will understand what I had in mind. Good luck, spent 8 hours on this best selling feature.


Refresh again,

just found a different implementation method using cron to collect the best sales data stored daily in a table that includes product_id and the calculated base sales figure. it’s just a left join, without using 'group by', this means that the main functions do not need to change and speed up the entire sorting process. Finally finished! hehe

+2
source

To sort the pagination problem for custom sorting, rewrite the resource model of this collection from

  app \ code \ core \ Mage \ Catalog \ Model \ Resource \ Product \ Collection.php 
And change below method from kernel
  protected function _getSelectCountSql ($ select = null, $ resetLeftJoins = true)
      {
         $ this -> _ renderFilters ();
         $ countSelect = (is_null ($ select))?
         $ this -> _ getClearSelect ():
         $ this -> _ buildClearSelect ($ select);

  /*Added to reset count filters for Group*/ if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) { $countSelect->reset(Zend_Db_Select::GROUP); } /*Added to reset count filters for Group*/ $countSelect->columns('COUNT(DISTINCT e.entity_id)'); if ($resetLeftJoins) { $countSelect->resetJoinLeft(); } return $countSelect; } 

The problem of counting for custom sorting will be solved above.

0
source

Source: https://habr.com/ru/post/1502437/


All Articles