简体   繁体   English

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

[英]How to sort product collection on stock status in magento

I want to sort product collection on category page so that the in stock products shall appear before out of stock products. 我想在类别页面上对产品集合进行排序,以便有库存的产品应在无库存的产品之前出现。 All the out of stock products will be displayed after the in stock products. 所有缺货的产品将显示在有货的产品之后。 I have already rewritten the Mage_Product_Block_List block. 我已经重写了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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM