in Web Development

Add sort by stock availability

How to add sort by stock availability to product toolbar

Edit

/app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php 

Add

public function sortByStock($dir){ 
$table = $this->getTable('cataloginventory/stock_item'); $entity_code_id = Mage::getModel('review/review')->getEntityIdByCode(Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE); $cond = $this->getConnection()->quoteInto('t2.product_id = e.entity_id'); $this->getSelect()->joinLeft(array('t2'=>$table), $cond,array('qty'))->order("qty $dir"); }

Edit

/app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php 

Add

if($this->getCurrentOrder() == 'stock'){ $this->_collection->sortByStock($this->getCurrentDirection()); } else

Edit

/app/code/core/Mage/Catalog/Model/Config.php
public function getAttributeUsedForSortByArray() { $options = array( 'position' => Mage::helper('catalog')->__('Position'), 'stock' => Mage::helper('catalog')->__('Availability') ); foreach ($this->getAttributesUsedForSortBy() as $attribute) { /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */ $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel(); } return $options; }

Write a Comment

Comment