Question

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.

If(valid){
    coupon is added to order, ajax response (1) is prepared
}
elseif(!valid){
    ajax response (2) is prepared
}
else{
    coupon is removed from order, ajax response (3) is prepared
}

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

Was it helpful?

Solution

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

public function couponPostAction()
{
 $result=array();
if (!Mage::getSingleton('checkout/cart')->getQuote()->getItemsCount()) {
    $result['valid']=0;
    $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()->getShippingAddress()->setCollectShippingRates(true);
    Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
        ->collectTotals()
        ->save();

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

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

        }
    } else {

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

    }

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

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

    Mage::logException($e);

}

    $this->getResponse()->setHeader('Content-type', 'application/json');
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top