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

时间:2013-04-08 13:11:44

标签: 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');

在对位置和名称进行分类时,缺货产品是最后的。但是在对价格进行分类时,缺货产品的正常订单不会持久。

我怎样才能让缺货产品在价格之后排在最后?

3 个答案:

答案 0 :(得分:5)

很高兴找到Alex!提示:如果您想避免更改核心文件(并可能将其转换为模块),您可以将其添加到事件监听器,如下所示(在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);
}

编辑:从 catalog_product_collection_apply_limitations_before 更改回 catalog_product_collection_load_before 并修改订单部件优先顺序。

答案 1 :(得分:0)

我建议您运行两个集合 1.库存产品的收集按价格排序。 - > addAttributeToFilter(' is_in_stock',array(' gt' => 0)); 收集参考链接: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections

  1. 按价格分类的超重产品的收集。
  2. 参考此链接: Magento List all out of stock items

答案 2 :(得分:0)

我找到了答案

您必须添加以下代码:

$this->getSelect()->joinLeft(
            array('_inventory_table'=>$this->getTable('cataloginventory/stock_item')),
            "_inventory_table.product_id = e.entity_id",
            array('is_in_stock', 'manage_stock')
        );
        $this->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());
        $this->getSelect()->order('on_top DESC');

就在

之前
if ($attribute == 'price' && $storeId != 0) {

在app / code / core / Mage / Catalog / Model / Resource / Eav / Mysql4 / Product / Collection.php

或者在app / code / core / Mage / Catalog / Model / Resource / Product / Collection.php中,如果你有Magento 1.7.0.0 +