3

Is it possible to find out all coupon codes was applied for an specific order?

i can only found out 1 coupon codes was applied for an order in sales_flat_order table. In the table, only 1 field was storing a coupon code (last code applied).

anyone know how to do that?

  • In default magento functionality is applied only one coupon code in order at time. Have you add any custom code to apply multiple coupon code to order? – Abdul Jun 16 '16 at 6:12
  • no, but if i apply a rule which is "Stop Further Rules Processing = false", so i can apply two rule one by one – hkguile Jun 16 '16 at 6:16
4

Try this code

<?php
$order_id = 101;
$items = Mage::getModel('sales/order_item')->getCollection()
    ->addFilter('order_id',array('eq'=>$order_id));

foreach($items as $item){
    if($item->getAppliedRuleIds() == '') {
        continue;
    }
    foreach(explode(",",$item->getAppliedRuleIds()) as $ruleId){       
         //Load the rule object
        $rule = Mage::getModel('salesrule/rule')->load($ruleId); //Shopping Cart Rules //$rule = Mage::getModel('catalogrule/rule')->load($ruleId); catalog rules
        // Throw out some information like the rule name what product it was applied to
        echo "<p> Applied Rule Name: ".$rule->getName()." & Code : ".$rule->getCouponCode()."</p>";
        echo "\t";
    }
}

?>

OR

<?php
$order_id = 11060;
$order = Mage::getModel('sales/order')->load($order_id);

foreach(explode(",",$order->getAppliedRuleIds()) as $ruleId){       
 //Load the rule object
 $rule = Mage::getModel('salesrule/rule')->load($ruleId); //Shopping Cart Rules //$rule = Mage::getModel('catalogrule/rule')->load($ruleId); catalog rules
 // Throw out some information like the rule name what product it was applied to
 echo "<p> Applied Rule Name: ".$rule->getName()." & Code : ".$rule->getCouponCode()."</p>";
 echo "\t";
}    

?>
  • This might work and might not work. If someone placed an order last week using a coupon code and in the mean time you change the coupon code or deleted the discount rule you won't get the coupon code value. – Marius Jun 16 '16 at 6:44
  • i have tried, the name and code didn't print out – hkguile Jun 16 '16 at 6:44
  • updated ans pls check now – Abdul Jun 16 '16 at 6:46
  • both solution i have tried for an order which has applied two rules (one free shipping, one discount off), but only print out discount off rule. I the customer apply the rules one by one, seems there is no way to find out – hkguile Jun 16 '16 at 6:59
  • If your print_r($order->getAppliedRuleIds()); then how may ids found? – Abdul Jun 16 '16 at 7:25
2

You find only one code, because you can only have one code applied to the order.
You can have multiple discount rules but only one of them can have a code.

You can get the coupon code applied like this:

$order->getCouponCode();

where $order is an instance of the order model.

and you can get the ids of all the rules that are applied to the order, like this:

$order->getAppliedRuleIds();
  • there are no ways to find out which rules were applied? – hkguile Jun 16 '16 at 6:56
  • based on the rule ids, you can retrieve the rules from the database but that's not always going to work if you delete the rules or change them over time. – Marius Jun 16 '16 at 7:03

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.