3

I need to programitcally re order an Item.

I have achieved this by

  1. Cancel the current order
  2. Get the cancelled order quote
  3. Ammend the Qtys
  4. Recalulate the shipping

This is all fine... now I need to calculate if any coupons were applied are now still valid for the new order? Forinstance a rule would be that a coupon is only valid if an order is over £10.00 - I re order from the old quote but now my order is only £9.00 which means the coupon is not valid.

How can I check whether the coupon that is assigned to the quote is now valid for the new order totals?

Thanks in advance..

4

The following answer is just theoretical. I haven't tested it, but it seams the way to go.
You can get the cart rules that were applied to the order you cancel like this:

$ruleIds = explode(',', $order->getAppliedRuleIds());

Then you need to get the rules applied:

$rules = Mage::getModel('salesrule/rule')->getCollection()
       ->addFieldToFilter('rule_id', array('in'=>$ruleIds));

Then loop through the rules and try to apply the coupons to the quote.

foreach ($rules as $rule){
    if (!$rule->getCode()) {//skip rules without codes
        continue;
    }
    //normaly there should be only one rule that gets here since you can apply only one coupon code
    $quote->setCouponCode($rule->getCode())->collectTotals()->save(); 
    $quoteCouponCode = $quote->getCouponCode();//get the applied coupon code from the quote
    if ($quoteCouponCode){
        //$rule is valid and $rule->getCode() is a valid coupon code
    }
    //remove the coupon code from the quote if you want
    $quote->setCouponCode('')->collectTotals()->save(); 
}

There is a chance that even for a valid coupon you will get the 'not valid' result. This may happen if you just reached the coupon usage limit (per customer or total) or if you reorder after the expiration date of the rule.

  • Could you expand on //$rule is valid and $rule->getCode() is a valid coupon code, do I need to put it through a validator? – Jonathan Tizard Jun 21 '13 at 10:21
  • It depends on what you want to do after you find the valid coupon. If you just want the coupon to be applied and place the order, that if statement is useless. Just remove it, and remove the line that removes the coupon code and go on placing the order, but place the order outside the foreach loop. – Marius Jun 21 '13 at 10:34
  • At this point on the quote when I retrieve it - the coupon is already applied. What I need to know is what method do I use to check whether that coupon is valid? At that moment my order has a coupon on it, and for some reason when I collect totals - it applies the coupon even though it doesn't pass the rules (e.g order total is blow correct amount) for it to be applied. I was wondering I can run somekind of validation manually and remove the coupon myself if its not valid. But I dont know what model/method etc to use to check if that coupon passes all rules and validation. Sorry to confuse! – Jonathan Tizard Jun 21 '13 at 11:02
  • Maybe you should change step 2 of your process 'Get the cancelled order quote'. Try to transform the canceled order to a new quote instead of working with the quote from the canceled order using Mage_Sales_Model_Convert_Order::toQuote() (Just an idea) – Marius Jun 21 '13 at 11:12
  • Will have a bash at that, thanks for your continued support :-) – Jonathan Tizard Jun 24 '13 at 10:47

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.