检索与Magento中的目录价格规则关联的所有产品ID

时间:2012-02-26 15:25:04

标签: magento

我需要一种方法来检索与目录价格规则促销相关联的产品ID(例如,BICYCLES类别中所有项目的价格的50%)。我想这样做而不必遍历整个产品数据库。

我知道有一个名为getRuleProductIds($ruleID)的函数,它应该按规则ID返回一个产品ID数组,但我不知道在哪里使用它,哪个集合与它相关联等等。

我在products_in_promotion.phtml模板中有以下代码:

<?php 
  $rules = Mage::getModel('catalogrule/rule');
  $collection = $rules->getCollection();
  $sale_items = $collection->getRuleProductIds(1); # ??????? this throws an error
?>

$collection正确存储了所有目录价格规则的数组,但这与我能得到的一样接近。没有产品清单。

我在这里做错了什么想法?

3 个答案:

答案 0 :(得分:7)

您不必将其作为集合。只需加载您想要的规则,然后使用getMatchingProductIds(又名Mage_CatalogRule_Model_Rule)中的catalogrule/rule

$catalog_rule = Mage::getModel('catalogrule/rule')->load(1);  // Rule ID
$skus = $catalog_rule->getMatchingProductIds();

var_dump($skus);

HTH

答案 1 :(得分:1)

如果您的商店中有许多产品,那么这是有效的方式。您可以在class更具体的位置添加website_id,custom_group_id。

$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_read');
$tableName = $resource->getTableName('catalogrule_product');
$productIdList = $connection->fetchAll('SELECT product_id as product_id FROM '.$tableName.' WHERE rule_id = 1);

答案 2 :(得分:0)

在将网站过滤器添加到seanbreeden建议的代码后,它对我有用。

$catalog_rule = Mage::getModel('catalogrule/rule')->load(1);  // Rule ID
$catalog_rule->addWebsiteFilter('1');
$skus = $catalog_rule->getMatchingProductIds();

var_dump($skus);
相关问题