2

I'm looking for a way to get the name of a salesrule that is applied to a product, go show in the frontend.

I've been looking in the SalesRule, Rule.php but with no real luck.

Mage::getModel('salesrule/rule')->getName()

This is what I've come to, but I'm not sure if it's correct. Can it even be done?

4

You need to load the rule first, then you can get the details. Any product get a value assigned 'applied_rule_ids' which is a comma separated list of all rules applied to the product.

Note that this is an example. Best practice is not to place this code in the display layer (.phtml) files. To do it properly you need to extend the required renderer class for cart items, and add a new method - something like getAppliedRulesNames() which you can then echo out at the right place.

Example code at the top of design/frontend/base/default/template/checkout/cart/item/default.phtml

$_item = $this->getItem();
if($_item->getAppliedRuleIds()) {
    $appliedRulesArray = explode(',',$_item->getAppliedRuleIds());
    foreach($appliedRulesArray as $ruleId){
        $rule = mage::getModel('salesrule/rule')->load($ruleId);
        $name = $rule->getName();
    }

}

Another way to do it would be to create a helper function in your module, to which you can pass the product (either id, or object) which will then return the concatenated list of rule names. This way you can use the helper method anywhere else to echo out the result (not just in the cart)

something like:

echo mage::Helper('mymodulehelper')->getRulesNames($product);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.