Do you wish to enable the Magento 2 auto-select shipping method?
In Magento 2, it is possible to configure multiple shipping methods. On the checkout page, the customers can select their preferred shipping method and proceed further. In this stage, the customers may re-think about purchasing the product if the selected shipping method is too costly. This may also add friction along the customer’s path to conversion.
Pre-selecting the most convenient shipping method on the checkout page can make things better. Recently, I came across such a requirement of one of our clients. The client wanted to pre-select a specific payment method in Magento 2 for orders exceeding $100 order total.
In this tutorial, I am going to show you how to auto-select shipping method in Magento 2.
Method to Auto Select Shipping Method in Magento 2
In order to auto select shipping method in Magento 2, we will use the selectShippingMethodAction()
function.
You can follow the steps below to auto-select shipping methods in Magento 2:
- Step 1: Copy the vendor\magento\module-checkout\view\frontend\web\js\model\checkout-data-resolver.js file and paste it at app\design\frontend\Themes\Yourtheme\Magento_Chekout\web\js\model\checkout-data-resolver.js.
- Step 2: Now, open the file and search for the following code:
if (ratesData.length === 1) { //set shipping rate if we have only one available shipping rate selectShippingMethodAction(ratesData[0]); return; }
and replace it with:
if(quote.totals()['grand_total'] > 100){ selectShippingMethodAction(ratesData[1]); return; }
- Step 3: Save the file and you’re done!
You can alter the above code as per your condition and the shipping method.
Ending Words
Customer experience is a crucial aspect of online stores. Enhancing every part of the customer’s journey can help you achieve an optimum conversion rate. The above method enables you to auto select shipping method in Magento 2 and improves the customer experience by pre-selecting the most convenient shipping method.
This way, you can smoothen the customer’s journey and enhance conversion rate.
If you are still facing any issues or have any queries, feel free to comment. I’d be happy to help you!
Was this solution helpful? Let us know by rating this post.
Also, do not forget to share this guide with your friends via social media.