Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve all product IDs associated with a Catalog Price Rule in Magento

Tags:

magento

I need a way to retrieve product IDs associated with a Catalog Price Rule promotion (ex. 50% the price of all items in the BICYCLES category). I'd like to do this without having to iterate through the entire product database.

I know there is a function called getRuleProductIds($ruleID), which should return an array of product IDs by rule ID, but I have no idea where to use it, which collection to associate it with, etc.

I have the following code in a products_in_promotion.phtml template:

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

$collection properly stores an array of all the Catalog Price rules, but that's as close as I can get. No product list is in sight.

Any ideas what I'm doing wrong here?

like image 487
20goto10 Avatar asked Feb 26 '12 15:02

20goto10


1 Answers

You don't have to get it as a collection. Just load the rule that you want and use getMatchingProductIds as found in Mage_CatalogRule_Model_Rule (aka catalogrule/rule).

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

var_dump($skus);

hth

like image 137
seanbreeden Avatar answered Sep 27 '22 19:09

seanbreeden