开发者

Magento - Sort by Date Added

How can I make Magento sort products in the catalog by the da开发者_StackOverflow社区te they were added? This isn't an option in the admin so guessing it needs to be done in the code somewhere.

Thanks.


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.


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

<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>


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.


Like this

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


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:

<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
...
<action method="setDefaultDirection"><dir>desc</dir></action>
</block>

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


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.


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");


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜