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

时间:2015-03-04 20:03:08

标签: magento

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

此代码执行相同的操作,但使用"缺货"产品

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

请帮忙!

1 个答案:

答案 0 :(得分:0)

在$ collection-&gt; getSelect() - &gt; order('stock_down DESC')之间添加此代码;并返回$ 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;
}