Paazl / magento2-checkout-widget

6 stars 15 forks source link

Wrong shipping mixin extension_attributes retrieval #69

Closed LexKoomen closed 3 years ago

LexKoomen commented 3 years ago

In https://github.com/Paazl/magento2-checkout-widget/blob/master/view/frontend/web/js/checkout/action/set-shipping-information-mixin.js

/**
 * Copyright © 2019 Paazl. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Updates current quote's shipping method with data from server.
 *      Reason: after shipping option is selected,
 *              JS object of the quote still contains old "method_title"
 */
define(
    [
        'underscore',
        'Magento_Checkout/js/model/quote',
        'widgetConfig'
    ],
    function (
        _,
        quote,
        widgetConfig
    ) {
        return function (target) {
            return function () {
                return target().done(function (res) {
                    var shippingMethod = quote.shippingMethod();
                    if (widgetConfig.prototype.getCarrierCode() !== shippingMethod.carrier_code
                        || widgetConfig.prototype.getMethodCode() !== shippingMethod.method_code) {
                        return;
                    }

                    var methods = res.extension_attributes && res.extension_attributes.shipping_methods || [];
                    var found = _.find(methods, function (m) {
                        return m.carrier_code === shippingMethod.carrier_code
                            && m.method_code === shippingMethod.method_code;
                    });

                    found && quote.shippingMethod(found);
                })
            }
        }
    }
);

the methods are retrieved by res.extension_attributes. However the extension_attributes are not at the top level in the response:

payment_methods: (3) [{…}, {…}, {…}]
totals: {grand_total: 7.39, base_grand_total: 8.94, subtotal: 3.3, base_subtotal: 3.3, discount_amount: 0, …}
[[Prototype]]: Object

The extension_attributes are in the totals. So I guess the res.extension_attributes should be changed to: res.totals.extension_attributes:

console.log(res.totals.extension_attributes):
base_reward_currency_amount: 0
reward_currency_amount: 0
reward_points_balance: 0
shipping_methods: [{…}]

So change it to:

return function (target) {
            return function () {
                return target().done(function (res) {
                    var shippingMethod = quote.shippingMethod();
                    if (widgetConfig.prototype.getCarrierCode() !== shippingMethod.carrier_code
                        || widgetConfig.prototype.getMethodCode() !== shippingMethod.method_code) {
                        return;
                    }

                    var methods = res.totals.extension_attributes && res.totals.extension_attributes.shipping_methods || []; <<<<<<-----
                    var found = _.find(methods, function (m) {
                        return m.carrier_code === shippingMethod.carrier_code
                            && m.method_code === shippingMethod.method_code;
                    });

                    found && quote.shippingMethod(found);
                })
            }
        }
    }
Marvin-Magmodules commented 3 years ago

Hi Lex, thank you for opening this issue and suggestion for the fix. This has been added to the v1.9.0 release. We are closing this issue now but please feel free to reopen the issue if this still occurs.