Magento 1 : Sort Product Listing By New?
-
23-01-2021 - |
Pregunta
Working on magento 1 website.
I want to sort the product sorting by new in all product categories. So that when any new product is added that shows on first.
Can i be please guided how can same be done.
Solución
Rewrite Toolbar block in your custom module
app/code/local/Vendor/Module/etc/config.xml
...
...
<global>
<blocks>
<catalog>
<rewrite>
<product_list_toolbar>Dinesh_Expiredproducts_Block_Catalog_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
</blocks>
</global>
...
...
Now create your custom block which rewrites the Toolbar block.
app/code/local/Vendor/Module/Block/Catalog/Product/List/Toolbar.php
<?php
class Vendor_Module_Block_Catalog_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
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()) {
if(($this->getCurrentOrder())=='new'){
$this->_collection->setOrder('entity_id','desc');
}
else {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}
return $this;
}
public function setDefaultOrder($field)
{
if (isset($this->_availableOrder[$field])) {
$new = ['new' => 'New'];
$this->_availableOrder = $new + $this->_availableOrder;
//$this->_orderField = $field;
$this->_orderField = 'new';
}
return $this;
}
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange