2

In Magento 2 I try to track shipping method change in JS.

For exemple when pickup relay is selected I need to display a specific message on frontend.

Do you how can I do this ?

1 Answer 1

4

1. Register mixin

app/code/Acme/StackExchange/view/frontend/requirejs-config.js

let config = {
    config: {
        mixins: {
            'Magento_Checkout/js/action/select-shipping-method': {
                'Acme_StackExchange/js/action/select-shipping-method-mixin': true
            }
        }
    }
};

2. Create mixin

app/code/Acme/StackExchange/view/frontend/web/js/action/select-shipping-method-mixin.js

define([
    'mage/utils/wrapper'
], function (wrapper) {
    'use strict';

    return function (selectShippingMethod) {
        return wrapper.wrap(selectShippingMethod, function (_super, shippingMethod) {
            if (shippingMethod) {
                // your logic before shippingMethod set to quote
            }
            _super(shippingMethod);
            if (shippingMethod) {
                // your logic after shippingMethod set to quote
            }
        });
    };
});

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.