如何在Magento的低库存中仅获得启用的产品

时间:2015-12-02 16:01:38

标签: php mysql magento zend-framework orm

我试图只在我的低库存集合中获得启用的产品。

我的代码如下所示:

/** @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))

任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

您需要加入catalog_product_entity_int表,其中包含每个产品的状态值,为了方便起见,请确保status属性的attribute_id,在我的情况下为96.您可以检查它来自管理面板Manage Attributeseav_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)