我试图在我的低库存集合中只获得已启用的产品。
我的代码如下:
/** @var $collection Mage_Reports_Model_Resource_Product_Lowstock_Collection */
$collection = Mage::getResourceModel('reports/product_lowstock_collection')
->addAttributeToSelect('*')
->setStoreId($storeId)
->filterByIsQtyProductTypes()
->joinInventoryItem('qty')
->joinInventoryItem('low_stock_date')
->useManageStockFilter($storeId)
->useNotifyStockQtyFilter($storeId)
->setOrder('qty', Varien_Data_Collection::SORT_ORDER_ASC);
但我不能给ORM添加这样的过滤器:
->addAttributeToFiler('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
有人知道怎么做吗?
您需要加入catalog_product_entity_int
表,该表保存每个产品的状态值,为了简化操作,请确保状态属性的attribute_id,在我的情况下是96。您可以从管理面板Manage Attributes
或eav_attribute
表中进行检查。
以下是要应用的内部联接:
$collection = Mage::getResourceModel('reports/product_lowstock_collection')
->addAttributeToSelect('*')
->setStoreId($storeId)
->filterByIsQtyProductTypes()
->joinInventoryItem('qty')
->joinInventoryItem('low_stock_date')
->useManageStockFilter($storeId)
->useNotifyStockQtyFilter($storeId)
->setOrder('qty', 'asc')
->getSelect()
->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_int', 'lowstock_inventory_item.product_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_int.entity_id',array('value'))
->where(Mage::getConfig()->getTablePrefix().'catalog_product_entity_int.value=1 AND '.Mage::getConfig()->getTablePrefix().'catalog_product_entity_int.attribute_id=96');
这是生成的SQL查询:
SELECT `e`.*, `lowstock_inventory_item`.`qty`, `lowstock_inventory_item`.`low_stock_date`, `catalog_product_entity_int`.`value` FROM `catalog_product_entity` AS `e`
INNER JOIN `cataloginventory_stock_item` AS `lowstock_inventory_item` ON e.entity_id = lowstock_inventory_item.product_id
INNER JOIN `catalog_product_entity_int` ON lowstock_inventory_item.product_id =catalog_product_entity_int.entity_id WHERE (((`e`.`type_id` = 'simple') OR (`e`.`type_id` = 'virtual') OR (`e`.`type_id` = 'giftcard'))) AND (IF(lowstock_inventory_item.use_config_manage_stock = 1, 1, lowstock_inventory_item.manage_stock) = 1) AND (qty < IF(lowstock_inventory_item.use_config_notify_stock_qty = 1, 1, lowstock_inventory_item.notify_stock_qty)) AND (catalog_product_entity_int.value=1 AND catalog_product_entity_int.attribute_id=96)