添加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
但是我不知道我將如何實現這樣的事情在我上面的代碼。
我看到你以前的答案卻是如何在這個工作我在做什麼?我將在哪裏適合我的代碼? –
嘗試將我的代碼放在'if(isset($ orderFilterType)&& $ orderFilterType =='price'&& isset($ dirFilterType)&& $ dirFilterType =='asc'){' 和'} else {' – TaganPablo
I' m努力尋找這會如何影響$ this - > _ productCollection的值,這需要返回。我得到解析語法錯誤簡單地把這個之間時,我的if語句 –