如何根据特价可用性加载特定类别的产品?

时间:2016-01-08 16:21:40

标签: magento magento-1.7 magento-1.8 magento-1.4

我有以下代码

$promoCatId = (int)Mage::getStoreConfig('promoprodsec/promoprodgroup/promocategoryid');

$products = Mage::getModel('catalog/category')->setStoreId($store)->load($promoCatId)
    ->getProductCollection();

$allProductsInPromoCategory = $products->getAllIds();

它为我提供了$ promoCatId类别的所有产品。

现在我想从$allProductsInPromoCategory

的所有产品中获取所有没有特价或特价日期范围的产品已过期

如何在不使用foreach循环的情况下再次加载每个产品系列来实现此目的?

foreach ($allProductsInPromoCategory as $promoprod) {
    Mage:getModel('catalog/product')->load( $promoprod);

    //do validation for the special price criteria
}

1 个答案:

答案 0 :(得分:2)

在您的类别中说明->getProductCollection()后,您最终会得到Mage_Catalog_Model_Resource_Product_Collection类型的对象,该对象基本上就像其他所有产品一样。

由于此集合是 EAV 集合,您可以借助函数addAttributeToFilter

过滤它

由于您确实需要ORwhere special price is null or special to date <= now()),因此您希望两个条件位于同一个addAttributeToFilter

所以我想说你可以做到这一点:

$promoCatId  = (int)Mage::getStoreConfig('promoprodsec/promoprodgroup/promocategoryid');

$products = Mage::getModel('catalog/category')
                ->setStoreId($store)
                ->load(promoCatId)
                ->getProductCollection()
                ->addAttributeToFilter(
                    array(
                        array('attribute' => 'special_price', 'null' => true),
                        array('attribute' => 'special_from_date', 'lt' => Mage::getModel('core/date')->date(Varien_Date::DATETIME_PHP_FORMAT))
                    )
                );

$allProductsInPromoCategory = $products->getAllIds();

echo $products->getSelect();

有用的知道:

  • 我实际上是使用Mage::getModel('core/date')->date(Varien_Date::DATETIME_PHP_FORMAT)来获取相对于商店配置的区域设置时区的当前日期
  • 集合上的
  • echo $products->getSelect();将显示Magento完成的查询,真正有助于实际查看您真正想要的位置。在我的情况下,它显示:
  

SELECT`e`。*,`cat_index` .position` AS`cat_index_position`,   `at_special_price` .value` AS`special_price`,   IF(at_special_from_date.value_id&gt; 0,at_special_from_date.value,   at_special_from_date_default.value)AS`special_from_date` FROM   `catalog_product_entity` AS`e`INNER JOIN   `catalog_category_product_index` AS`cat_index`开   cat_index.product_id = e.entity_id AND cat_index.store_id = 1 AND   cat_index.category_id =&#39; 15&#39;内部联接   `catalog_product_entity_decimal` AS`at_special_price` ON   (`at_special_price` .entity_id` =`e` .entity_id`)AND   (`at_special_price` .attribute_id` =&#39; 76&#39;)AND   (`at_special_price` .store_id` = 0)INNER JOIN   `catalog_product_entity_datetime` AS   `at_special_from_date_default`开   (`at_special_from_date_default` .entity_id` =`e` .entity_id`)   AND(`at_special_from_date_default` .attribute_id` =&#39; 77&#39;)AND   `at_special_from_date_default` .store_id` = 0 LEFT JOIN   `catalog_product_entity_datetime` AS`at_special_from_date`ON   (`at_special_from_date` .entity_id` =`e` .entity_id`)AND   (`at_special_from_date` .attribute_id` =&#39; 77&#39;)AND   (`at_special_from_date` .store_id` = 1)WHERE   ((at_special_price.value IS NULL)OR(IF(at_special_from_date.value_id)   &GT; 0,at_special_from_date.value,at_special_from_date_default.value)&lt; &#39; 2016-01-08 22:05:14&#39;))