您的示例未考虑“ use config”的值。
让我们看一下如何addInStockFilterToCollection
工作:
public function addInStockFilterToCollection($collection)
{
$this->getResource()->setInStockFilterToCollection($collection);
return $this;
}
好的,它委托给另一种方法:
public function setInStockFilterToCollection($collection)
{
$manageStock = Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK);
$cond = array(
'{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
'{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0',
);
if ($manageStock) {
$cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';
} else {
$cond[] = '{{table}}.use_config_manage_stock = 1';
}
$collection->joinField(
'inventory_in_stock',
'cataloginventory/stock_item',
'is_in_stock',
'product_id=entity_id',
'(' . join(') OR (', $cond) . ')'
);
return $this;
}
这将使库存表符合以下条件:
产品不使用全局配置,并且“管理库存”设置为“是” ,并且有库存
要么
产品不使用全局配置,并且“管理库存”设置为“否”
要么
- 产品使用全局配置,并且如果全局配置为“管理库存=是”,则表示有库存
您需要反转条件,如下所示:
产品不使用全局配置,并且“管理库存”设置为“是” ,并且没有库存
要么
产品采用全球配置和全球配置是“管理股票= YES” 和无货
说明:您仅采用实际检查了in_stock的条件,并将比较值更改为0。未检查in_stock的条件(“ manage stock” =“ no”)意味着该产品始终处于库存状态,而与库存状态无关,因此我们不会将它们包括在“缺货”查询中。
然后这是您的代码:
public function setOutOfStockFilterToCollection($collection)
{
$manageStock = Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK);
$cond = array(
'{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=0'
);
if ($manageStock) {
$cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=0';
}
$collection->joinField(
'inventory_in_stock',
'cataloginventory/stock_item',
'is_in_stock',
'product_id=entity_id',
'(' . join(') OR (', $cond) . ')'
);
return $this;
}