Php 将案例或类似语句添加到_productCollection->;在Magento中添加AttributeToSort()

Php 将案例或类似语句添加到_productCollection->;在Magento中添加AttributeToSort(),php,sql,magento,Php,Sql,Magento,一旦用户选择价格下拉列表以首先显示最便宜的,我将尝试编辑我的产品在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 我已经设法编辑了文件/app/code/core/Mage/C

一旦用户选择价格下拉列表以首先显示最便宜的,我将尝试编辑我的产品在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
我已经设法编辑了文件
/app/code/core/Mage/Catalog/Block/Product/List.php中的
\u 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

但是,我不知道如何在上面的代码中实现类似的内容。

就像我之前说过的:

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

这是你和我合并的代码

$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();
}

我看到了你之前的答案,但这在我所做的工作中是如何工作的?在我的代码中应该放在哪里?试着把我的代码放在if(isset($orderFilterType)&&$orderFilterType=='price'&&isset($dirFilterType)&&$dirFilterType=='asc'){
}else之间{
我正在努力找出这对需要返回的$this->\u productCollection的值有何影响。当我简单地将其放在if语句之间时,就会出现解析语法错误。这里是与我的代码合并的代码`$orderFilterType=$this->getRequest()->getParam('order');$dirFilterType=$this->getRequest()->getParam('dir');if(isset($orderFilterType)&&$orderFilterType='price'&&isset($dirFilterType)&&$dirFilterType='asc'){$this->\U productCollection=$layer->getProductCollection()->addAttributeToSort('price','DESC');}其他{$this->\U productCollection=$layer->getProductCollection()}`嗨,塔根…谢谢你,我明天会检查这个,当我回到我的电脑上,打开这个网站,让你知道我的进展情况…说真的,谢谢你到目前为止的所有帮助…我刚刚尝试过这个,但是当我试着按价格asc订购时,它在网站上抛出异常错误,问题是日志部分太长,无法粘贴到这里我更新了我的que和我上面使用的代码保持一致,以防你发现我在什么地方犯了错误