From 825b43fdbf00155e11d93540d49d04c9b76d31bd Mon Sep 17 00:00:00 2001 From: richayles Date: Thu, 7 Jul 2022 11:16:50 +0100 Subject: [PATCH 1/2] Shopify routing to work with Markets --- packages/theme-cart/request.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/theme-cart/request.js b/packages/theme-cart/request.js index 8243befe..c4c997ed 100644 --- a/packages/theme-cart/request.js +++ b/packages/theme-cart/request.js @@ -20,7 +20,7 @@ function fetchJSON(url, config) { } export function cart() { - return fetchJSON('/cart.js', getDefaultRequestConfig()); + return fetchJSON(window.Shopify.routes.root + 'cart.js', getDefaultRequestConfig()); } export function cartAdd(id, quantity, properties) { @@ -33,7 +33,7 @@ export function cartAdd(id, quantity, properties) { properties: properties }); - return fetchJSON('/cart/add.js', config); + return fetchJSON(window.Shopify.routes.root + 'cart/add.js', config); } export function cartAddFromForm(formData) { @@ -43,7 +43,7 @@ export function cartAddFromForm(formData) { config.method = 'POST'; config.body = formData; - return fetchJSON('/cart/add.js', config); + return fetchJSON(window.Shopify.routes.root + 'cart/add.js', config); } export function cartChange(line, options) { @@ -58,14 +58,14 @@ export function cartChange(line, options) { properties: options.properties }); - return fetchJSON('/cart/change.js', config); + return fetchJSON(window.Shopify.routes.root + 'cart/change.js', config); } export function cartClear() { var config = getDefaultRequestConfig(); config.method = 'POST'; - return fetchJSON('/cart/clear.js', config); + return fetchJSON(window.Shopify.routes.root + 'cart/clear.js', config); } export function cartUpdate(body) { @@ -74,9 +74,9 @@ export function cartUpdate(body) { config.method = 'POST'; config.body = JSON.stringify(body); - return fetchJSON('/cart/update.js', config); + return fetchJSON(window.Shopify.routes.root + 'cart/update.js', config); } export function cartShippingRates() { - return fetchJSON('/cart/shipping_rates.json', getDefaultRequestConfig()); + return fetchJSON(window.Shopify.routes.root + 'cart/shipping_rates.json', getDefaultRequestConfig()); } From 7e4da488345070134e785a011f0f73275b652524 Mon Sep 17 00:00:00 2001 From: richayles Date: Thu, 7 Jul 2022 11:30:48 +0100 Subject: [PATCH 2/2] add in fallback route, just in case --- packages/theme-cart/request.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/theme-cart/request.js b/packages/theme-cart/request.js index c4c997ed..99f9bc94 100644 --- a/packages/theme-cart/request.js +++ b/packages/theme-cart/request.js @@ -19,8 +19,15 @@ function fetchJSON(url, config) { }); } +function getRoute() { + if(window.Shopify.routes.root) { + return window.Shopify.routes.root; + } + return '/'; +} + export function cart() { - return fetchJSON(window.Shopify.routes.root + 'cart.js', getDefaultRequestConfig()); + return fetchJSON(getRoute() + 'cart.js', getDefaultRequestConfig()); } export function cartAdd(id, quantity, properties) { @@ -33,7 +40,7 @@ export function cartAdd(id, quantity, properties) { properties: properties }); - return fetchJSON(window.Shopify.routes.root + 'cart/add.js', config); + return fetchJSON(getRoute() + 'cart/add.js', config); } export function cartAddFromForm(formData) { @@ -43,7 +50,7 @@ export function cartAddFromForm(formData) { config.method = 'POST'; config.body = formData; - return fetchJSON(window.Shopify.routes.root + 'cart/add.js', config); + return fetchJSON(getRoute() + 'cart/add.js', config); } export function cartChange(line, options) { @@ -58,14 +65,14 @@ export function cartChange(line, options) { properties: options.properties }); - return fetchJSON(window.Shopify.routes.root + 'cart/change.js', config); + return fetchJSON(getRoute() + 'cart/change.js', config); } export function cartClear() { var config = getDefaultRequestConfig(); config.method = 'POST'; - return fetchJSON(window.Shopify.routes.root + 'cart/clear.js', config); + return fetchJSON(getRoute() + 'cart/clear.js', config); } export function cartUpdate(body) { @@ -74,9 +81,9 @@ export function cartUpdate(body) { config.method = 'POST'; config.body = JSON.stringify(body); - return fetchJSON(window.Shopify.routes.root + 'cart/update.js', config); + return fetchJSON(getRoute() + 'cart/update.js', config); } export function cartShippingRates() { - return fetchJSON(window.Shopify.routes.root + 'cart/shipping_rates.json', getDefaultRequestConfig()); + return fetchJSON(getRoute() + 'cart/shipping_rates.json', getDefaultRequestConfig()); }