Vil*_*kas 43
如果您可以(不应该)修改核心文件,则可以很容易地添加按日期排序选项.只需修改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;
}
Run Code Online (Sandbox Code Playgroud)
如果您不修改核心文件,那就不那么容易了.在这种情况下,您必须创建这一堆文件:
应用程序的/ etc /模块/ 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>
Run Code Online (Sandbox Code Playgroud)
应用程序/代码/本地/#1 /目录的/ etc/config.xml中
<?xml version="1.0"?>
<config>
<global>
<models>
<catalog>
<rewrite>
<config>Stackoverflow_Catalog_Model_Config</config>
</rewrite>
</catalog>
</models>
</global>
</config>
Run Code Online (Sandbox Code Playgroud)
应用程序/代码/本地/#1 /目录/型号/ 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;
}
}
Run Code Online (Sandbox Code Playgroud)
提示:采取干净的方式,从长远来看,它将付出代价.
use*_*711 14
将此代码放入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>
Run Code Online (Sandbox Code Playgroud)
小智 13
我通过将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;
Run Code Online (Sandbox Code Playgroud)
您可以使用'e.entity_id desc'或'e.created_at desc'排序.
Mar*_*ive 11
像这样
$_newest_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->setOrder('created_at', 'desc')
$_newest_productCollection->load();
Run Code Online (Sandbox Code Playgroud)
我不确定是否有一种简单的方法可以在不深入研究核心代码的情况下做到这一点。不过,我还没有尝试过,但它似乎应该有效:
创建一个新的日期属性。您会看到属性选项底部有一个选项,名为“用于在产品列表中排序”。为此选择“是”。然后将其添加到您的属性组中。然后,当您添加产品时,只需选择当前日期,您应该就可以使用它进行排序。要设置默认值,请进入系统>>配置>>目录>>前端,您将在“产品列表排序依据”选项中看到您的属性。
希望这对你有用。
| 归档时间: |
|
| 查看次数: |
38794 次 |
| 最近记录: |