Magento 2 – Apply Free Shipping Coupon Code for Specific Shipping Method

coupon-codesmagento2

in project i have two different custom shipping method one for 8 to 15 days delivery and 2 to 4 days delivery.
i have create coupon code for free shipping but i want to allow free shipping coupon code to only 8 to 15 days shipping method.
please help.

Best Answer

You can change code in your carrier model file collectRates function check coupon code is applied or not from calling quote object and set custom price if your 8 to 15 days delivery method available and set 0 price for free shipping .

you can get a coupon code from quote object from here

$quote  = $this->session->getQuote();

$address = $quote->getShippingAddress();

$shippingaddress    = $quote->getShippingAddress()->getData();

$coupon     = $quote->getCouponCode();

Before apply this code you have to declare session in construct method like this

protected $session;

public function __construct(

\Magento\Checkout\Model\Session $session, ) {   

$this->session = $session;

}

after then you get quote data from session in magento 2.

Related Topic