0

I need to enable cash on delivery option only for a particular state in a country.

I have tried to do it in "Payment methods" from magento dashboard and found only country-wise cash on delivery option.

Kindly some one guide me to achieve this without much of coding as I am not so good in magento 2.

1

2 Answers 2

0

First Override Magento\OfflinePayments\Model\Cashondelivery.php file using custom module and then put below code in file

public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
    {
   ...........Write your own logic here and reture true/false on basis of ................     
    return false;
    }
2
  • Thank you Nilesh for your quick response. But i am not so sure how to override the mentioned file using a custom module. Can you guide me???
    – Praveen
    Jan 25, 2018 at 12:06
  • using di.xml you can do this Jan 29, 2018 at 5:46
0

Plugin is a best way to override or change functionality of a function with out modify its class.

So you must create a module and plugin inside it.

You must define plugin for class Magento\OfflinePayments\Model\Cashondelivery and Inside plugin class write following:

it will attach you method after isAavailable() method.

public function afterIsAvailable(\Magento\Quote\Api\Data\CartInterface $quote, $result) {
    $billingaddr =$quote->getBillingAddress();
    if($billingaddr->getRegionId()!="yourvalue") {
        $result = false;
    }
    return $result; // if condition not match will return default result
}
2
  • Thank you Kumar for your quick response. I am not a techie guy to create the plugin. May be i would be if you can guide me how to create one. Please help.
    – Praveen
    Jan 25, 2018 at 12:07
  • I have updated my answer please check
    – Kumar M
    Jan 25, 2018 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.