我尝试按数量过滤集合:
$_productCollection->addAttributeToFilter('qty', array('gt' => 30));
但这不起作用。
有人可以给我一个提示,按数量过滤产品系列吗?
我也尝试过:
$stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
->addFieldToFilter('qty', array('gt' => 99999999));
$entityIds = array();
foreach ($stockCollection as $item) {
$entityIds[] = $item->getOrigData('product_id');
}
$_productCollection->addAttributeToFilter('id', array('in' => $entityIds));
entityId数组为空,但$ _productCollection仍返回产品?!
答案 0 :(得分:5)
你第二次尝试就接近了。有一种在Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
/**
* Add filter by quantity to collection
*
* @param string $comparsionMethod
* @param float $qty
* @return Mage_CatalogInventory_Model_Resource_Stock_Item_Collection
*/
public function addQtyFilter($comparsionMethod, $qty)
{
$methods = array(
'<' => 'lt',
'>' => 'gt',
'=' => 'eq',
'<=' => 'lteq',
'>=' => 'gteq',
'<>' => 'neq'
);
if (!isset($methods[$comparsionMethod])) {
Mage::throwException(
Mage::helper('cataloginventory')->__('%s is not a correct comparsion method.', $comparsionMethod)
);
}
return $this->addFieldToFilter('main_table.qty', array($methods[$comparsionMethod] => $qty));
}
$stockIds = Mage::getModel('cataloginventory/stock_item')
->getCollection()
->addQtyFilter('>=', 30)
->getAllIds();
$products = Mage::getModel('catalog/product')
->getCollection()
->addIdFilter($stockIds)
->setPageSize(10);