我正在尝试编辑我的产品在Magento产品列表中的订购方式,一旦用户选择价格下拉列表以显示最便宜的产品。这将显示列出时顶部为0的价格,显示为:
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
我已设法编辑文件_getProductCollection()
内的/app/code/core/Mage/Catalog/Block/Product/List.php
方法(不要担心我已复制到本地)并添加以下几行:
$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
但是我不知道如何在上面的代码中实现这样的东西。
答案 0 :(得分: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']);
答案 1 :(得分: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();
}