@@ -14,55 +14,71 @@ define([
14
14
'use strict' ;
15
15
16
16
var rateProcessors = [ ] ,
17
- totalsProcessors = [ ] ;
17
+ totalsProcessors = [ ] ,
18
18
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 ( ) ;
21
24
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
36
28
) {
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' ) ) ;
38
41
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
+ } ) ;
40
55
}
56
+ } ,
41
57
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
+ } ,
47
64
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
+ } ;
59
79
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 ) ;
83
+ customerData . get ( 'cart' ) . subscribe ( estimateTotalsShipping ) ;
68
84
} ) ;
0 commit comments