Skip to content

Commit 6c2562c

Browse files
authored
Merge pull request #4995 from DivanteLtd/doc-multistore-refactor
Draft: docs: multistore refactor
2 parents 57c7bef + c18e0cf commit 6c2562c

File tree

1 file changed

+215
-14
lines changed

1 file changed

+215
-14
lines changed

docs/guide/integrations/multistore.md

Lines changed: 215 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Vue Storefront supports Magento Multistore installations
66

77
Multiwebsite support starts with the Elasticsearch indexing. Basically, each store has its own Elasticsearch index and should be populated separately using the [mage2vuestorefront](https://github.com/DivanteLtd/mage2vuestorefront) tool.
88

9+
:::warning
10+
11+
Using node indexer to enable _multistore_ feature for _Vue Storefront_ has been deprecated.
12+
You can use _Bridge Indexer_ for the better performance and maintainability. For Magento 2, you can find it [here](https://github.com/DivanteLtd/magento2-vsbridge-indexer)
13+
:::
14+
915
The simplest script to index multi site:
1016

1117
```bash
@@ -102,17 +108,30 @@ The last thing is to change the `vue-storefront/config/local.json` to configure
102108
"tax": {
103109
"defaultCountry": "DE",
104110
"defaultRegion": "",
105-
"calculateServerSide": true
111+
"sourcePriceIncludesTax": false,
112+
"calculateServerSide": true,
113+
"userGroupId": null,
114+
"useOnlyDefaultUserGroupId": false,
115+
"deprecatedPriceFieldsSupport": true,
116+
"finalPriceIncludesTax": false
106117
},
107118
"i18n": {
108-
"fullCountryName": "Germany",
109-
"fullLanguageName": "German",
110-
"defaultLanguage": "DE",
111119
"defaultCountry": "DE",
120+
"defaultLanguage": "DE",
121+
"availableLocale": [
122+
"de-DE"
123+
],
112124
"defaultLocale": "de-DE",
113125
"currencyCode": "EUR",
114-
"currencySign": "EUR",
115-
"dateFormat": "HH:mm D-M-YYYY"
126+
"currencySign": "",
127+
"currencyDecimal": "",
128+
"currencyGroup": "",
129+
"fractionDigits": 2,
130+
"priceFormat": "{sign}{amount}",
131+
"dateFormat": "HH:mm D/M/YYYY",
132+
"fullCountryName": "Deutschland",
133+
"fullLanguageName": "German",
134+
"bundleAllStoreviewLanguages": false
116135
}
117136
},
118137
"it": {
@@ -125,24 +144,41 @@ The last thing is to change the `vue-storefront/config/local.json` to configure
125144
"index": "vue_storefront_catalog_it"
126145
},
127146
"tax": {
128-
"defaultCountry": "DE",
147+
"defaultCountry": "IT",
129148
"defaultRegion": "",
130-
"calculateServerSide": true
149+
"sourcePriceIncludesTax": false,
150+
"calculateServerSide": true,
151+
"userGroupId": null,
152+
"useOnlyDefaultUserGroupId": false,
153+
"deprecatedPriceFieldsSupport": true,
154+
"finalPriceIncludesTax": false
131155
},
132156
"i18n": {
133-
"fullCountryName": "Italy",
134-
"fullLanguageName": "Italian",
135157
"defaultCountry": "IT",
136158
"defaultLanguage": "IT",
159+
"availableLocale": [
160+
"it-IT"
161+
],
137162
"defaultLocale": "it-IT",
138163
"currencyCode": "EUR",
139-
"currencySign": "EUR",
140-
"dateFormat": "HH:mm D-M-YYYY"
164+
"currencySign": "",
165+
"currencyDecimal": "",
166+
"currencyGroup": "",
167+
"fractionDigits": 2,
168+
"priceFormat": "{sign}{amount}",
169+
"dateFormat": "HH:mm D/M/YYYY",
170+
"fullCountryName": "Italy",
171+
"fullLanguageName": "Italian",
172+
"bundleAllStoreviewLanguages": false
141173
}
142174
}
143175
},
144176
```
145177

178+
:::tip
179+
You can find more options available to _multistore_ features in [store view](/guide/basics/configuration.html#store-views) section of _Configuration File Explained_.
180+
:::
181+
146182
After these changes, you'll have a `LanguageSwitcher` component visible on the bottom.
147183

148184
By default, the language / store is switched by the URL prefix:
@@ -178,9 +214,162 @@ export default function(app, router, store) {
178214

179215
Another option is to create a separate theme for a specific storeview. Runtime theme changes are not possible, as themes are compiled in the JS bundles by webpack during the page build process. In that case, you should run separate instances of `vue-storefront` having the proper theme set in the `config/local.json` file.
180216

181-
## Localized routes
217+
## Useful _Helpers_
218+
219+
### How to get current store view?
220+
221+
Here is a helper method to get the value of current store view.
222+
223+
You just need to import `currentStoreView` function from `core/lib/multistore` as example follows:
224+
225+
```js
226+
import { currentStoreView } from '@vue-storefront/core/lib/multistore'
227+
// ... abridged
228+
return {
229+
currency: currentStoreView().i18n.currencyCode,
230+
value: method.price_incl_tax
231+
}
232+
```
233+
234+
`currentStoreView()` will return the object value you set in `local.json` - it should be type of StoreView or extended StoreView.
235+
```ts
236+
interface StoreView {
237+
storeCode: string,
238+
extend?: string,
239+
disabled?: boolean,
240+
storeId: any,
241+
name?: string,
242+
url?: string,
243+
appendStoreCode?: boolean,
244+
elasticsearch: {
245+
host: string,
246+
index: string
247+
},
248+
tax: {
249+
sourcePriceIncludesTax?: boolean,
250+
finalPriceIncludesTax?: boolean,
251+
deprecatedPriceFieldsSupport?: boolean,
252+
defaultCountry: string,
253+
defaultRegion: null | string,
254+
calculateServerSide: boolean,
255+
userGroupId?: number,
256+
useOnlyDefaultUserGroupId: boolean
257+
},
258+
i18n: {
259+
fullCountryName: string,
260+
fullLanguageName: string,
261+
defaultLanguage: string,
262+
defaultCountry: string,
263+
defaultLocale: string,
264+
currencyCode: string,
265+
currencySign: string,
266+
currencyDecimal: string,
267+
currencyGroup: string,
268+
fractionDigits: number,
269+
priceFormat: string,
270+
dateFormat: string
271+
},
272+
seo: {
273+
defaultTitle: string
274+
}
275+
}
276+
```
277+
278+
### How to remove store code from route
279+
280+
When you need to remove `storeCode` from route in such a case you want to route to default store by mapping fallback, use `removeStoreCodeFromRoute` as following example :
281+
282+
```js
283+
import { removeStoreCodeFromRoute } from '@vue-storefront/core/lib/multistore'
284+
// ... abridged
285+
const urlWithoutStorecode1 = removeStoreCodeFromRoute('/gb/home'); // should return '/home`
286+
const urlWithoutStorecode2 = removeStoreCodeFromRoute('gb/home'); // should return 'home`
287+
const urlWithoutStorecode3 = removeStoreCodeFromRoute({
288+
path: '/gb/home'
289+
}); // should return '/home`
290+
```
291+
292+
### Update/append a storeCode to your URL
293+
If you need to append or update `storeCode` query parameter in provided URL you can do it by calling `adjustMultistoreApiUrl` function as following example:
294+
295+
```js
296+
import { adjustMultistoreApiUrl } from '@vue-storefront/core/lib/multistore'
297+
298+
// ... abridged
299+
// Let's say current storeCode is `de`
300+
const myUrl1 = adjustMultistoreApiUrl('https://example.com?a=b'); // returns 'https://example.com?a=b&storeCode=de'
301+
const myUrl2 = adjustMultistoreApiUrl('https://example.com?a=b&storeCode=it'); // returns 'https://example.com?a=b&storeCode=de'
302+
```
303+
304+
This feature is extra useful when you are sending a request to the VSF-API and you want VSF-API to use endpoint of certain storeCode.
305+
306+
### Using endpoint of certain storeCode in Vue Storefront API
307+
In `src/api/extensions/example-magento-api/index.js` we have example line that creates Magento 2 Client:
308+
```js
309+
const client = Magento2Client(config.magento2.api);
310+
```
311+
312+
If you want to support multistore for certain endpoint, you should make it this way:
313+
```js
314+
const client = Magento2Client(multiStoreConfig(config.magento2.api, req));
315+
```
316+
317+
Obviously, you could do the same in Magento 1:
318+
```js
319+
const client = Magento1Client(multiStoreConfig(config.magento2.api, req));
320+
```
321+
322+
It will use `storeCode` query parameter from the `req` to figure out which store to use. To make it work properly you should also configure different stores in your VSF-API's config. Check this example configuration for `de` and `it` store codes:
323+
```js
324+
"magento2": {
325+
"imgUrl": "http://demo-magento2.vuestorefront.io/media/catalog/product",
326+
"assetPath": "/../var/magento2-sample-data/pub/media",
327+
"api": {
328+
"url": "https://my-magento.com/rest",
329+
"consumerKey": "******",
330+
"consumerSecret": "******",
331+
"accessToken": "******",
332+
"accessTokenSecret": "******"
333+
},
334+
"api_de": {
335+
"url": "https://my-magento.com/de/rest",
336+
"consumerKey": "******",
337+
"consumerSecret": "******",
338+
"accessToken": "******",
339+
"accessTokenSecret": "******"
340+
},
341+
"api_it": {
342+
"url": "https://my-magento.com/it/rest",
343+
"consumerKey": "******",
344+
"consumerSecret": "******",
345+
"accessToken": "******",
346+
"accessTokenSecret": "******"
347+
}
348+
},
349+
```
350+
351+
### Localize URL with correct store code
352+
:::tip
353+
Localized Route means denoting store code in URL by convention without a parameter.
354+
355+
e.g. It's `de` in `https://example.com/de?a=b`
356+
:::
357+
358+
Method that allows use to do that is `localizedRoute`. Example transformations for `de` as a current storeCode:
359+
```js
360+
localizedRoute('http://example.com/'); // returns http://example.com/de
361+
localizedRoute('/'); // returns /de
362+
localizedRoute('/?a=b'); // returns /de?a=b
363+
localizedRoute('/about'); // returns /de/about
364+
```
365+
366+
:::warning
367+
`appendStoreCode` option of the store view configuration should be set to `true` to display store code as tip above
368+
:::
182369

183-
The route switching mechanism by default works on the URL level. Please use the `localizedRoute` mixin:
370+
:::warning
371+
`localizedRoute` is injected to each Vue's instance so you can access it in your component with `this.localizedRoute`. You could also use it in template without additional imports.
372+
:::
184373

185374
```vue
186375
<router-link :to="localizedRoute(page.link)" class="cl-accent relative">{{
@@ -204,3 +393,15 @@ or
204393
"
205394
></router-link>
206395
```
396+
397+
### How to extract store code from route
398+
399+
You can extract store code from route as follows :
400+
401+
```js
402+
import storeCodeFromRoute from '@vue-storefront/core/lib/storeCodeFromRoute'
403+
// abridged
404+
const storeCode = storeCodeFromRoute(currentRoute)
405+
```
406+
407+
You should get store code `gb` from route `https://example.com/gb/foo` where storeCode is `gb`

0 commit comments

Comments
 (0)