hackthisjay
hackthisjay

Reputation: 177

Magento get salesrule id from product_id (or entity_id)

We are using Magento Enterprise 1.10 and I need a way to get the salesrule_id for each product on category pages as well as product detailed pages. This is pretty easy when you're using the cart.

Basically all I want to do is something like this --> Pseudo query

SELECT rule_id FROM salesrule WHERE entity_id = <entity_id)

Do you know of any way to do this outside of the cart. Is there a method I can call with a product id that return's a rule_id if it exists for this product?

Any suggestion would greatly help me.

Upvotes: 0

Views: 2494

Answers (2)

Dmytro Zavalkin
Dmytro Zavalkin

Reputation: 5277

Try to use Mage_CatalogRule_Model_Resource_Rule::getRulesFromProduct().

Upvotes: 2

Ashley Swatton
Ashley Swatton

Reputation: 2125

A product does not have sales rules attached to it and there is no inbuilt method to load rules for a given product. On the cart page it is easy because the rule gets applied to the quote and on each item you can get applied_rule_ids which gives you the ids of any matched rules currently applied to the quote item which is not the same as a product object. So for example, you could have multiple sales rules which apply to the same item so applied_sales_rule_ids would give you all the applied rules not just one.

You could try and load a collection of sales rules and add a filter on the product_ids column and then pass in the entity id of the product. This post here explores this with some code examples and may be of some help.

Upvotes: 2

Related Questions