2

一旦用户选择了价格下拉菜单以首先显示最便宜的,我正在尝试编辑我的产品在 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

但是我不知道如何在上面的代码中实现这样的东西。

4

2 回答 2

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']);
于 2013-04-02T12:11:58.030 回答
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();
}
于 2013-04-08T16:09:25.480 回答