如何在magento中对库存状态的产品集合进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在magento中对库存状态的产品集合进行排序相关的知识,希望对你有一定的参考价值。

我想在类别页面上对产品集合进行排序,以便在缺货产品之前出现库存产品。所有缺货产品将在库存产品后显示。我已经重写了Mage_Product_Block_List块。

答案
You can use catalog_product_collection_load_before event.   
And in the observer:

public function modifyProductCollection(Varien_Event_Observer $observer) {
    //catalog_product_collection_load_before
    $collection = $observer->getCollection();

    $collection->getSelect()->joinLeft(
                array('_inventory_table' => $collection->getTable('cataloginventory/stock_item')),
                "_inventory_table.product_id = e.entity_id",
                array('is_in_stock')
            )
            ->order('is_in_stock DESC')
            ->order('created_at DESC');
}

以上是关于如何在magento中对库存状态的产品集合进行排序的主要内容,如果未能解决你的问题,请参考以下文章