2013-04-02 16 views
2

添加CASE或類似的語句來_productCollection-> AddAttributeToSort()我想修改我的產品如何在Magento的產品列表排序一旦用戶選擇了價格下拉到第一顯示最便宜的。這顯示了0上上市的時候頂部的價格,表現爲:在Magento

Product 4 £0 
Product 5 £0 
Product 1 £50 
Product 2 £65 
Product 3 £80 

目前,它會顯示爲這樣:

Product 1 £50 
Product 2 £65 
Product 3 £80 
Product 4 £0 
Product 5 £0 

我已成功地編輯文件/app/code/core/Mage/Catalog/Block/Product/List.php_getProductCollection()方法(別擔心,我複製到本地),並加入這幾行:

$orderFilterType = $this->getRequest()->getParam('order'); 
$dirFilterType = $this->getRequest()->getParam('dir'); 

if(isset($orderFilterType) && $orderFilterType == 'price' && isset($dirFilterType) && $dirFilterType == 'asc') { 

$this->_productCollection = $layer->getProductCollection(); 

$order_field = 'new_price_for_sort'; 

$this->_productCollection->getSelect()->joinLeft(array('a' => 'catalog_product_entity_price'), 'a.product_id=e.entity_id AND a.attibute_id=PRICE or SPECIAL PRICE ATTRIBUTE ID', array('new_price_for_sort' => new Zend_Db_Expr('IF(a.value > 0,1, 0)'))); 

$this->_productCollection->getSelect()->reset(Zend_Db_Select::ORDER); 

$dir = 'DESC'; 

if (!$this->_productCollection->isEnabledFlat()) { 

$this->_productCollection->setOrder($order_field, $dir); 

} else { 

$this->_productCollection->getSelect()->order($order_field . ' ' . $dir); 

} 

$this->_productCollection->setOrder('price', $dir); 


} else { $this->_productCollection = $layer->getProductCollection(); } 

這意味着任何時候有人從下拉菜單中選擇價格,ASC然後過濾器它增加了使用排序其中addAttributeToSort工作正常價格,但我不能工作,如何讓0價位產品使用此去底部。

在SQL我通常使用CASE語句,如:

SELECT * FROM table ORDER BY CASE price WHEN 0 then 1 ELSE 0 END, price 

但是我不知道我將如何實現這樣的事情在我上面的代碼。

回答

1

就像我以前說:

$order_field = 'new_price_for_sort'; 
$collection->getSelect()->joinLeft(array('a' => 'catalog_product_entity_price'), // or here you will need Price Index Table Name if you have Groupped or configurable products. But need to remember that non-simple products always have 0 price and MAX_PRICE/MIN_PRICE should be used then 
'a.product_id=e.entity_id AND a.attibute_id=PRICE or SPECIAL PRICE ATTRIBUTE ID', // or here you will not require attribute_id condition if you are using INDEX TABLE 
array('new_price_for_sort' => new Zend_Db_Expr('IF(a.value > 0,1, 0)'), // which mean that in new field we will have price if it's not 0, and will have big integer is real price is zero. 
        )); 
      } 
// now we are reseting current order by 
      $collection->getSelect()->reset(Zend_Db_Select::ORDER); 

// and creating new order by new field, but with saving direction of order 
$dir = 'DESC'; // as we need to have products WITH price first, and after that WITHOUT price 
      if (!$collection->isEnabledFlat()) { 
       $collection->setOrder($order_field, $dir); 
      } else { 
       $collection->getSelect()->order($order_field . ' ' . $dir); 
      } 
//and now add Order by price with keeping direction that user have choosed 
$collection->setOrder('price', $arr['dir']); 
+0

我看到你以前的答案卻是如何在這個工作我在做什麼?我將在哪裏適合我的代碼? –

+0

嘗試將我的代碼放在'if(isset($ orderFilterType)&& $ orderFilterType =='price'&& isset($ dirFilterType)&& $ dirFilterType =='asc'){' 和'} else {' – TaganPablo

+0

I' m努力尋找這會如何影響$ this - > _ productCollection的值,這需要返回。我得到解析語法錯誤簡單地把這個之間時,我的if語句 –

0

這裏是你的代碼合併礦

$orderFilterType = $this->getRequest()->getParam('order'); 
$dirFilterType = $this->getRequest()->getParam('dir'); 

if(isset($orderFilterType) && $orderFilterType == 'price' && isset($dirFilterType) && $dirFilterType == 'asc') { 

$this->_productCollection = $layer->getProductCollection(); 
$order_field = 'new_price_for_sort'; 
$this->_productCollection->getSelect()->joinLeft(array('a' => 'catalog_product_entity_price'), // or here you will need Price Index Table Name if you have Groupped or configurable products. But need to remember that non-simple products always have 0 price and MAX_PRICE/MIN_PRICE should be used then  
'a.product_id=e.entity_id AND a.attibute_id=PRICE or SPECIAL PRICE ATTRIBUTE ID', // or here you will not require attribute_id condition if you are using INDEX TABLE 
array('new_price_for_sort' => new Zend_Db_Expr('IF(a.value > 0,1, 0)'), // which mean that in new field we will have price if it's not 0, and will have big integer is real price is zero. 
)); 
    } 
// now we are reseting current order by 
$this->_productCollection->getSelect()->reset(Zend_Db_Select::ORDER); 
// and creating new order by new field, but with saving direction of order 
$dir = 'DESC'; // as we need to have products WITH price first, and after that WITHOUT price 
if (!$this->_productCollection->isEnabledFlat()) { 
    $this->_productCollection->setOrder($order_field, $dir); 
  } else { 
    $this->_productCollection->getSelect()->order($order_field . ' ' . $dir); 
} 
//and now add Order by price with keeping direction that user have choosed  
$this->_productCollection->setOrder('price', $arr['dir']); 
} else { 
    $this->_productCollection = $layer->getProductCollection(); 
} 
+0

嗨塔安...感謝這一點,當我回到我的電腦上使用的網站,我會檢查這個明天,讓你知道我的身體情況如何......說真的,感謝您的幫助,所以遠... –

+0

我只是嘗試這樣做,但是當我試圖通過價格遞增訂購的網站在其拋出異常錯誤,麻煩的是日誌部太長,這裏貼上 –

+0

伊夫更新我的問題與使用的確切代碼IVE以上情況可以幫助您查看是否在某處出現了錯誤 –