Magento 2 – Update Shipping Rates on Changing Postcode at Checkout

checkoutmagento2magento2.2shipping-methods

I'm trying to create a module with custom shipping method. So I've followed this tutorial to create it. This link and this link, to configure shipping carrier validations.

The problem is, if I change the zip/postcode, the shipping's value does not change. My question is, how can I update the shipping cost value when customer update its address information (postcode to be more specific)? Or when we use the guest-checkout and there is no address information?

Thanks in advance.

Best Answer

I had the same problem. The problem is on your xml definition of the rates validation:

<item name="shipping-rates-validation" xsi:type="array">
      <item name="children" xsi:type="array">
            <item name="inchoo" xsi:type="array">
                <item name="component" xsi:type="string">Inchoo_Shipping/js/view/inchoo</item>
            </item>
      </item>
</item>

In your xml, when you define the js validator:

<item name="inchoo" xsi:type="array">

You must change the name to:

<item name="inchoo-rates-validation" xsi:type="array">

And inchoo must be the same as carrier code of your shipping method. The problem is when magento try lo find and load all validators in :

...vendor/magento/module-checkout/Block/Checkout/LayoutProcessor.php

on method processShippingChildrenComponents

private function processShippingChildrenComponents($shippingRatesLayout)
{
    $activeCarriers = $this->getShippingConfig()->getActiveCarriers(
        $this->getStoreResolver()->getCurrentStoreId()
    );
    foreach (array_keys($shippingRatesLayout) as $carrierName) {
        $carrierKey = str_replace('-rates-validation', '', $carrierName);
        if (!array_key_exists($carrierKey, $activeCarriers)) {
            unset($shippingRatesLayout[$carrierName]);
        }
    }
    return $shippingRatesLayout;
}

If magento does not find the same name as carrier, unset the file in layout.

Regards

Mauro M. Martinez