2

我正在使用 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');

排序位置和名称时,缺货产品排在最后。但是在按价格排序时,缺货的产品不是最后的正常顺序。

我怎样才能使缺货产品即使在价格之后的排序中也能保持最后?

4

3 回答 3

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);
}

编辑:从catalog_product_collection_apply_limitations_before改回catalog_product_collection_load_before并固定订单零件优先级。

于 2014-06-16T08:39:40.440 回答
0

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

  1. 缺货产品的集合按价格排序。

请参阅此链接: Magento 列出所有缺货商品

于 2013-04-08T18:45:55.927 回答
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 +

于 2013-04-10T10:55:29.630 回答