0

I need to add free shipping method fro particular quote in an observer. I am calling an event sales_quote_collect_totals_before.

And my observer code is -

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SalesQuoteCollectTotalsBefore implements ObserverInterface
{
    public function execute(EventObserver $observer)
    { 
        $quote = $observer->getEvent()->getQuote();
        $address = $quote->getShippingAddress();
        $address->setFreeShipping(true);   
    }
}
0

Use this code in your observer :

$quote = $observer->getEvent()->getQuote();
$quote->getShippingAddress()->setFreeShipping(true);
$quote->save();

It may be helpful for you.

1
  • No, It is not working. Right now, the shipping methods which are available are listed on checkout page. I don't want to give user an option and set shipping method free on particular condition. Please do suggest as I am new in magento. Aug 3 '18 at 7:50
0

use below

    $quote = $observer->getEvent()->getQuote();
    $shippingMethod= 'freeshipping_freeshipping';
    $shippingAddress = $quote->getShippingAddress();
    $shippingAddress->setCollectShippingRates(true)
                    ->collectShippingRates()
                    ->setShippingMethod($shippingMethod)
    $shippingAddress->setCollectShippingRates(true)->collectShippingRates();
    $quote->collectTotals();
    $quote->save();

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.