How to set default shipping method in case of multiple shipping methods in magento 2?
As i understood from your question is that you want to always have a shipping method selected when someone comes on checkout page.
To achieve this, we need to override a javascript from Magento_Checkout module.
First things first, we need to create a module :
Namespace/Module/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Namespace_Module',
__DIR__
);
Namespace/Module/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Namespace_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
After this, we need to create the requirejs for overriding that js.
Namespace/Module/view/frontend/requirejs-config.js
var config = {
map: {
'*': {
'Magento_Checkout/js/model/checkout-data-resolver': 'Namespace_Module/js/model/checkout-data-resolver'
}
}
};
Now we have to copy checkout-data-resolver.js
from Magento_Checkout/view/frontend/web/js/model
in our module with the same path Namespace_Module/view/frontend/web/js/model
After that, we need to change a condition inside the function resolveShippingRates: function (ratesData)
From:
...
if (ratesData.length == 1) {
//set shipping rate if we have only one available shipping rate
selectShippingMethodAction(ratesData[0]);
return;
}
...
To:
...
if (ratesData.length >= 1) {
//set shipping rate if we have only one available shipping rate
selectShippingMethodAction(ratesData[0]);
return;
}
...
And thats it, all you have to do now is:
- Activate the module : php bin/magento module:enable Namespace_Module
- Run a setup upgrade : php bin/magento setup:upgrade
- Do a static deploy : php bin/magento setup:static-content:deploy
if (ratesData.length >= 1) {
//set shipping rate if we have only one available shipping rate
selectShippingMethodAction(ratesData[0]);
return;
}
This will make it impossible to select any other shipping option. This solves the issue for me:
if (ratesData.length == 1) {
//set shipping rate if we have only one available shipping rate;
selectShippingMethodAction(ratesData[0]);
return;
}
if (ratesData.length >= 1) {
//set shipping rate if we have only one available shipping rate
if(!selectedShippingRate) {
selectShippingMethodAction(ratesData[0]);
}
}
There is no need for entire module to achieve this change. You can simple extend the Checkout module and override in in your theme.
Copy the js file checkout-data-resolver.js
from
vendor\magento\module-checkout\view\frontend\web\js\model
to
app\design\frontend\Namespace\ThemeName\Magento_Checkout\web\js\model.Locate line with following code:
if (ratesData.length == 1) {
and replace it with:
if (ratesData.length >= 1 && !selectedShippingRate) {