Magento:在类别页面中显示最后的缺货产品

Ale*_*lex 2 magento magento-1.7

我正在使用Magento 1.7.0.2,我在/app/code/core/Mage/Catalog/Block/Product/list.php中使用了这个代码行:

$this->_productCollection = $layer->getProductCollection()
                    ->joinField(
                        'inventory_in_stock', 
                        'cataloginventory_stock_item', 
                        'is_in_stock', 
                        'product_id=entity_id',
                        'is_in_stock>=0', 
                        'left')
                    ->setOrder('inventory_in_stock','desc');
Run Code Online (Sandbox Code Playgroud)

在排序位置和名称时,缺货产品是最后的.但是在对价格进行分类时,缺货产品处于正常状态并且不会持久.

我怎样才能让缺货产品即使在价格之后仍然是最后的?

Ric*_*ist 5

很棒的亚历克斯!提示:如果您想避免更改核心文件(并可能将其转换为模块),您可以将其添加到事件监听器,如下所示(在1.8.1.0上测试):

/**
 * Fires before a product collection is loaded
 *
 * @param Varien_Event_Observer $observer
 */
public function catalog_product_collection_load_before($observer)
{
    $collection = $observer->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(
        'on_top',
        '(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('on_top DESC');
    // Make sure on_top is the first order directive
    $order = $collection->getSelect()->getPart('order');
    array_unshift($order, array_pop($order));
    $collection->getSelect()->setPart('order', $order);
}
Run Code Online (Sandbox Code Playgroud)

编辑:改回catalog_product_collection_load_beforecatalog_product_collection_apply_limitations_before和固定的顺序部件优先级.