我正在尝试创建一个列出所有缺货商品的cms页面。
protected function _getProductCollection()
{
$collection = Mage::getModel('catalog/product')->getCollection();
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToSort('entity_id', 'desc') //<b>THIS WILL SHOW THE LATEST PRODUCTS FIRST</b>
->addAttributeToFilter('stock_status', 0)
->setPageSize($this->get_prod_count())
->setCurPage($this->get_cur_page());
$this->setProductCollection($collection);
return $collection;
}
但它不起作用。它列出了所有项目。列出所有qty = 0或is_in_stock = 0的项目的方法是什么?
答案 0 :(得分:7)
这是我一直在使用的功能
function getOutOfStockProducts()
{
$products = Mage::getModel('catalog/product')
->getCollection()
->joinField(
'is_in_stock',
'cataloginventory/stock_item',
'is_in_stock',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
)
->addAttributeToFilter('is_in_stock', array('eq' => 0));
return $products;
}
答案 1 :(得分:6)
你应该使用不同的集合
$outOfStockItems = Mage::getModel('cataloginventory/stock_item')
->getCollection()
->addFieldToFilter('is_in_stock', 0);
答案 2 :(得分:2)
以下是我最终使用的方法。提供后端缺货的所有商品列表
protected function _getProductCollection()
{
$stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
/*->addFieldToFilter('is_in_stock', 0);*/ //this can also be used to filter
->addFieldToFilter('qty', 0);
$productIds = array();
foreach ($stockCollection as $item) {
$productIds[] = $item->getOrigData('product_id');
}
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection = $this->_addProductAttributesAndPrices($productCollection)
->addIdFilter($productIds)
->addAttributeToSort('entity_id', 'desc') //THIS WILL SHOW THE LATEST PRODUCTS FIRST
->setPageSize($this->get_prod_count())
->setCurPage($this->get_cur_page());
$this->setProductCollection($productCollection);
return $productCollection;
}// _getProductCollection
}// Mage_Catalog_Block_Product_Outofstock