Magento2.2.7
.
Want to auto select first shipping method while multiple shipping there or auto select while single method there on checkout page.
Already try this solution but not working for me.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityMagento2.2.7
.
Want to auto select first shipping method while multiple shipping there or auto select while single method there on checkout page.
Already try this solution but not working for me.
You need to override core file in your theme
vendor/magento/module-checkout/view/frontend/web/js/model/checkout-data-resolver.js
Override file in your theme : sendorname/theme/Magento_Chekout/web/js/model/checkout-data-resolver.js
and check line number 151 code
if (ratesData.length === 1) {
//set shipping rate if we have only one available shipping rate
selectShippingMethodAction(ratesData[0]);
return;
}
You can see here this code check that if only one shipping method thaen automatic got selected.So you need to changed the conditon here that if more than one shipping method then first should be selected with below code
if (ratesData.length >= 1) {
//set shipping rate if we have only one available shipping rate
selectShippingMethodAction(ratesData[0]);
return;
}
Now Cache clear and go to checkout page ,you will see first shipping method will automatic got selected.
Note: If you are using any third party one step checkout module then you need to search same file to override in your theme.
You can use a ready made module, It is free and from BSS commerce.
https://bsscommerce.com/magento-2-preselect-shipping-payment-extension.html
Note: You can select shipping of your choice Or first one
I used above module in some of my project.
create {Vendor}/{Module}/view/frontend/requirejs-config.js file
var config = {
config: {
mixins: {
'Magento_Checkout/js/model/checkout-data-resolver': {
'Vendor_Module/js/model/checkout-data-resolver': true
},
}
}
}
then create Vendor/Module/view/frontend/web/js/model/checkout-data-resolver.js file
define([
'underscore',
'mage/utils/wrapper',
'Magento_Checkout/js/checkout-data',
'Magento_Checkout/js/action/select-shipping-method',
'Magento_Checkout/js/action/select-payment-method'
],function (_, wrapper, checkoutData, selectShippingMethodAction) {
'use strict';
return function (checkoutDataResolver) {
var resolveShippingRates = wrapper.wrap(
checkoutDataResolver.resolveShippingRates,
function (originalResolveShippingRates, ratesData) {
// if selected shipping method is null then set default shipping method using the ratesData and selectShippingMethodAction in case of multiple shipping method
if (!checkoutData.getSelectedShippingRate() && ratesData.length > 1) {
selectShippingMethodAction(ratesData[0]);
}
return originalResolveShippingRates(ratesData);
}
);
return _.extend(checkoutDataResolver, {
resolveShippingRates: resolveShippingRates
});
};
});
Alternatively, you can download the module here which allows you to select a default shipping and payment method : Default Shipping & Payment