如何根据特殊价格可用性加载特定类别的产品?
How to load products from a specific category based on special price availability?
我有以下代码
$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
}
一旦您在您的类别上声明 ->getProductCollection()
,您最终会得到一个 Mage_Catalog_Model_Resource_Product_Collection
类型的对象,它基本上是一个与其他所有产品集合一样的产品集合。
由于此集合是 EAV 集合,您可以借助函数 addAttributeToFilter
对其进行过滤
并且因为您实际上想要一个 OR
(where 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 > 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` ON
cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND
cat_index.category_id = '15' INNER JOIN
`catalog_product_entity_decimal` AS `at_special_price` ON
(`at_special_price`.`entity_id` = `e`.`entity_id`) AND
(`at_special_price`.`attribute_id` = '76') AND
(`at_special_price`.`store_id` = 0) INNER JOIN
`catalog_product_entity_datetime` AS
`at_special_from_date_default` ON
(`at_special_from_date_default`.`entity_id` = `e`.`entity_id`)
AND (`at_special_from_date_default`.`attribute_id` = '77') 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` = '77') AND
(`at_special_from_date`.`store_id` = 1) WHERE
((at_special_price.value IS NULL) OR (IF(at_special_from_date.value_id
> 0, at_special_from_date.value, at_special_from_date_default.value) < '2016-01-08 22:05:14'))
我有以下代码
$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
}
一旦您在您的类别上声明 ->getProductCollection()
,您最终会得到一个 Mage_Catalog_Model_Resource_Product_Collection
类型的对象,它基本上是一个与其他所有产品集合一样的产品集合。
由于此集合是 EAV 集合,您可以借助函数 addAttributeToFilter
并且因为您实际上想要一个 OR
(where 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 > 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` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.category_id = '15' INNER JOIN `catalog_product_entity_decimal` AS `at_special_price` ON (`at_special_price`.`entity_id` = `e`.`entity_id`) AND (`at_special_price`.`attribute_id` = '76') AND (`at_special_price`.`store_id` = 0) INNER JOIN `catalog_product_entity_datetime` AS `at_special_from_date_default` ON (`at_special_from_date_default`.`entity_id` = `e`.`entity_id`) AND (`at_special_from_date_default`.`attribute_id` = '77') 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` = '77') AND (`at_special_from_date`.`store_id` = 1) WHERE ((at_special_price.value IS NULL) OR (IF(at_special_from_date.value_id > 0, at_special_from_date.value, at_special_from_date_default.value) < '2016-01-08 22:05:14'))