From 58248378140e2320723dee1b9937e69161d05756 Mon Sep 17 00:00:00 2001 From: Marcin Kwiatkowski Date: Tue, 5 Jul 2022 13:53:30 +0200 Subject: [PATCH 1/2] fix: m2-723 - button add to cart doesn't work for configurable products --- .../theme/modules/catalog/pages/product.vue | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/theme/modules/catalog/pages/product.vue b/packages/theme/modules/catalog/pages/product.vue index b4ddb389d..733ef8973 100644 --- a/packages/theme/modules/catalog/pages/product.vue +++ b/packages/theme/modules/catalog/pages/product.vue @@ -102,7 +102,7 @@ export default defineComponent({ // eslint-disable-next-line no-underscore-dangle const renderer = computed(() => product.value?.__typename ?? ProductTypeEnum.SIMPLE_PRODUCT); - const fetchProduct = async (searchQuery = getBaseSearchQuery()) => { + const fetchProductBaseData = async (searchQuery = getBaseSearchQuery()) => { const result = await getProductDetails({ ...searchQuery, }); @@ -110,8 +110,14 @@ export default defineComponent({ product.value = merge({}, product.value, result.items[0] as Product ?? null); }; + const fetchProductExtendedData = async (searchQuery = getBaseSearchQuery()) => { + const { data } = await query(getProductPriceBySkuGql, searchQuery); + + product.value = merge({}, product.value, data.products?.items?.[0] as Product); + }; + useFetch(async () => { - await fetchProduct(); + await fetchProductBaseData(); if (Boolean(product?.value?.sku) === false) nuxtError({ statusCode: 404 }); @@ -131,18 +137,14 @@ export default defineComponent({ addTags([...tags, ...productTags]); }); - onMounted(async () => { - const { data } = await query(getProductPriceBySkuGql, getBaseSearchQuery()); - - product.value = merge({}, product.value, data.products?.items?.[0] as Product); - }); + onMounted(async () => fetchProductExtendedData()); return { renderer, loading, breadcrumbs, product, - fetchProduct, + fetchProduct: fetchProductExtendedData, }; }, }); From 55a59e83415f6e1a27cc83c01c628904132535af Mon Sep 17 00:00:00 2001 From: Bartosz Herba Date: Wed, 6 Jul 2022 09:04:27 +0200 Subject: [PATCH 2/2] fix: currency cookie breaks the application M2-918 --- packages/theme/plugins/__tests__/i18n.spec.js | 3 ++ packages/theme/plugins/i18n.ts | 38 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/theme/plugins/__tests__/i18n.spec.js b/packages/theme/plugins/__tests__/i18n.spec.js index 2d166594d..c88aa539a 100644 --- a/packages/theme/plugins/__tests__/i18n.spec.js +++ b/packages/theme/plugins/__tests__/i18n.spec.js @@ -118,5 +118,8 @@ describe('i18n plugin', () => { expect(testCaseAppMock.$vsf.$magento.config.state.setLocale).toHaveBeenCalledWith('de_DE'); expect(testCaseAppMock.$vsf.$magento.config.state.setStore).toHaveBeenCalledWith('de_DE'); expect(testCaseAppMock.$vsf.$magento.config.state.setCurrency).toHaveBeenCalledWith('EUR'); + expect(callbackRequest.headers.cookie).toMatchInlineSnapshot( + '"vsf-store=de_DE; vsf-locale=de_DE; vsf-currency=EUR; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "', + ); }); }); diff --git a/packages/theme/plugins/i18n.ts b/packages/theme/plugins/i18n.ts index d8efca3a4..d6367b853 100644 --- a/packages/theme/plugins/i18n.ts +++ b/packages/theme/plugins/i18n.ts @@ -1,4 +1,4 @@ -import { Context } from '@nuxt/types'; +import { Context, NuxtAppOptions } from '@nuxt/types'; import { LocaleObject } from 'nuxt-i18n'; const findLocaleBasedOnMagentoStoreCode = (storeCode: string, locales: Array) => locales.find((locale) => ((typeof locale === 'object' ? locale.code : locale) === storeCode)); @@ -8,6 +8,35 @@ const findCurrencyBasedOnMagentoStoreCode = (storeCode: string, locales: Array { + const apiState = app.$vsf.$magento.config.state; + const customerTokenCookie = apiState.getCustomerToken(); + const cartIdCookie = apiState.getCartId(); + + let cookie = `vsf-store=${newStoreCode}; `; + cookie += `vsf-locale=${newStoreCode}; `; + cookie += `vsf-currency=${currency}; `; + cookie += `vsf-country=${apiState.getCountry()}; `; + + if (customerTokenCookie) { + cookie += `vsf-customer=${customerTokenCookie}; `; + } + + if (cartIdCookie) { + cookie += `vsf-cart=${cartIdCookie} `; + } + + return cookie; +}; + export default ({ app, route }: Context) => app.$vsf.$magento.client.interceptors.request.use(async (request) => { const { $vsf: { $magento: { config: { state } } }, @@ -25,11 +54,14 @@ export default ({ app, route }: Context) => app.$vsf.$magento.client.interceptor const shouldLocaleBeRefreshed = i18nCurrentLocaleCode !== state.getLocale(); if (shouldLocaleBeRefreshed) { - const currency = findCurrencyBasedOnMagentoStoreCode(currentStoreCode, i18n.locales); - state.setCurrency(currency); + const currency = findCurrencyBasedOnMagentoStoreCode(i18nCurrentLocaleCode, i18n.locales); state.setStore(i18nCurrentLocaleCode); state.setLocale(i18nCurrentLocaleCode); + state.setCurrency(currency); + + // eslint-disable-next-line no-param-reassign + request.headers.cookie = prepareNewCookieString(app, i18nCurrentLocaleCode, currency); } return request;