1

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.

2

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*/
2

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;
    }
}
  • Thanks for your answer where i want to post this code? – Ramesh S Aug 22 '17 at 11:13
  • I tried this code in app\code\core\Mage\Checkout\Model\Observer.php after salesQuoteSaveAfter($observer) function nothing happen – Ramesh S Aug 22 '17 at 14:04
  • 1
    Why are you adding the code in magento core . Create a new custom module and use your code over there . Can you put a log that your events are getting called or not ? – Rahul Aug 22 '17 at 14:49
2

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

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.