如何让Magento按添加日期对目录中的产品进行排序?这在管理员中不是一个选项,所以猜测它需要在代码中的某个地方完成。
谢谢。
发布于 2011-08-26 17:14:05
如果你同意(不应该)修改核心文件,那么添加一个按日期排序的选项是非常容易的。只需修改app/code/core/Mage/Catalog/Model/Config.php文件,如下例所示:
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;
}如果你不喜欢修改核心文件,那就没那么容易了。在这种情况下,您必须创建以下文件:
app/etc/modules/Stackoverflow_Catalog.xml
<?xml version="1.0"?>
<config>
<modules>
<Stackoverflow_Catalog>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog />
</depends>
</Stackoverflow_Catalog>
</modules>
</config>app/code/local/Stackoverflow/Catalog/etc/config.xml
<?xml version="1.0"?>
<config>
<global>
<models>
<catalog>
<rewrite>
<config>Stackoverflow_Catalog_Model_Config</config>
</rewrite>
</catalog>
</models>
</global>
</config>app/code/local/Stackoverflow/Catalog/Model/Config.php
<?php
class Stackoverflow_Catalog_Model_Config extends Mage_Catalog_Model_Config {
public function getAttributeUsedForSortByArray() {
$options = parent::getAttributeUsedForSortByArray();
if (!isset($options['created_at'])) {
$options['created_at'] = Mage::helper('catalog')->__('Date');
}
return $options;
}
}小贴士:走一条干净的路,从长远来看它会有回报的。
发布于 2012-07-26 15:16:24
将此代码放入您的local.xml中,无需覆盖任何Magento核心文件。
如果在将来覆盖任何Magento核心文件,将会出现升级问题
<layout>
<catalog_category_default>
<reference name="product_list">
<action method="setAvailableOrders" json="value">
<value><![CDATA[
{"created_at" : "Latest","price":"Price"}
]]>
</value>
</action>
</reference>
<reference name="product_list_toolbar">
<action method="setDefaultDirection">
<dir>desc</dir>
</action>
</reference>
</catalog_category_default>
</layout>发布于 2012-02-04 01:47:29
我通过将app/code/core/Mage/Catalog/Block/Product/List.php复制到app/ code /local并在其_getProductCollection()方法的末尾添加一些排序代码来解决这个问题:
// 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;您可以使用'e.entity_id desc'或'e.created_at desc'进行排序。
https://stackoverflow.com/questions/2237513
复制相似问题