I need to figure out a method to check whether a coupon code is valid or not and add this method to a boolean. I have a few cases as to why:

1.controller is offered a valid coupon code

2.controller is offered an invalid coupon code

3.controller is offered no coupon code and this will generate coupon code removal functionality.

    coupon is added to order, ajax response (1) is prepared
    ajax response (2) is prepared
    coupon is removed from order, ajax response (3) is prepared

TLDR: How do I check whether a coupon code is valid or not?

1 Answer 1


On custom controller add an action which is check current coupen is validate and and apply.

public function couponPostAction()
if (!Mage::getSingleton('checkout/cart')->getQuote()->getItemsCount()) {
    $result['message']='YOUR ERROR Message';


$couponCode = (string) $this->getRequest()->getParam('coupon_code');
if ($this->getRequest()->getParam('remove') == 1) {
    $couponCode = '';
$oldCouponCode = Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode();

if (!strlen($couponCode) && !strlen($oldCouponCode)) {


try {
    $codeLength = strlen($couponCode);
    $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;

    Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')

    if ($codeLength) {
        if ($isCodeLengthValid && $couponCode == Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode()) {

        $result['message']=$this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
        } else {
        $result['message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));

    } else {

    $result['message']=$this->__('Coupon code was canceled.');


} catch (Mage_Core_Exception $e) {
        $result['message']=$this->__('Cannot apply the coupon code');

} catch (Exception $e) {
        $result['message']=$this->__('Cannot apply the coupon code.');



    $this->getResponse()->setHeader('Content-type', 'application/json');

In my code i have created action couponPostAction and this function is return jSON data(message, valid). and you you have remove coupon then send an extra params remove $this->getRequest()->getParam('remove') which coupon from current cart


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.