In my webshop I have two shipping methods, free shipping (for orders above 200 euro) and flat rate shipping (5 euro). I do not have other shipping methods for other countries or something.
I want to show the flat rate shipping if the order is under 200, else the free shipping method. I want to show this in the cart (not the estimate function, but as soon as the user goes to the cart it will see the appropriate shipping).
I created a plugin for the file: vendor/magento/module-tax/Block/Checkout/Shipping.php. This class has a function called displayShipping
. This is called from the cart page to check if shipping should be displayed.
I created a before method for this function, so before Magento does the check it executes my function:
$shippingAddress = $this->getQuote()->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
$this
->getQuote()
->getShippingAddress()
->setShippingMethod('flatrate_flatrate')
->setCountryId('NL')
->save();
}
Now, shipping cost does show up in the cart, but the shipping amount is set to 0 instead of the 5 euro.
How can I fix this?