将零价产品移至目录产品列表的末尾 (magento)

Move zero price products to the end of the catalog product list (magento)

无论选择哪种排序顺序,我都需要将零价产品推到目录产品列表的末尾。

此代码执行相同的操作,但使用 "out of stock" 个产品。

<?php
class Test_Ext_Model_Catalog_Observer extends Mage_Catalog_Model_Observer
{
    public function rebuildCollection($observer)
    {
        $event = $observer->getEvent();     
        $collection = $event->getCollection();

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

        $collection->addExpressionAttributeToSelect(
            'stock_down',
            '(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) 
            AND (_inventory_table.is_in_stock = 1))
            OR  ((_inventory_table.use_config_manage_stock = 0) 
            AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) 
            THEN 1 ELSE 0 END)',
            array());
        $collection->getSelect()->order('stock_down DESC');

        return $collection;
    }
}  

请帮忙!

在 $collection->getSelect()->order('stock_down DESC') 之间添加此代码;和 return $collection;

$collection->addExpressionAttributeToSelect(
    'zero_price_down',
    '(CASE WHEN (price_index.price = 0) THEN 0 ELSE 1 END)',
    array());
$collection->getSelect()->order('zero_price_down DESC');

并且无论选择哪种排序顺序,您都会获得将零价和缺货产品移至目录产品列表末尾的方法:

public function rebuildCollection($observer)
{
    $event = $observer->getEvent();     
    $collection = $event->getCollection();

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

    $collection->addExpressionAttributeToSelect(
        'stock_down',
        '(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) 
        AND (_inventory_table.is_in_stock = 1))
        OR  ((_inventory_table.use_config_manage_stock = 0) 
        AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) 
        THEN 1 ELSE 0 END)',
        array());
    $collection->getSelect()->order('stock_down DESC');

    $collection->addExpressionAttributeToSelect(
        'zero_price_down',
        '(CASE WHEN (price_index.price = 0) THEN 0 ELSE 1 END)',
        array());
    $collection->getSelect()->order('zero_price_down DESC');

    return $collection;
}