0

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.

4
  • please check bsscommerce.com/…. it is free and I used it as well!
    – Pawan
    Feb 27, 2020 at 12:10
  • @Pawan Thank you. But Want to auto select first shipping method while multiple shipping there
    – Shorabh
    Feb 27, 2020 at 12:19
  • there is option to select first one, please see screen shot of module.
    – Pawan
    Feb 27, 2020 at 12:19
  • @Shorabh,I am sure you were using any third party module if solution is not working as per your link.Please check my answer and let me know.
    – akgola
    Feb 27, 2020 at 13:45

3 Answers 3

6

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.

1
  • Is this also the file to edit when you want this behaviour on the CART page (NOT Checkout page)?
    – TheFrakes
    Nov 17, 2021 at 15:10
2

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.

1

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
        });
    };
});
  • Run a setup upgrade : php bin/magento setup:upgrade
  • Do a static deploy : php bin/magento setup:static-content:deploy

Alternatively, you can download the module here which allows you to select a default shipping and payment method : Default Shipping & Payment

Your Answer

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

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