Khi ta vào 1 danh mục nào đó, ta sẽ thấy trên toolbar có mục sort by, trên đó gồm Best value, Name, Price. Giờ ta muốn thêm hoặc sửa mục Best value thành Newest products thì ta làm như sau: Bạn mở file ListSort theo tên class Mage_Adminhtml_Model_System_Config_Source_Catalog_ListSort:
publicfunction toOptionArray(){ $options = array(); $options[]= array(//benz001'label'=>Mage::helper('catalog')->__('Newest'),'value'=>'entity_id');//end benz001 $options[]= array('label'=>Mage::helper('catalog')->__('Best Value'),'value'=>'position');foreach($this->_getCatalogConfig()->getAttributesUsedForSortBy()as $attribute){ $options[]= array('label'=>Mage::helper('catalog')->__($attribute['frontend_label']),'value'=> $attribute['attribute_code']);} return $options;}
Sau đó bạn mở file theo tên class Mage_Catalog_Model_Category_Attribute_Source_Sortby:
publicfunction getAllOptions(){if(is_null($this->_options)){ $this->_options = array( array(//benz001'label'=>Mage::helper('catalog')->__('Newest'),'value'=>'entity_id'),//end benz001 array('label'=>Mage::helper('catalog')->__('Best Value'),'value'=>'position'));foreach($this->_getCatalogConfig()->getAttributesUsedForSortBy()as $attribute){ $this->_options[]= array('label'=>Mage::helper('catalog')->__($attribute['frontend_label']),'value'=> $attribute['attribute_code']);}} return $this->_options;}
Và file theo tên class Mage_Catalog_Model_Config:
publicfunction getAttributeUsedForSortByArray(){ $options = array('entity_id'=>Mage::helper('catalog')->__('Newest'),//benz001 'position'=>Mage::helper('catalog')->__('Position'),);foreach($this->getAttributesUsedForSortBy()as $attribute){/* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */ $options[$attribute->getAttributeCode()]= $attribute->getStoreLabel();} return $options;}
Phần này là để sort by theo entity_id/Newest products. Để chọn mặc định là sort theo newest product thì bạn vào Admin Panel, vào system/configuration/catalog/frontend ở mục Product Listing Sort by bạn chọn Newest products. sau đó save lại, và fresh lại cache.
Để lúc hiển thị thì sẽ hiển thị sản phầm mới nhất ra trước, ta vào file theo tên class Mage_Catalog_Block_Product_List_Toolbar
ở phần khai báo protected $_direction = ‘asc’; bạn sửa lại thành protected $_direction = ‘desc’;
Tiếp theo bạn mở file toolbar.php trong thư mục app/design/frontend/default/your theme/template/catalog/product/list/toolbar.php và tìm đoạn code:
<select onchange=”setLocation(this.value)”> < ?php foreach($this->getAvailableOrders() as $_key=>$_order): ?> <option value=”< ?php echo $this->getOrderUrl($_key, ‘asc’) ?>”< ?php if($this->isOrderCurrent($_key)): ?> selected=”selected”< ?php endif; ?>> < ?php echo $this->__($_order) ?> </option> < ?php endforeach; ?> </select>
và sửa lại thành
<select onchange=”setLocation(this.value)”> <option value=”< ?php echo $this->getOrderUrl(‘name’, ‘asc’) ?>”< ?php if($this->isOrderCurrent(‘name’) && $this->getCurrentDirection() == ‘asc’): ?> selected=”selected”< ?php endif; ?>>Name A-Z</option> <option value=”< ?php echo $this->getOrderUrl(‘name’, ‘desc’) ?>”< ?php if($this->isOrderCurrent(‘name’) && $this->getCurrentDirection() == ‘desc’): ?> selected=”selected”< ?php endif; ?>>Name Z-A</option> <option value=”< ?php echo $this->getOrderUrl(‘price’, ‘asc’) ?>”< ?php if($this->isOrderCurrent(‘price’) && $this->getCurrentDirection() == ‘asc’): ?> selected=”selected”< ?php endif; ?>>Price – Low to High</option> <option value=”< ?php echo $this->getOrderUrl(‘price’, ‘desc’) ?>”< ?php if($this->isOrderCurrent(‘price’) && $this->getCurrentDirection() == ‘desc’): ?> selected=”selected”< ?php endif; ?>>Price – High to Low</option> <option value=”< ?php echo $this->getOrderUrl(‘entity_id’, ‘desc’) ?>”< ?php if($this->isOrderCurrent(‘entity_id’) && $this->getCurrentDirection() == ‘desc’): ?> selected=”selected”< ?php endif; ?>>Newest Products</option> <option value=”< ?php echo $this->getOrderUrl(‘entity_id’, ‘asc’) ?>”< ?php if($this->isOrderCurrent(‘entity_id’) && $this->getCurrentDirection() == ‘asc’): ?> selected=”selected”< ?php endif; ?>>Oldest Products</option> </select>
Xong rồi xem thử nhé! Nguồn: http://stackoverflow.com/questions/11012195/magento-sort-by-entity-id