I would like to update my shipping cost to zero based on a certain payment method selected in the checkout.
My approach is to create a before plugin for this method \Magento\Checkout\Model\TotalsInformationManagement::calculate
, which content is :
public function calculate(
$cartId,
\Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation
) {
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->cartRepository->get($cartId);
$this->validateQuote($quote);
if ($quote->getIsVirtual()) {
$quote->setBillingAddress($addressInformation->getAddress());
} else {
$quote->setShippingAddress($addressInformation->getAddress());
$quote->getShippingAddress()->setCollectShippingRates(true)->setShippingMethod(
$addressInformation->getShippingCarrierCode() . '_' . $addressInformation->getShippingMethodCode()
);
}
$quote->collectTotals();
return $this->cartTotalRepository->get($cartId);
}
and this is plugin:
public function beforeCalculate(
TotalsInformationManagement $subject,
$cartId,
TotalsInformationInterface $addressInformation
) {
...
/** @var Magento\Quote\Api\CartRepositoryInterface $quote */
$quote = $this->cartRepository->get($cartId);
$quote->getShippingAddress()
->setShippingAmount(0);
$quote->getShippingAddress()
->setBaseShippingAmount(0);
$this->cartRepository->save($quote);
}
But this doesn't work or neither is the right approach. Do you have any idea how can I achieve this ?
Thanks