Magento - Sort by Date Added

匿名 (未验证) 提交于 2019-12-03 01:13:01

问题:

How can I make Magento sort products in the catalog by the date they were added? This isn't an option in the admin so guessing it needs to be done in the code somewhere.

Thanks.

回答1:

It is quite easy to add a sorting by date option if you're OK (you shouldn't be) with modifying core files. Simply modify app/code/core/Mage/Catalog/Model/Config.php file as in example below:

public function getAttributeUsedForSortByArray() {     $options = array(         'position'  => Mage::helper('catalog')->__('Position'),          // HERE IS OUR NEW OPTION         'created_at' => Mage::helper('catalog')->__('Date')     );     foreach ($this->getAttributesUsedForSortBy() as $attribute) {         /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */         $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();     }      return $options; } 

It is not so easy, if you are not into modifying core files. In that case you must create this bunch of files:

app/etc/modules/Stackoverflow_Catalog.xml

truelocal

app/code/local/Stackoverflow/Catalog/etc/config.xml

Stackoverflow_Catalog_Model_Config

app/code/local/Stackoverflow/Catalog/Model/Config.php

__('Date');         }         return $options;     } } 

TIP: Go for a clean way, it will pay of in a long run.



回答2:

I solved this by copying app/code/core/Mage/Catalog/Block/Product/List.php into app/code/local and adding some sorting code at the end of its _getProductCollection() method:

// sort by created_at date or entity_id if(!isset($_GET['order'])) {     $this->_productCollection->getSelect()->reset( Zend_Db_Select::ORDER );     $this->_productCollection->getSelect()->order('e.entity_id desc'); } return $this->_productCollection; 

You can use either 'e.entity_id desc' or 'e.created_at desc' to sort.



回答3:

Place this code into your local.xml no need to override any Magento core files.

If you override any Magento core files in future upgrade problem will occur

desc


回答4:

Like this

$_newest_productCollection = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->addAttributeToFilter('visibility', $visibility) ->setOrder('created_at', 'desc') $_newest_productCollection->load(); 


回答5:

Yust for update (works with Mage 1.7.0.2): in an setupscript of an own module:

$installer = $this; $installer->startSetup();  $productEntityTypeId = Mage::getModel('catalog/product')->getResource()->getEntityType()->getId();  //lets change created_at properties ////////////////////////////////////////////////// $installer->updateAttribute($productEntityTypeId, 'created_at', array(     'visible_on_front' => true,     'used_in_product_listing' => true     'used_for_sort_by' => 1,     'frontend_label' => 'Created at' ));  $installer->endSetup();  // mark index as "reindex required" $indexerCodes = array(     'catalog_product_flat' ); $indexer = Mage::getModel('index/process'); foreach ($indexerCodes as $code) {     $indexer->load($code, 'indexer_code')         ->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX); } 

and in the catalog.xml layout handle:

 ... desc

After this you can select created_at as default sorting in the system configuration or in the catagory display settings



回答6:

I'm not sure that there is an easy way to do that, without digging into the core code. However, I haven't tried this but it seems like it should work:

Create a new Date attribute. You'll see that there is an option at the bottom of the attribute options called "Used for sorting in product listing". Select Yes for that. Then and add it to your attribute group. Then when you add a product, just select the current date, and you should be able to use that for sorting. To set the default of that, go into System >> Configuration >> Catalog >> Frontend and you'll see your attribute in the "Product listing sort by" option.

Hope that works out for you.



回答7:

You can try below

app/code/core/mage/catalog/model/resource/eav/mysql4/product/collection.php 

in "public function addAttributeToSort($attribute, $dir='asc')"

after

$this->getSelect()->order("cat_index_position {$dir}"); 

add this

$this->getSelect()->order("e.entity_id desc"); 


回答8:

I've done it by rewriting the class:

Mage_Catalog_Model_Category_Attribute_Source_Sortby 

and function:

public function getAllOptions() {     if (is_null($this->_options)) {         $this->_options = array(array(             'label' => Mage::helper('catalog')->__('Best Value'),             'value' => 'position'         ));         $this->_options = array(array(             'label' => Mage::helper('catalog')->__('Created At'),             'value' => 'created_at'         ));         foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {             $this->_options[] = array(                 'label' => Mage::helper('catalog')->__($attribute['frontend_label']),                 'value' => $attribute['attribute_code']             );         }     }     return $this->_options; } 


回答9:

I've developed a free extension that does the trick and is pretty easy to install and use. Fell free guys to download it.

https://magento.mdnsolutions.com/extensions/mdn-sort-by-date.html

Cheers,



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!