2015-03-04 2 views
0

Мне нужно подтолкнуть продукты с нулевой ценой до конца списка продуктов каталога независимо от того, какой порядок сортировки выбран.Переведите продукты с нулевой ценой в конец списка продуктов (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; 
    } 
} 

Помогите!

ответ

0

Добавить этот код прямо между $ 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; 
}