I've created a program to automatically create coupon codes based on certain criteria for a user, the code generation works fine, and I can test that by sending the coupon code in through the URL(like this ?code=34o77134iy3). Copying and pasting that code and then pressing "apply discount"(34o77134iy3 in this case) applies the code correctly, but the code below does not apply it automatically.
$coupon = "34o77134iy3";
Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon)->collectTotals()->save();
Mage::getSingleton("checkout/session")->setCartCouponCode($coupon);
Edit: Upon further investigation, the ->collectTotals()
line is what is resetting it to nothing.
Dumping ->getCouponCode()
gives the coupon code until that is run.
I can also use the post coupon code to apply it but for some reason, I can't use any other method.