Search code examples
magentomagento-1.9

Coupon is always re-added during checkout


I encountered the following bug

  1. Add a product to cart
  2. Apply a coupon code
  3. Remove coupon code
  4. Coupon code is not shown anymore / remove successful
  5. Go in checkout
  6. Continue billing and shipping step
  7. Go back in cart
  8. Coupon code of step #2 is active again

Solution

  • It seems to be a bug in CE 1.9

    There is a new session value set in Mage_Checkout_CartController::couponPostAction()

    $this->_getSession()->setCartCouponCode($couponCode);
    

    which restores the coupon code in Mage_Checkout_Model_Type_Onepage::_setCartCouponCode()

    protected function _setCartCouponCode()
    {
        if ($couponCode = $this->getCheckout()->getCartCouponCode()) {
            $this->getQuote()->setCouponCode($couponCode);
        }
        return $this;
    }
    

    I solved it by event/observer

        <events>
            <controller_action_predispatch_checkout_cart_couponPost>
                <observers>
                    <remove_session_coupon_code>
                        <type>singleton</type>
                        <class>yourmodule/observer</class>
                        <method>removeCoupon</method>
                    </remove_session_coupon_code>
                </observers>
            </controller_action_predispatch_checkout_cart_couponPost>
        </events>
    

    and:

    public function removeCoupon(Varien_Event_Observer $observer)
    {
        $controller = $observer->getControllerAction();
        if ($controller->getRequest()->getParam('remove') == 1) {
            Mage::getSingleton("checkout/session")->unsetData('cart_coupon_code');
        }
        return $this;
    }