将零价格产品移动到目录产品列表的末尾(magento)
无论选择哪种排序顺序,我都需要将零价格产品推到目录产品列表的末尾 这段代码也做了同样的事情,但是有“缺货”的产品将零价格产品移动到目录产品列表的末尾(magento),magento,Magento,无论选择哪种排序顺序,我都需要将零价格产品推到目录产品列表的末尾 这段代码也做了同样的事情,但是有“缺货”的产品 <?php class Test_Ext_Model_Catalog_Observer extends Mage_Catalog_Model_Observer { public function rebuildCollection($observer) { $event = $observer->getEvent();
<?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;
}