0

I use magento 2.4.6. I have a custom code that allows the free shipping method only for peculiar products so, if one of these products are in the cart, the free shipping is visible, otherwise it's not.

In order to achieve this I created a plugin beforeAppend on magento class \Magento\Shipping\Model\Rate\Result. The code works:

  • I add the product to the cart from the product page, I go to checkout, I see the shipping method
  • I put another product (which does not have the free shipping option) in the cart and when I go to checkout, I no longer see the shipping method.
  • If I remove the second product from the cart and then I go to checkout, I see the free shipping again

My problem is when I remove the valid product from the minicart. When I do, the checkout is still showing the free shipping and I have to reload the page in order for the shipping method to disappear.

Is there a way to reload the checkout shipping method data when the product is removed from minicart without the need of manually reloading the checkout page?

1
  • I think your best approach here would be removing the delete action in minicart (or maybe all minicart content) so users may edit / delete products in checkout/cart page instead of minicart Commented Apr 18 at 14:05

1 Answer 1

0

@Lou Nik

Hey,

You can try below code into js file where add/remove item events called.

define(
    [
        'Magento_Checkout/js/model/quote',
        'Magento_Checkout/js/model/shipping-rate-processor/new-address',
        'Magento_Checkout/js/model/shipping-rate-processor/customer-address',
        'Magento_Checkout/js/model/shipping-rate-registry'

    ],
    function (quote, newAddress, customerAddress, rateRegistry) {
       'use strict';

       var processors = [];
       rateRegistry.set(quote.shippingAddress().getCacheKey(), null);

       processors.default = newAddress;
       processors['customer-address'] = customerAddress;

       var addressType = quote.shippingAddress().getType();

       if (processors[addressType]) {
          processors[addressType].getRates(quote.shippingAddress());
       } else {
          processors.default.getRates(quote.shippingAddress());
       }
    }
);

OR

quote is object of  'Magento_Checkout/js/model/quote'
rateRegistry is object of 'Magento_Checkout/js/model/shipping-rate-registry'

var address = quote.shippingAddress();
address.trigger_reload = new Date().getTime();
rateRegistry.set(address.getKey(), null);
rateRegistry.set(address.getCacheKey(), null);

Thank You!

Your Answer

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

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