Skip to content

Commit 8433083

Browse files
author
Timon de Groot
committed
Restructure estimate-service.js
1 parent f710f9b commit 8433083

File tree

1 file changed

+58
-43
lines changed

1 file changed

+58
-43
lines changed

app/code/Magento/Checkout/view/frontend/web/js/model/cart/estimate-service.js

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,70 @@ define([
1414
'use strict';
1515

1616
var rateProcessors = [],
17-
totalsProcessors = [];
17+
totalsProcessors = [],
1818

19-
quote.shippingAddress.subscribe(function () {
20-
var type = quote.shippingAddress().getType();
19+
/**
20+
* Estimate totals for shipping address and update shipping rates.
21+
*/
22+
estimateTotalsAndUpdateRates = function() {
23+
var type = quote.shippingAddress().getType();
2124

22-
if (
23-
quote.isVirtual() ||
24-
window.checkoutConfig.activeCarriers && window.checkoutConfig.activeCarriers.length === 0
25-
) {
26-
// update totals block when estimated address was set
27-
totalsProcessors['default'] = totalsDefaultProvider;
28-
totalsProcessors[type] ?
29-
totalsProcessors[type].estimateTotals(quote.shippingAddress()) :
30-
totalsProcessors['default'].estimateTotals(quote.shippingAddress());
31-
} else {
32-
// check if user data not changed -> load rates from cache
33-
if (!cartCache.isChanged('address', quote.shippingAddress()) &&
34-
!cartCache.isChanged('cartVersion', customerData.get('cart')()['data_id']) &&
35-
cartCache.get('rates')
25+
if (
26+
quote.isVirtual() ||
27+
window.checkoutConfig.activeCarriers && window.checkoutConfig.activeCarriers.length === 0
3628
) {
37-
shippingService.setShippingRates(cartCache.get('rates'));
29+
// update totals block when estimated address was set
30+
totalsProcessors['default'] = totalsDefaultProvider;
31+
totalsProcessors[type] ?
32+
totalsProcessors[type].estimateTotals(quote.shippingAddress()) :
33+
totalsProcessors['default'].estimateTotals(quote.shippingAddress());
34+
} else {
35+
// check if user data not changed -> load rates from cache
36+
if (!cartCache.isChanged('address', quote.shippingAddress()) &&
37+
!cartCache.isChanged('cartVersion', customerData.get('cart')()['data_id']) &&
38+
cartCache.get('rates')
39+
) {
40+
shippingService.setShippingRates(cartCache.get('rates'));
3841

39-
return;
42+
return;
43+
}
44+
45+
// update rates list when estimated address was set
46+
rateProcessors['default'] = defaultProcessor;
47+
rateProcessors[type] ?
48+
rateProcessors[type].getRates(quote.shippingAddress()) :
49+
rateProcessors['default'].getRates(quote.shippingAddress());
50+
51+
// save rates to cache after load
52+
shippingService.getShippingRates().subscribe(function (rates) {
53+
cartCache.set('rates', rates);
54+
});
4055
}
56+
},
4157

42-
// update rates list when estimated address was set
43-
rateProcessors['default'] = defaultProcessor;
44-
rateProcessors[type] ?
45-
rateProcessors[type].getRates(quote.shippingAddress()) :
46-
rateProcessors['default'].getRates(quote.shippingAddress());
58+
/**
59+
* Estimate totals for shipping address.
60+
*/
61+
estimateTotalsShipping = function() {
62+
totalsDefaultProvider.estimateTotals(quote.shippingAddress());
63+
},
4764

48-
// save rates to cache after load
49-
shippingService.getShippingRates().subscribe(function (rates) {
50-
cartCache.set('rates', rates);
51-
});
52-
}
53-
});
54-
quote.shippingMethod.subscribe(function () {
55-
totalsDefaultProvider.estimateTotals(quote.shippingAddress());
56-
});
57-
quote.billingAddress.subscribe(function () {
58-
var type = quote.billingAddress().getType();
65+
/**
66+
* Estimate totals for billing address.
67+
*/
68+
estimateTotalsBilling = function() {
69+
var type = quote.billingAddress().getType();
70+
71+
if (quote.isVirtual()) {
72+
// update totals block when estimated address was set
73+
totalsProcessors['default'] = totalsDefaultProvider;
74+
totalsProcessors[type] ?
75+
totalsProcessors[type].estimateTotals(quote.billingAddress()) :
76+
totalsProcessors['default'].estimateTotals(quote.billingAddress());
77+
}
78+
};
5979

60-
if (quote.isVirtual()) {
61-
// update totals block when estimated address was set
62-
totalsProcessors['default'] = totalsDefaultProvider;
63-
totalsProcessors[type] ?
64-
totalsProcessors[type].estimateTotals(quote.billingAddress()) :
65-
totalsProcessors['default'].estimateTotals(quote.billingAddress());
66-
}
67-
});
80+
quote.shippingAddress.subscribe(estimateTotalsAndUpdateRates);
81+
quote.shippingMethod.subscribe(estimateTotalsShipping);
82+
quote.billingAddress.subscribe(estimateTotalsBilling);
6883
});

0 commit comments

Comments
 (0)