如何使自定义运输方式的 Magento 2 免运费
How to make shipping cost free Magento 2 for a custom shipping method
我成功创建了我的自定义运输方式,使用自定义模块,运费为 5 美元。
例如,如果购物车总金额超过 30 美元,我想免费提供。
有没有办法做到这一点?
我不知道从哪里开始,所以我没有代码可以尝试,请帮忙。
谢谢!
您可以在管理员中设置购物车规则来执行此操作。
https://www.aitoc.com/blog/configuring-free-shipping-in-magento-2/
找到了,万一有人需要呢:
在我的 shipping.php 文件中有一个名为 collectRates 的函数,它负责返回运费,我添加了
if($request->getBaseSubtotalInclTax()>100){
$method->setPrice(0);
}
基本上如果总价超过100$我就免运费,最好把100$做成动态值,可以在运费设置里改,我稍后再做。
最后这是我的 collectRates 函数:
* @param RateRequest $request
* @return bool|Result
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateResultFactory->create();
/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
$method = $this->_rateMethodFactory->create();
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
$amount = $this->getShippingPrice();
$method->setPrice($amount);
if($request->getBaseSubtotalInclTax()>100){
$method->setPrice(0);
}
$method->setCost($amount);
$result->append($method);
return $result;
}
我成功创建了我的自定义运输方式,使用自定义模块,运费为 5 美元。
例如,如果购物车总金额超过 30 美元,我想免费提供。
有没有办法做到这一点?
我不知道从哪里开始,所以我没有代码可以尝试,请帮忙。
谢谢!
您可以在管理员中设置购物车规则来执行此操作。
https://www.aitoc.com/blog/configuring-free-shipping-in-magento-2/
找到了,万一有人需要呢:
在我的 shipping.php 文件中有一个名为 collectRates 的函数,它负责返回运费,我添加了
if($request->getBaseSubtotalInclTax()>100){
$method->setPrice(0);
}
基本上如果总价超过100$我就免运费,最好把100$做成动态值,可以在运费设置里改,我稍后再做。
最后这是我的 collectRates 函数:
* @param RateRequest $request
* @return bool|Result
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateResultFactory->create();
/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
$method = $this->_rateMethodFactory->create();
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
$amount = $this->getShippingPrice();
$method->setPrice($amount);
if($request->getBaseSubtotalInclTax()>100){
$method->setPrice(0);
}
$method->setCost($amount);
$result->append($method);
return $result;
}