Question

I would like to remove cash on delivery payment method when coupon code is active.

Am crating a TEST50 coupon code in Shopping Cart Price Rule for discount 50%.

When TEST50 is active cash on delivery remove in checkout page. If TEST50 is non-active show cash on delivery.

Was it helpful?

Solution

I got the solution go to /app/code/local/Mage/Payment/Model/Method/Cashondelivery.php

After this line

public function getInstructions()
{
    return trim($this->getConfigData('instructions'));
}

Add new function

/* code added for cash on delivery hide for when coupon active*/
    public function isAvailable($quote = null)
    {
        if ($quote) 
        {
            $coupon = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();
            if($coupon ==true) //or use your coupon code here
            {
                $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();

                if (!in_array($coupon)) {
                    return false;
                }
            }
        }

        return parent::isAvailable($quote);
    }
   /* code added for cash on delivery hide for when coupon active*/

OTHER TIPS

Better than overriding the Model Class , we can use the Event and Observer for this purpose.

We can use payment_method_is_active

public function HideCODforCouponCode($observer) {
    $event           = $observer->getEvent();
    $methodInstance = $event->getMethodInstance();
    $store_id = Mage::app()->getStore()->getStoreId();     
     $result          = $event->getResult();  
    $cashOnDeliveryenable = Mage::getStoreConfig('payment/phoenix_cashondelivery/active' , $store_id);
    $coupon = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();

    if (($methodInstance->getCode()  != 'phoenix_cashondelivery')   || ($cashOnDeliveryenable == 0)) {
        return $this; //if it's not the payment method your a looking for do nothing
    }    

    if ($coupon == "TEST50") && ($methodInstance->getCode()  == 'phoenix_cashondelivery')) {
        $result->isAvailable = false;
    }
}

Inside your custom module's config.xml under add the following code.

<events>
<payment_method_is_active>
              <observers>
                  <paymentfilter_payment_method_is_active>
                      <type>singleton</type>
                      <class>overrides/observer</class>
                      <method>HideCODforCouponCode</method>
                  </paymentfilter_payment_method_is_active>
              </observers>
        </payment_method_is_active>
      </events>

Write the shared function inside Observer.php of your custom module . For further info , Please check url https://stackoverflow.com/questions/11195682/implementing-an-event-observer-in-magento

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