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

  • 本文关键字:magento 产品列表 移动 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;
    }
}  

请帮忙!

在$collection->getSelect()->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;
}

最新更新