Skip to main content

I have created one module for discount code popup which havehas method to apply code without reloadreloading cart page

in your javascript method updateCoupon() you have call ajax method which callcalls controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action

I have created one module for discount code popup which have method to apply code without reload cart page

in your javascript method updateCoupon() you have call ajax method which call controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action

I have created one module for discount code popup which has method to apply code without reloading cart page

in your javascript method updateCoupon() you have call ajax method which calls controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action

Source Link
Vaibhav Ahalpara
  • 5.3k
  • 4
  • 43
  • 81

I have created one module for discount code popup which have method to apply code without reload cart page

Here is link for this module for reference

in your javascript method updateCoupon() you have call ajax method which call controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action

public function applyAction()
    {
        $response = array();
        $couponCode = (string) $this->getRequest()->getParam('coupon_code');
        if ($this->getRequest()->getParam('remove') == 1) {
            $couponCode = '';
        }
        $oldCouponCode = $this->_getQuote()->getCouponCode();
        if (!strlen($couponCode) && !strlen($oldCouponCode)) {
            $message = "Please enter valid code";
            $response['error'] = 1;
        }
        try {
            $codeLength = strlen($couponCode);
            $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;
            $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
            $this->_getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
                ->collectTotals()
                ->save();
            if ($codeLength) {
                if ($isCodeLengthValid && $couponCode == $this->_getQuote()->getCouponCode()) {
                        $message = $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
                    $this->_getSession()->setCartCouponCode($couponCode);
                } else {
                        $message = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
                        $response['error'] = 1;
                }
            } else {
                $message = $this->__('Coupon code was canceled.');
            }
        } catch (Mage_Core_Exception $e) {
            $message = $e->getMessage();
            $response['error'] = 1;
        } catch (Exception $e) {
            $response['error'] = 1;
            $message = $this->__('Cannot apply the coupon code.');
            Mage::logException($e);
        }
        $response['message'] = $message;
        $response['code'] = $couponCode;
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
    }