【问题标题】:Magento : Out of stock products showing last in the category pageMagento : 缺货产品在类别页面中显示在最后
【发布时间】:2013-04-08 13:11:44
【问题描述】:

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

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

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

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    很高兴找到亚历克斯!提示:如果您想避免更改核心文件(并可能将其放入模块中),您可以将其添加到事件侦听器中,如下所示(在 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 并固定订单部分优先级。

    【讨论】:

      【解决方案2】:

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

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

      参考这个链接: Magento List all out of stock items

      【讨论】:

      • 如果你能准确地告诉我该怎么做或给我代码将是完美的:)
      • 有人知道答案吗?
      【解决方案3】:

      我找到了答案

      您必须添加此代码:

      $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中

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-30
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 2012-06-04
        相关资源
        最近更新 更多