2013-04-08 274 views
2

我正在使用Magento 1.7.0.2,並且我在/ app/code/core/Mage/Catalog/Block/Product/list中使用了此代碼行。 php:Magento:缺貨中顯示產品類別頁最後的產品

$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'); 

當排序的位置和名稱缺貨產品是最後一次。但是,當按價格排序時,缺貨產品的排列順序並不是最後的。

我該如何使缺貨產品即使在價格之後也能持續使用?

回答

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) { 
在應用

/碼/ COR如果你有Magento 1.7,或者在app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php中。 0.0 +

0

我推薦你跑兩個系列 1.收藏的產品有現貨按價格排序。 - > addAttributeToFilter('is_in_stock',array('gt'=> 0)); 參考鏈接集合: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections

  • 收集的是由價格outofstock排序產品。
  • 請參考以下鏈接: Magento List all out of stock items

    +0

    如果你能告訴我究竟該怎麼做或給我的代碼將是完美的:) – Alex 2013-04-08 19:37:53

    +0

    有誰知道答案? – Alex 2013-04-09 06:54:49

    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_load_before from catalog_product_collection_apply_limitations_before和固定訂單部分優先級。

    相關問題