gpt4 book ai didi

玛根托 : Out of stock products showing last in the category page

转载 作者:行者123 更新时间:2023-12-03 09:28:10 25 4
gpt4 key购买 nike

我正在使用 Magento 1.7.0.2,并且我在/app/code/core/Mage/Catalog/Block/Product/list.php 中使用了以下代码行:

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

按位置和名称排序时,缺货产品排在最后。但按价格排序时,缺货的产品按正常顺序排列,不排在最后。

怎样才能让缺货的产品在价格之后的排序中排在最后?

最佳答案

很高兴找到亚历克斯!提示:如果您想避免更改核心文件(并可能将其放入模块中),您可以将其添加到事件监听器,如下所示(在 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_apply_limitations_before改回catalog_product_collection_load_before,并修复了订单部分优先级。

关于玛根托 : Out of stock products showing last in the category page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com