@@ -36,26 +36,16 @@ const findCheapestShippingOption = (shippingOptions: StoreCartShippingOption[])
36
36
} ;
37
37
38
38
const ensureSelectedCartShippingMethod = async ( request : Request , cart : StoreCart ) => {
39
- const shippingOptions = await fetchShippingOptions ( cart . id ) ;
40
-
41
39
const selectedShippingMethod = cart . shipping_methods ?. [ 0 ] ;
42
40
43
- const selectedShippingOption =
44
- selectedShippingMethod && shippingOptions . find ( ( option ) => option . id === selectedShippingMethod . shipping_option_id ) ;
41
+ if ( selectedShippingMethod ) return ;
45
42
46
- if (
47
- ! selectedShippingMethod || // No shipping method has been selected
48
- ! selectedShippingOption // The selected shipping method is no longer available
49
- ) {
50
- const cheapestShippingOption = findCheapestShippingOption ( shippingOptions ) ;
43
+ const shippingOptions = await fetchShippingOptions ( cart . id ) ;
51
44
52
- if ( cheapestShippingOption ) {
53
- await setShippingMethod ( request , { cartId : cart . id , shippingOptionId : cheapestShippingOption . id } ) ;
54
- }
45
+ const cheapestShippingOption = findCheapestShippingOption ( shippingOptions ) ;
55
46
56
- return ;
57
- } else if ( selectedShippingMethod . amount !== selectedShippingOption . amount ) {
58
- await setShippingMethod ( request , { cartId : cart . id , shippingOptionId : selectedShippingOption . id } ) ;
47
+ if ( cheapestShippingOption ) {
48
+ await setShippingMethod ( request , { cartId : cart . id , shippingOptionId : cheapestShippingOption . id } ) ;
59
49
}
60
50
} ;
61
51
@@ -64,9 +54,10 @@ const ensureCartPaymentSessions = async (request: Request, cart: StoreCart) => {
64
54
65
55
let activeSession = cart . payment_collection ?. payment_sessions ?. find ( ( session ) => session . status === 'pending' ) ;
66
56
67
- const paymentProviders = await listCartPaymentProviders ( cart . region_id ! ) ;
57
+ if ( ! activeSession ) {
58
+ const paymentProviders = await listCartPaymentProviders ( cart . region_id ! ) ;
59
+ if ( ! paymentProviders . length ) return activeSession ;
68
60
69
- if ( ! activeSession && paymentProviders . length ) {
70
61
const provider = paymentProviders . find ( ( p ) => p . id !== SYSTEM_PROVIDER_ID ) || paymentProviders [ 0 ] ;
71
62
72
63
const { payment_collection } = await initiatePaymentSession ( request , cart , {
0 commit comments