1

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

1 Answer 1

1

No-code solution: You can create a cart rule which grants free shipping if a certain payment method is used. I just validated this on a vanilla install with sample data:

Conditions section: enter image description here

Actions section: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.