AnswerBun.com

Magento 2: How to Show method if not applicable Country Wise?

Magento Asked on January 6, 2022

I’m using DHL Shipping Method. I have set Show Method if Not Applicable to Yes.

Displayed Error Message will be displayed, when it’s applicable.

I would like to show this Error Message by Specific Country.

Example: If it’s Singapore then this message will be not displayed. For other countries, it will be displayed.

One Answer

Below is the solution

magentoappcodeCustomCheckoutviewfrontendrequirejs-config.js

var config = {
  "map": {
    "*": {
      "Magento_Checkout/js/model/shipping-service" : "Custom_Checkout/js/model/shipping-service"
    }
  }
};

magentoappcodeCustomCheckoutviewfrontendwebjsmodelshipping-service.js

/*global define*/
define(
        [
            'jquery', // For jQuery Added
            'Magento_Checkout/js/model/quote', // For Quote Added
            'ko',
            'Magento_Checkout/js/model/checkout-data-resolver'
        ],
        function ($, quote, ko, checkoutDataResolver) {
            "use strict";
            var shippingRates = ko.observableArray([]);
            return {
                isLoading: ko.observable(false),
                /**
                 * Set shipping rates
                 *
                 * @param ratesData
                 */
                setShippingRates: function (ratesData) {

                   // START TO REMOVE ERROR MESSAGE FOR DHL IF SG
                    var address = quote.shippingAddress();

                    $.each(ratesData, function (k, v) {
                        if(v.method_code == null && v.carrier_code == 'dhl' && v.error_message != '' && address.countryId == 'SG'){
                            ratesData.splice(k , 1);
                        }
                    });
                    // STOP TO REMOVE ERROR MESSAGE FOR DHL IF SG

                    shippingRates(ratesData);
                    shippingRates.valueHasMutated();
                    checkoutDataResolver.resolveShippingRates(ratesData);
                },

                /**
                 * Get shipping rates
                 *
                 * @returns {*}
                 */
                getShippingRates: function () {
                    return shippingRates;
                }
            };
        }
);

If someone has solution on PHP side then it would be great.

Answered by Jackson on January 6, 2022

Add your own answers!

Related Questions

Magento bms/erp migration from 1.9.3.2 to 2.3.4

0  Asked on November 18, 2020 by sahil-khanna

 

Magento2 Page jumps to top when click Menu Icon on mobile

1  Asked on November 13, 2020 by jaydip-patel

     

WYSIWYG media directory contents removed

1  Asked on November 12, 2020 by tyler-v

       

Magento 2 hacked with script in hacked

4  Asked on October 25, 2020 by casey

 

Magento 2 How to check Price Type?

1  Asked on October 21, 2020 by jackson

   

Magento 2 : How to insert Price inclusive Tax?

3  Asked on October 11, 2020 by anastasia-di-paulo

   

How to create and use REST API for mobile app?

1  Asked on September 23, 2020 by gowri-shankar

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP