Last active
May 18, 2022 15:14
-
-
Save ikraamg/0a7e3998a706ade156bd40ff659f2dc4 to your computer and use it in GitHub Desktop.
Send dynamic promotions and metadata via addToCart method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/packages/nextjs-starter-autonomy/src/redux/actions/cart/add-to-cart.js b/packages/nextjs-starter-autonomy/src/redux/actions/cart/add-to-cart.js | |
index e1733c3..eab6c7d 100644 | |
--- a/packages/nextjs-starter-autonomy/src/redux/actions/cart/add-to-cart.js | |
+++ b/packages/nextjs-starter-autonomy/src/redux/actions/cart/add-to-cart.js | |
@@ -12,12 +12,18 @@ export const addToCartRequest = sku => { | |
} | |
} | |
-export const addToCartSuccess = (cart, variant, quantity) => { | |
+export const addToCartSuccess = ( | |
+ cart, | |
+ variant, | |
+ quantity, | |
+ additional_properties = {} | |
+) => { | |
return { | |
type: ADD_TO_CART_SUCCESS, | |
data: cart, | |
variant, | |
- quantity | |
+ quantity, | |
+ additional_properties | |
} | |
} | |
@@ -31,14 +37,14 @@ export const addToCartError = (error, meta = {}) => { | |
} | |
export const addToCart = | |
- (sku, quantity) => | |
+ (sku, quantity, additional_properties = {}) => | |
async (dispatch, getState, { api }) => { | |
try { | |
dispatch(addToCartRequest(sku)) | |
const { data: cart } = await getOrLoadCart(dispatch, getState) | |
- await api.addToCart(cart, sku, quantity) | |
+ await api.addToCart(cart, sku, quantity, additional_properties) | |
const updatedCart = await api.fetchCart(cart.number) | |
@@ -46,9 +52,11 @@ export const addToCart = | |
li => li.variant.sku === sku | |
) | |
const variant = addLineItem.variant | |
- dispatch(addToCartSuccess(updatedCart, variant, quantity)) | |
+ dispatch( | |
+ addToCartSuccess(updatedCart, variant, quantity, additional_properties) | |
+ ) | |
} catch (error) { | |
- dispatch(addToCartError(error, { sku, quantity })) | |
+ dispatch(addToCartError(error, { sku, quantity, additional_properties })) | |
throw error | |
} | |
} | |
diff --git a/packages/nextjs-starter-autonomy/src/services/api/index.js b/packages/nextjs-starter-autonomy/src/services/api/index.js | |
index 1c6d4c0..66ae5fe 100644 | |
--- a/packages/nextjs-starter-autonomy/src/services/api/index.js | |
+++ b/packages/nextjs-starter-autonomy/src/services/api/index.js | |
@@ -64,13 +64,14 @@ class Api { | |
return response.data | |
} | |
- async addToCart(cart, sku, quantity) { | |
+ async addToCart(cart, sku, quantity, additional_properties = {}) { | |
const url = `/api/orders/${cart.number}/line_items` | |
const payload = { | |
line_item: { | |
sku: sku, | |
- quantity: quantity | |
+ quantity: quantity, | |
+ ...additional_properties | |
} | |
} | |
diff --git a/packages/nextjs-starter-autonomy/src/services/api/index.test.js b/packages/nextjs-starter-autonomy/src/services/api/index.test.js | |
index 5a029db..6b69c3b 100644 | |
--- a/packages/nextjs-starter-autonomy/src/services/api/index.test.js | |
+++ b/packages/nextjs-starter-autonomy/src/services/api/index.test.js | |
@@ -64,10 +64,23 @@ describe('api', () => { | |
const cart = { number: 'CART-ID' } | |
const sku = 'SOME-SKU' | |
const quantity = 3 | |
+ const additional_properties = { | |
+ some_property: 'property', | |
+ array_of_objects: [ | |
+ { some_property: 'property' }, | |
+ { some_property: 'property' } | |
+ ] | |
+ } | |
+ | |
const payload = { | |
line_item: { | |
sku: sku, | |
- quantity: quantity | |
+ quantity: quantity, | |
+ some_property: 'property', | |
+ array_of_objects: [ | |
+ { some_property: 'property' }, | |
+ { some_property: 'property' } | |
+ ] | |
} | |
} | |
@@ -76,7 +89,7 @@ describe('api', () => { | |
return {} | |
}) | |
- api.addToCart(cart, sku, quantity) | |
+ api.addToCart(cart, sku, quantity, additional_properties) | |
expect(axios.post).toHaveBeenCalledWith( | |
`/api/orders/${cart.number}/line_items`, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment