我在catalog_block_product_list_collection事件的观察者中获取产品集合
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Itdelight_Sorting>
<version>1.0.0.1</version>
</Itdelight_Sorting>
</modules>
<global>
<blocks>
<sorting>
<class>Itdelight_Sorting_Block</class>
</sorting>
</blocks>
<models>
<sorting>
<class>Itdelight_Sorting_Model</class>
</sorting>
</models>
<events>
<catalog_block_product_list_collection>
<observers>
<options>
<type>singleton</type>
<class>Itdelight_Sorting_Model_Observer</class>
<method>sort</method>
</options>
</observers>
</catalog_block_product_list_collection>
</events>
</global>
</config>
在sort函数的文件Observer中,我需要为这个集合创建一个sql表达式,但我不知道它是怎么做的。
class Itdelight_Sorting_Model_Observer {
/**
*
* @param Varien_Event_Observer $observer
*/
public function sort(Varien_Event_Observer $observer){
$event = $observer->getEvent();
$collection = $event->getCollection();
}
}
答案 0 :(得分:0)
您可能需要添加表达式以确定该值是否为零,然后添加排序依据。问题是前端用户选择的排序,可能需要查看。没有经过测试,但这样的事情可能会这样做。
$collection->addExpressionAttributeToSelect('zero_price', 'IF(price = 0, 1, 0)');
$collection->getSelect()->order('zero_price ASC');
您可以使用var_dump($collection->getSelect()->__toString()); die();
输出sql查询来调试sql。
答案 1 :(得分:0)
我已经解决了这个问题。我重写了工具栏块并添加了代码:
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
if(($this->getCurrentDirection()=='asc')){
$this->_collection->getSelect()->columns(array('price_zero' => new Zend_Db_Expr('IF(price_index.price > 0,price_index.price, 9999999)')));
$this->_collection->getSelect()->order('price_zero ASC');
}else{
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}
return $this;
}
感谢您的帮助:)