@@ -9,28 +9,20 @@ const connectActions = {
99 toggleMicrocart ( { commit } ) {
1010 commit ( types . CART_TOGGLE_MICROCART )
1111 } ,
12- async clear ( { commit, dispatch, getters } , options = { recreateAndSyncCart : true } ) {
12+ async clear ( { commit, dispatch, getters } ) {
1313 await commit ( types . CART_LOAD_CART , [ ] )
14- if ( options . recreateAndSyncCart && getters . isCartSyncEnabled ) {
15- await commit ( types . CART_LOAD_CART_SERVER_TOKEN , null )
16- await commit ( types . CART_SET_ITEMS_HASH , null )
17- await dispatch ( 'connect' , { guestCart : ! config . orders . directBackendSync } ) // guest cart when not using directBackendSync because when the order hasn't been passed to Magento yet it will repopulate your cart
18- }
14+ await commit ( types . CART_LOAD_CART_SERVER_TOKEN , null )
15+ await commit ( types . CART_SET_ITEMS_HASH , null )
1916 } ,
2017 async disconnect ( { commit } ) {
2118 commit ( types . CART_LOAD_CART_SERVER_TOKEN , null )
2219 } ,
2320 async authorize ( { dispatch, getters } ) {
24- const coupon = getters . getCoupon . code
25- const lastCartBypassTs = await StorageManager . get ( 'user' ) . getItem ( 'last-cart-bypass-ts' )
26- const timeBypassCart = config . orders . directBackendSync || ( Date . now ( ) - lastCartBypassTs ) >= ( 1000 * 60 * 24 )
27-
28- if ( ! config . cart . bypassCartLoaderForAuthorizedUsers || timeBypassCart ) {
29- await dispatch ( 'connect' , { guestCart : false } )
21+ await dispatch ( 'connect' , { guestCart : false } )
3022
31- if ( ! getters . getCoupon ) {
32- await dispatch ( 'applyCoupon' , coupon )
33- }
23+ const coupon = getters . getCoupon . code
24+ if ( ! getters . getCoupon ) {
25+ await dispatch ( 'applyCoupon' , coupon )
3426 }
3527 } ,
3628 async connect ( { getters, dispatch, commit } , { guestCart = false , forceClientState = false } ) {
@@ -41,7 +33,7 @@ const connectActions = {
4133 Logger . info ( 'Server cart token created.' , 'cart' , result ) ( )
4234 commit ( types . CART_LOAD_CART_SERVER_TOKEN , result )
4335
44- return dispatch ( 'sync' , { forceClientState, dryRun : ! config . cart . serverMergeByDefault } )
36+ return dispatch ( 'sync' , { forceClientState, dryRun : ! config . cart . serverMergeByDefault , mergeQty : true } )
4537 }
4638
4739 if ( resultCode === 401 && getters . bypassCounter < config . queues . maxCartBypassAttempts ) {
@@ -53,6 +45,17 @@ const connectActions = {
5345
5446 Logger . warn ( 'Cart sync is disabled by the config' , 'cart' ) ( )
5547 return createDiffLog ( )
48+ } ,
49+ /**
50+ * Create cart token when there are products in cart and we don't have token already
51+ */
52+ async create ( { dispatch, getters } ) {
53+ const storedItems = getters [ 'getCartItems' ] || [ ]
54+ const cartToken = getters [ 'getCartToken' ]
55+ if ( storedItems . length && ! cartToken ) {
56+ Logger . info ( 'Creating server cart token' , 'cart' ) ( )
57+ await dispatch ( 'connect' , { guestCart : false } )
58+ }
5659 }
5760}
5861
0 commit comments