浏览文章
文章信息
Magento2 以编程方式设置免邮包邮|Set freeshipping Programmly when add tocart
13450
<!--免邮--> <event name="sales_quote_collect_totals_before"> <observer name="Aiweline_Upsell::sales_quote_collect_totals_before" instance="Aiweline\Upsell\Observer\Quote\SalesQuoteCollectTotalsBefore" /> </event>代码:
<?php
/**
* @Author 秋枫雁飞
* @Email aiweline@qq.com/1714255949@qq.com
* @Desc 文件由Aiweline(秋枫雁飞)编写,若有升级需要
* 建议不要随意修改文件源码。
**/
namespace Aiweline\Upsell\Observer\Quote;
use Magento\Framework\Event\Observer;
class SalesQuoteCollectTotalsBefore implements \Magento\Framework\Event\ObserverInterface
{
/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
$quote = $observer->getEvent()->getData('quote');
if (!$quote) return;
$shippingAddress = $quote->getShippingAddress()->setFreeShipping(1);
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('freeshipping_freeshipping');
}
}