@@ -15,14 +15,26 @@ const readStoreCookie = (app: NuxtAppOptions) => app.$cookies.get(cookieNames.st
1515 * Find locale code based on magento store code
1616 * @param storeCode {string} - magento store code
1717 * @param locales {array} - array with locales
18- * @returns boolean
18+ * @returns string
1919 */
2020const findLocaleBasedOnStoreCode = ( storeCode : string , locales : string [ ] | LocaleObject [ ] ) => {
2121 if ( locales . some ( ( l ) => typeof l !== 'string' ) ) {
22- return ! ! ( locales as LocaleObject [ ] ) . find ( ( locale ) => locale . code === storeCode ) ;
22+ return ( locales as LocaleObject [ ] ) . find ( ( locale ) => locale . code === storeCode ) ;
2323 }
2424
25- return ( locales as string [ ] ) . includes ( storeCode ) ;
25+ return ( locales as string [ ] ) . find ( ( locale ) => locale === storeCode ) ;
26+ } ;
27+
28+ /**
29+ * Find defaultCurrency code based on magento store code
30+ * @param storeCode {string} - magento store code
31+ * @param locales {array} - array with locales
32+ * @returns string
33+ */
34+ const findCurrencyBasedOnStoreCode = ( storeCode : string , locales : string [ ] | LocaleObject [ ] ) : string => {
35+ const match = ( locales as LocaleObject [ ] ) . find ( ( locale ) => locale . code === storeCode ) ;
36+
37+ return match . defaultCurrency ;
2638} ;
2739
2840/**
@@ -39,15 +51,16 @@ const setDefaultLocale = async (i18n) => {
3951 *
4052 * @param apiState {ConfigState}
4153 * @param newStoreCode {string}
54+ * @param currency {string}
4255 * @returns {string }
4356 */
44- const prepareNewCookieString = ( apiState : ConfigState , newStoreCode : string ) => {
57+ const prepareNewCookieString = ( apiState : ConfigState , newStoreCode : string , currency : string ) => {
4558 const customerTokenCookie = apiState . getCustomerToken ( ) ;
4659 const cartIdCookie = apiState . getCartId ( ) ;
4760
4861 let cookie = `vsf-store=${ newStoreCode } ; ` ;
4962 cookie += `vsf-locale=${ newStoreCode } ; ` ;
50- cookie += `vsf-currency=${ apiState . getCurrency ( ) } ; ` ;
63+ cookie += `vsf-currency=${ currency } ; ` ;
5164 cookie += `vsf-country=${ apiState . getCountry ( ) } ; ` ;
5265
5366 if ( customerTokenCookie ) {
@@ -61,11 +74,10 @@ const prepareNewCookieString = (apiState: ConfigState, newStoreCode: string) =>
6174 return cookie ;
6275} ;
6376
64- export default async ( { app } : Context ) => {
77+ export default async ( { app, route } : Context ) => {
6578 await app . $vsf . $magento . client . interceptors . request . use ( async ( request ) => {
6679 const { i18n } = app ;
67- const currentStoreCode = readStoreCookie ( app ) ;
68-
80+ const currentStoreCode = readStoreCookie ( app ) ?? route . path . split ( '/' ) . find ( ( element ) => String ( element ) ) ;
6981 // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
7082 if ( ! currentStoreCode || ! findLocaleBasedOnStoreCode ( currentStoreCode , i18n . locales ) ) {
7183 await setDefaultLocale ( i18n ) ;
@@ -74,16 +86,19 @@ export default async ({ app }: Context) => {
7486 }
7587
7688 const i18nCurrentLocaleCode = i18n . locale ;
89+
7790 const localeCookie = app . $cookies . get ( cookieNames . localeCookieName ) ;
7891
7992 if ( i18nCurrentLocaleCode !== localeCookie ) {
8093 const apiState = app . $vsf . $magento . config . state as ConfigState ;
94+ const currency = findCurrencyBasedOnStoreCode ( i18nCurrentLocaleCode , i18n . locales ) ;
8195
8296 apiState . setStore ( i18nCurrentLocaleCode ) ;
8397 apiState . setLocale ( i18nCurrentLocaleCode ) ;
98+ apiState . setCurrency ( currency ) ;
8499
85100 // eslint-disable-next-line no-param-reassign
86- request . headers . cookie = prepareNewCookieString ( apiState , i18nCurrentLocaleCode ) ;
101+ request . headers . cookie = prepareNewCookieString ( apiState , i18nCurrentLocaleCode , currency ) ;
87102 }
88103
89104 return request ;
0 commit comments