Reputation: 71
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?
Upvotes: 5
Views: 5834
Reputation: 11
If you have many products in your store then this is the efficient way. You may add website_id, custom_group_id in where class to be more specific.
$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);
Upvotes: 1
Reputation: 7788
It worked for me after adding website filter to the code suggested by seanbreeden.
$catalog_rule = Mage::getModel('catalogrule/rule')->load(1); // Rule ID
$catalog_rule->addWebsiteFilter('1');
$skus = $catalog_rule->getMatchingProductIds();
var_dump($skus);
Upvotes: 0
Reputation: 6097
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
Upvotes: 7