Pergunta

I want to set the shipping method to each cart to free shipping without choosing the shipping method, so when customer doing checkout, the shipping method will set by default to free shipping without choosing it. How can i do it?

Foi útil?

Solução

To auto select free shipping on checkout page, you can override a javascript from Magento_Checkout module. You can achieve this by two way


1] By creating your 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>

then create the requirejs to overriding the checkout-data-resolver js,

for this

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 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, change a condition inside the function resolveShippingRates

From:

............................

     if (ratesData.length == 1) {
                    //set shipping rate if we have only one available shipping rate
                    selectShippingMethodAction(ratesData[0]);

                    return;
                }

.............................

To

.................................




     var selectedShippingRate = checkoutData . getSelectedShippingRate();
        var availableRate = false;
        if (selectedShippingRate == null) { // custom code start here
            for (var i = 0;i < ratesData . length;i++){
                if (ratesData[i] . method_code == 'freeshipping') {
                    selectShippingMethodAction(ratesData[i]);
                    return;
                }
            }
            } else { // custom code end here
            if (ratesData . length == 1) {
                //set shipping rate if we have only one available shipping rate
                selectShippingMethodAction(ratesData[0]);
                return;
            }
        }

.....................................

Replace Namespace/Module/ with your YOUR_NAMESPACE and YOUR_MODULENAME

  1. Activate the module : php bin/magento module:enable Namespace_Module
  2. Run a setup upgrade : php bin/magento setup:upgrade
  3. Do a static deploy : php bin/magento setup:static-content:deploy

2] By using theme


Just copy checkout-data-resolver.js into your custom theme but make sure It path should be,

app\design\yourvendor\yourthemename\Magento_Checkout\view\frontend\web\js\model

After copying update checkout-data-resolver.js with resolveShippingRates function,

  resolveShippingRates: function (ratesData) {
               var selectedShippingRate = checkoutData . getSelectedShippingRate();
               var availableRate = false;

               if (selectedShippingRate == null) { // custom code start here
                    for (var i = 0;i < ratesData . length;i++){
                            if (ratesData[i] . method_code == 'freeshipping') {
                                selectShippingMethodAction(ratesData[i]);
                                return;
                            }
                        }
                    } else { // custom code end here
                            if (ratesData . length == 1) {
                                    //set shipping rate if we have only one available shipping rate
                                    selectShippingMethodAction(ratesData[0]);
                                    return;
                            }
                    }

                if (quote.shippingMethod()) {
                    availableRate = _.find(ratesData, function (rate) {
                        return rate.carrier_code == quote.shippingMethod().carrier_code &&
                            rate.method_code == quote.shippingMethod().method_code;
                    });
                }

                if (!availableRate && selectedShippingRate) {
                    availableRate = _.find(ratesData, function (rate) {
                        return rate.carrier_code + '_' + rate.method_code === selectedShippingRate;
                    });
                }

                if (!availableRate && window.checkoutConfig.selectedShippingMethod) {
                    availableRate = true;
                    selectShippingMethodAction(window.checkoutConfig.selectedShippingMethod);
                }

                //Unset selected shipping method if not available
                if (!availableRate) {
                    selectShippingMethodAction(null);
                } else {
                    selectShippingMethodAction(availableRate);
                }
            },

After overriding js file into your custom theme, execute below commands,

  1. php bin/magento cache:flush
  2. php bin/magento setup:static-content:deploy
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top