Magento:缺货中显示产品类别页最后的产品
我正在使用Magento 1.7.0.2,并且我在/ app/code/core/Mage/Catalog/Block/Product/list中使用了此代码行。 php:Magento:缺货中显示产品类别页最后的产品
$this->_productCollection = $layer->getProductCollection()
->joinField(
'inventory_in_stock',
'cataloginventory_stock_item',
'is_in_stock',
'product_id=entity_id',
'is_in_stock>=0',
'left')
->setOrder('inventory_in_stock','desc');
当排序的位置和名称缺货产品是最后一次。但是,当按价格排序时,缺货产品的排列顺序并不是最后的。
我该如何使缺货产品即使在价格之后也能持续使用?
我找到了答案
您必须添加以下代码:
$this->getSelect()->joinLeft(
array('_inventory_table'=>$this->getTable('cataloginventory/stock_item')),
"_inventory_table.product_id = e.entity_id",
array('is_in_stock', 'manage_stock')
);
$this->addExpressionAttributeToSelect('on_top',
'(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) AND (_inventory_table.is_in_stock = 1)) OR ((_inventory_table.use_config_manage_stock = 0) AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) THEN 1 ELSE 0 END)',
array());
$this->getSelect()->order('on_top DESC');
就在
if ($attribute == 'price' && $storeId != 0) {
在应用
/码/ COR如果你有Magento 1.7,或者在app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php中。 0.0 +
我推荐你跑两个系列 1.收藏的产品有现货按价格排序。 - > addAttributeToFilter('is_in_stock',array('gt'=> 0)); 参考链接集合: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections
- 收集的是由价格outofstock排序产品。
请参考以下链接: Magento List all out of stock items
很好找Alex!小贴士:如果你想避免更改核心文件(也可能使之成为一个模块这一点),你可以将其添加到事件侦听器,像这样(在1.8.1.0测试):
/**
* Fires before a product collection is loaded
*
* @param Varien_Event_Observer $observer
*/
public function catalog_product_collection_load_before($observer)
{
$collection = $observer->getCollection();
$collection->getSelect()->joinLeft(
array('_inventory_table'=>$collection->getTable('cataloginventory/stock_item')),
"_inventory_table.product_id = e.entity_id",
array('is_in_stock', 'manage_stock')
);
$collection->addExpressionAttributeToSelect(
'on_top',
'(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) AND (_inventory_table.is_in_stock = 1)) OR ((_inventory_table.use_config_manage_stock = 0) AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) THEN 1 ELSE 0 END)',
array()
);
$collection->getSelect()->order('on_top DESC');
// Make sure on_top is the first order directive
$order = $collection->getSelect()->getPart('order');
array_unshift($order, array_pop($order));
$collection->getSelect()->setPart('order', $order);
}
编辑:改变返回catalog_product_collection_load_before from catalog_product_collection_apply_limitations_before和固定订单部分优先级。
如果你能告诉我究竟该怎么做或给我的代码将是完美的:) – Alex 2013-04-08 19:37:53
有谁知道答案? – Alex 2013-04-09 06:54:49