|
8 | 8 | * Reason: after shipping option is selected,
|
9 | 9 | * JS object of the quote still contains old "method_title"
|
10 | 10 | */
|
11 |
| -define( |
12 |
| - [ |
13 |
| - 'underscore', |
14 |
| - 'Magento_Checkout/js/model/quote', |
15 |
| - 'widgetConfig', |
16 |
| - 'mage/utils/wrapper', |
17 |
| - 'Paazl_CheckoutWidget/js/checkout/model/shipping-locations' |
18 |
| - ], |
19 |
| - function ( |
20 |
| - _, |
21 |
| - quote, |
22 |
| - widgetConfig, |
23 |
| - wrapper, |
24 |
| - shippingLocations |
25 |
| - ) { |
26 |
| - return function (target) { |
27 |
| - return wrapper.wrap(target, function (originalAction) { |
28 |
| - var shippingMethod = quote.shippingMethod(); |
29 |
| - if (shippingLocations.selectedLocationCode() |
30 |
| - && shippingLocations.locationsList().length |
31 |
| - && shippingMethod |
32 |
| - && widgetConfig.prototype.getCarrierCode() === shippingMethod.carrier_code |
33 |
| - && widgetConfig.prototype.getMethodCode() === shippingMethod.method_code) { |
34 |
| - var collectionPointInfo =_.findWhere(shippingLocations.locationsList(), {code: shippingLocations.selectedLocationCode()}), |
35 |
| - shippingAddress = quote.shippingAddress(); |
36 |
| - if (collectionPointInfo && collectionPointInfo.address) { |
37 |
| - shippingAddress.countryId = collectionPointInfo.address.country; |
38 |
| - shippingAddress.city = collectionPointInfo.address.city; |
39 |
| - shippingAddress.postcode = collectionPointInfo.address.postalCode; |
40 |
| - shippingAddress.street = [ |
41 |
| - collectionPointInfo.address.street, |
42 |
| - collectionPointInfo.address.streetNumber |
43 |
| - ]; |
44 |
| - quote.shippingAddress(shippingAddress); |
45 |
| - } |
| 11 | +define([ |
| 12 | + 'underscore', |
| 13 | + 'Magento_Checkout/js/model/quote', |
| 14 | + 'widgetConfig', |
| 15 | + 'mage/utils/wrapper', |
| 16 | + 'Paazl_CheckoutWidget/js/checkout/model/shipping-locations', |
| 17 | + 'Magento_Customer/js/customer-data' |
| 18 | +], function (_, quote, widgetConfig, wrapper, shippingLocations, customerData) { |
| 19 | + 'use strict'; |
| 20 | + |
| 21 | + return function (target) { |
| 22 | + return wrapper.wrap(target, function (originalAction) { |
| 23 | + let shippingMethod = quote.shippingMethod(); |
| 24 | + let address = getActiveAddress(); |
| 25 | + |
| 26 | + if (shippingLocations.selectedLocationCode() |
| 27 | + && shippingLocations.locationsList().length |
| 28 | + && shippingMethod |
| 29 | + && widgetConfig.prototype.getCarrierCode() === shippingMethod.carrier_code |
| 30 | + && widgetConfig.prototype.getMethodCode() === shippingMethod.method_code) |
| 31 | + { |
| 32 | + var collectionPointInfo =_.findWhere(shippingLocations.locationsList(), {code: shippingLocations.selectedLocationCode()}), |
| 33 | + shippingAddress = quote.shippingAddress(); |
| 34 | + |
| 35 | + if (collectionPointInfo && collectionPointInfo.address) { |
| 36 | + shippingAddress.countryId = collectionPointInfo.address.country; |
| 37 | + shippingAddress.city = collectionPointInfo.address.city; |
| 38 | + shippingAddress.postcode = collectionPointInfo.address.postalCode; |
| 39 | + shippingAddress.street = [ |
| 40 | + collectionPointInfo.address.street, |
| 41 | + collectionPointInfo.address.streetNumber |
| 42 | + ]; |
| 43 | + quote.shippingAddress(shippingAddress); |
| 44 | + } |
| 45 | + } else { |
| 46 | + quote.shippingAddress(address); |
| 47 | + } |
| 48 | + |
| 49 | + // Update billing address after updating shipping address |
| 50 | + quote.billingAddress(address); |
| 51 | + |
| 52 | + function getActiveAddress() { |
| 53 | + const isCustomerLogin = window.checkoutConfig.isCustomerLoggedIn; |
| 54 | + const selectedShippingAddress = customerData.get('checkout-data')().selectedShippingAddress; |
| 55 | + const shippingAddress = quote.billingAddress(); |
| 56 | + let currentAddress; |
| 57 | + |
| 58 | + if (isCustomerLogin) { |
| 59 | + currentAddress = selectedShippingAddress === 'new-customer-address' |
| 60 | + ? customerData.get('checkout-data')().shippingAddressFromData |
| 61 | + : window.checkoutConfig.customerData.addresses[getAddressId()]; |
| 62 | + } else { |
| 63 | + currentAddress = customerData.get('checkout-data')().shippingAddressFromData; |
46 | 64 | }
|
47 | 65 |
|
48 |
| - return originalAction().done(function (res) { |
49 |
| - var shippingMethod = quote.shippingMethod(); |
50 |
| - if (widgetConfig.prototype.getCarrierCode() !== shippingMethod.carrier_code |
51 |
| - || widgetConfig.prototype.getMethodCode() !== shippingMethod.method_code) { |
52 |
| - return; |
53 |
| - } |
54 |
| - var methods = []; |
55 |
| - if ((typeof res.totals !== 'undefined') |
56 |
| - && (typeof res.totals.extension_attributes !== 'undefined')) { |
57 |
| - methods = res.totals.extension_attributes || []; |
58 |
| - } else if (typeof res.extension_attributes !== 'undefined') { |
59 |
| - methods = res.extension_attributes.shipping_methods || []; |
60 |
| - } |
61 |
| - var found = _.find(methods, function (m) { |
62 |
| - return m.carrier_code === shippingMethod.carrier_code |
63 |
| - && m.method_code === shippingMethod.method_code; |
64 |
| - }); |
| 66 | + shippingAddress.countryId = currentAddress.country_id; |
| 67 | + shippingAddress.city = currentAddress.city; |
| 68 | + shippingAddress.postcode = currentAddress.postcode; |
| 69 | + shippingAddress.street = Object.values(currentAddress.street); |
| 70 | + |
| 71 | + return shippingAddress; |
| 72 | + } |
| 73 | + |
| 74 | + function getAddressId() { |
| 75 | + const selectedShippingAddress = customerData.get('checkout-data')().selectedShippingAddress; |
| 76 | + |
| 77 | + if (selectedShippingAddress) { |
| 78 | + return selectedShippingAddress?.match(/\d+/)[0]; |
| 79 | + } |
| 80 | + |
| 81 | + return window.checkoutConfig.customerData.default_billing; |
| 82 | + } |
| 83 | + |
| 84 | + return originalAction().done(function (res) { |
| 85 | + var shippingMethod = quote.shippingMethod(); |
| 86 | + if (widgetConfig.prototype.getCarrierCode() !== shippingMethod.carrier_code |
| 87 | + || widgetConfig.prototype.getMethodCode() !== shippingMethod.method_code) { |
| 88 | + return; |
| 89 | + } |
| 90 | + var methods = []; |
| 91 | + if ((typeof res.totals !== 'undefined') |
| 92 | + && (typeof res.totals.extension_attributes !== 'undefined')) { |
| 93 | + methods = res.totals.extension_attributes || []; |
| 94 | + } else if (typeof res.extension_attributes !== 'undefined') { |
| 95 | + methods = res.extension_attributes.shipping_methods || []; |
| 96 | + } |
| 97 | + var found = _.find(methods, function (m) { |
| 98 | + return m.carrier_code === shippingMethod.carrier_code |
| 99 | + && m.method_code === shippingMethod.method_code; |
| 100 | + }); |
65 | 101 |
|
66 |
| - found && quote.shippingMethod(found); |
67 |
| - }) |
68 |
| - }); |
69 |
| - } |
| 102 | + found && quote.shippingMethod(found); |
| 103 | + }) |
| 104 | + }); |
70 | 105 | }
|
71 |
| -); |
| 106 | +}); |
0 commit comments