Magento - 按添加日期排序 [英] Magento - Sort by Date Added

查看:29
本文介绍了Magento - 按添加日期排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 Magento 按添加日期对目录中的产品进行排序?这不是管理员中的一个选项,所以猜测它需要在代码中的某处完成.

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.

谢谢.

推荐答案

如果您可以(不应该)修改核心文件,那么添加按日期排序选项非常容易.只需修改 app/code/core/Mage/Catalog/Model/Config.php 文件,如下例所示:

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

<?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;
    }
}

提示:走一条干净的路,从长远来看是值得的.

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

这篇关于Magento - 按添加日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆