@shopware/composables 0.0.0-canary-20250116155626
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +172 -0
- package/composables/index.ts +1 -0
- package/dist/index.d.mts +2239 -0
- package/dist/index.d.ts +2239 -0
- package/dist/index.mjs +2686 -0
- package/nuxt.config.ts +14 -0
- package/package.json +83 -0
- package/src/_test/index.ts +1 -0
- package/src/_test/useSetup.ts +43 -0
- package/src/cms/README.md +9 -0
- package/src/cms/cmsAssociations.ts +28 -0
- package/src/cms/index.ts +2 -0
- package/src/cms/useCmsElementConfig.test.ts +145 -0
- package/src/cms/useCmsElementConfig.ts +29 -0
- package/src/cms/useCmsElementImage.test.ts +179 -0
- package/src/cms/useCmsElementImage.ts +103 -0
- package/src/cms/useCmsElementProductBox.test.ts +15 -0
- package/src/cms/useCmsElementProductBox.ts +25 -0
- package/src/createShopwareContext.test.ts +23 -0
- package/src/createShopwareContext.ts +43 -0
- package/src/devtools/plugin.ts +201 -0
- package/src/helpers/ContextError.spec.ts +20 -0
- package/src/helpers/ContextError.ts +6 -0
- package/src/index.test.ts +54 -0
- package/src/index.ts +111 -0
- package/src/mocks/Cart.ts +774 -0
- package/src/mocks/CmsPage.ts +544 -0
- package/src/mocks/CmsProductBox.ts +377 -0
- package/src/mocks/Configurator.ts +123 -0
- package/src/mocks/Country.ts +184 -0
- package/src/mocks/CrossSellingResponse.ts +552 -0
- package/src/mocks/LandingPage.ts +62 -0
- package/src/mocks/Menu.ts +598 -0
- package/src/mocks/Order.ts +1151 -0
- package/src/mocks/Product.ts +473 -0
- package/src/mocks/ProductTierPrices.ts +771 -0
- package/src/mocks/Salutations.ts +51 -0
- package/src/mocks/Search.ts +518 -0
- package/src/types/cmsBlockTypes.ts +80 -0
- package/src/types/cmsElementTypes.ts +381 -0
- package/src/types/cmsSectionTypes.ts +15 -0
- package/src/types/index.ts +52 -0
- package/src/useAddToCart/useAddToCart.test.ts +39 -0
- package/src/useAddToCart/useAddToCart.ts +90 -0
- package/src/useAddress/useAddress.test.ts +134 -0
- package/src/useAddress/useAddress.ts +202 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +137 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.ts +225 -0
- package/src/useBreadcrumbs/useBreadcrumbs.test.ts +71 -0
- package/src/useBreadcrumbs/useBreadcrumbs.ts +89 -0
- package/src/useCart/useCart.test.ts +251 -0
- package/src/useCart/useCart.ts +314 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +122 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.ts +80 -0
- package/src/useCartItem/useCartItem.test.ts +168 -0
- package/src/useCartItem/useCartItem.ts +157 -0
- package/src/useCartNotification/useCartNotification.test.ts +129 -0
- package/src/useCartNotification/useCartNotification.ts +63 -0
- package/src/useCategory/useCategory.test.ts +14 -0
- package/src/useCategory/useCategory.ts +32 -0
- package/src/useCategorySearch/useCategorySearch.test.ts +98 -0
- package/src/useCategorySearch/useCategorySearch.ts +81 -0
- package/src/useCheckout/useCheckout.test.ts +104 -0
- package/src/useCheckout/useCheckout.ts +163 -0
- package/src/useCmsBlock/useCmsBlock.test.ts +25 -0
- package/src/useCmsBlock/useCmsBlock.ts +36 -0
- package/src/useCmsMeta/useCmsMeta.test.ts +17 -0
- package/src/useCmsMeta/useCmsMeta.ts +46 -0
- package/src/useCmsSection/useCmsSection.test.ts +18 -0
- package/src/useCmsSection/useCmsSection.ts +39 -0
- package/src/useCmsTranslations/useCmsTranslations.test.ts +10 -0
- package/src/useCmsTranslations/useCmsTranslations.ts +9 -0
- package/src/useContext/useContext.ts +36 -0
- package/src/useCountries/useCountries.test.ts +64 -0
- package/src/useCountries/useCountries.ts +64 -0
- package/src/useCustomerOrders/useCustomerOrders.test.ts +47 -0
- package/src/useCustomerOrders/useCustomerOrders.ts +89 -0
- package/src/useCustomerPassword/useCustomerPassword.test.ts +164 -0
- package/src/useCustomerPassword/useCustomerPassword.ts +57 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +41 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +45 -0
- package/src/useInternationalization/useInternationalization.test.ts +155 -0
- package/src/useInternationalization/useInternationalization.ts +159 -0
- package/src/useLandingSearch/useLandingSearch.test.ts +30 -0
- package/src/useLandingSearch/useLandingSearch.ts +58 -0
- package/src/useListing/useListing.test.ts +249 -0
- package/src/useListing/useListing.ts +592 -0
- package/src/useLocalWishlist/useLocalWishlist.test.ts +54 -0
- package/src/useLocalWishlist/useLocalWishlist.ts +95 -0
- package/src/useNavigation/useNavigation.test.ts +68 -0
- package/src/useNavigation/useNavigation.ts +83 -0
- package/src/useNavigationContext/useNavigationContext.test.ts +48 -0
- package/src/useNavigationContext/useNavigationContext.ts +39 -0
- package/src/useNavigationSearch/useNavigationSearch.test.ts +110 -0
- package/src/useNavigationSearch/useNavigationSearch.ts +60 -0
- package/src/useNewsletter/useNewsletter.test.ts +55 -0
- package/src/useNewsletter/useNewsletter.ts +109 -0
- package/src/useNotifications/useNotifications.test.ts +56 -0
- package/src/useNotifications/useNotifications.ts +119 -0
- package/src/useOrderDetails/useOrderDetails.test.ts +224 -0
- package/src/useOrderDetails/useOrderDetails.ts +329 -0
- package/src/useOrderPayment/useOrderPayment.test.ts +80 -0
- package/src/useOrderPayment/useOrderPayment.ts +119 -0
- package/src/usePrice/usePrice.test.ts +97 -0
- package/src/usePrice/usePrice.ts +106 -0
- package/src/useProduct/useProduct.test.ts +36 -0
- package/src/useProduct/useProduct.ts +56 -0
- package/src/useProductAssociations/useProductAssociations.spec.ts +105 -0
- package/src/useProductAssociations/useProductAssociations.ts +107 -0
- package/src/useProductConfigurator/useProductConfigurator.spec.ts +77 -0
- package/src/useProductConfigurator/useProductConfigurator.ts +134 -0
- package/src/useProductPrice/useProductPrice.test.ts +126 -0
- package/src/useProductPrice/useProductPrice.ts +131 -0
- package/src/useProductReviews/useProductReviews.test.ts +45 -0
- package/src/useProductReviews/useProductReviews.ts +80 -0
- package/src/useProductSearch/useProductSearch.test.ts +37 -0
- package/src/useProductSearch/useProductSearch.ts +58 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +19 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.ts +67 -0
- package/src/useProductWishlist/useProductWishlist.test.ts +63 -0
- package/src/useProductWishlist/useProductWishlist.ts +74 -0
- package/src/useSalutations/useSalutations.spec.ts +37 -0
- package/src/useSalutations/useSalutations.ts +55 -0
- package/src/useSessionContext/useSessionContext.test.ts +221 -0
- package/src/useSessionContext/useSessionContext.ts +281 -0
- package/src/useShopwareContext/useShopwareContext.test.ts +8 -0
- package/src/useShopwareContext/useShopwareContext.ts +31 -0
- package/src/useSyncWishlist/useSyncWishlist.test.ts +117 -0
- package/src/useSyncWishlist/useSyncWishlist.ts +117 -0
- package/src/useUrlResolver/useUrlResolver.test.ts +25 -0
- package/src/useUrlResolver/useUrlResolver.ts +31 -0
- package/src/useUser/useUser.test.ts +290 -0
- package/src/useUser/useUser.ts +321 -0
- package/src/useWishlist/useWishlist.test.ts +178 -0
- package/src/useWishlist/useWishlist.ts +128 -0
- package/types/api-types.ts +11 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2686 @@
|
|
|
1
|
+
import { pascalCase } from 'scule';
|
|
2
|
+
import { computed, effectScope, reactive, markRaw, inject, ref, provide, unref, onMounted, watch, resolveComponent } from 'vue';
|
|
3
|
+
import { urlIsAbsolute, relativeUrlSlash, getSrcSetForMedia, getMainImageUrl, getTranslatedProperty, getListingFilters, getProductTierPrices, buildUrlPrefix } from '@shopware-pwa/helpers-next';
|
|
4
|
+
import { ApiClientError } from '@shopware/api-client';
|
|
5
|
+
import { createSharedComposable, injectLocal, provideLocal, createInjectionState, syncRefs } from '@vueuse/core';
|
|
6
|
+
import { defu } from 'defu';
|
|
7
|
+
|
|
8
|
+
function useCmsElementImage(element) {
|
|
9
|
+
const { getConfigValue } = useCmsElementConfig(element);
|
|
10
|
+
const containerStyle = computed(() => ({
|
|
11
|
+
minHeight: getConfigValue("minHeight")
|
|
12
|
+
}));
|
|
13
|
+
const anchorAttrs = computed(() => ({
|
|
14
|
+
href: getConfigValue("url"),
|
|
15
|
+
target: getConfigValue("newTab") ? "_blank" : "_self"
|
|
16
|
+
}));
|
|
17
|
+
const imageLink = computed(() => ({
|
|
18
|
+
newTab: element.data?.newTab,
|
|
19
|
+
url: element.data?.url
|
|
20
|
+
}));
|
|
21
|
+
const imageContainerAttrs = computed(() => {
|
|
22
|
+
const attr = {};
|
|
23
|
+
if (imageLink.value.url) {
|
|
24
|
+
attr.href = urlIsAbsolute(imageLink.value.url) ? imageLink.value.url : relativeUrlSlash(imageLink.value.url);
|
|
25
|
+
}
|
|
26
|
+
if (imageLink.value.newTab) {
|
|
27
|
+
attr.target = "blank";
|
|
28
|
+
attr.rel = "noopener noreferrer";
|
|
29
|
+
}
|
|
30
|
+
return attr;
|
|
31
|
+
});
|
|
32
|
+
const imageAttrs = computed(() => ({
|
|
33
|
+
src: element.data?.media?.url,
|
|
34
|
+
alt: element.data?.media?.alt || "",
|
|
35
|
+
srcset: getSrcSetForMedia(element.data?.media)
|
|
36
|
+
}));
|
|
37
|
+
const displayMode = computed(
|
|
38
|
+
() => getConfigValue("displayMode") || "initial"
|
|
39
|
+
);
|
|
40
|
+
const isVideoElement = computed(() => {
|
|
41
|
+
return !!element.data?.media?.mimeType?.includes("video");
|
|
42
|
+
});
|
|
43
|
+
const mimeType = computed(() => {
|
|
44
|
+
return element.data?.media?.mimeType;
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
containerStyle,
|
|
48
|
+
anchorAttrs,
|
|
49
|
+
imageAttrs,
|
|
50
|
+
imageContainerAttrs,
|
|
51
|
+
imageLink,
|
|
52
|
+
displayMode,
|
|
53
|
+
isVideoElement,
|
|
54
|
+
mimeType
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function useCmsElementConfig(element) {
|
|
59
|
+
const getConfigValue = (key) => {
|
|
60
|
+
return element.config[key]?.source !== "mapped" && element.config[key]?.value;
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
getConfigValue
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function createShopwareContext(app, options) {
|
|
68
|
+
const scope = effectScope(true);
|
|
69
|
+
const state = scope.run(() => {
|
|
70
|
+
return reactive({
|
|
71
|
+
interceptors: {}
|
|
72
|
+
// sharedStore: options.initialStore || reactive({}),
|
|
73
|
+
// shopwareDefaults: options.shopwareDefaults || {},
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
const shopwarePlugin = markRaw({
|
|
77
|
+
install(app2) {
|
|
78
|
+
shopwarePlugin._a = app2;
|
|
79
|
+
app2.config.globalProperties.$shopware = shopwarePlugin;
|
|
80
|
+
app2.provide("shopware", shopwarePlugin);
|
|
81
|
+
},
|
|
82
|
+
_a: app,
|
|
83
|
+
_e: scope,
|
|
84
|
+
devStorefrontUrl: options.devStorefrontUrl,
|
|
85
|
+
state
|
|
86
|
+
});
|
|
87
|
+
if (options?.enableDevtools && typeof window !== "undefined") ;
|
|
88
|
+
return shopwarePlugin;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function useAddress() {
|
|
92
|
+
const { apiClient } = useShopwareContext();
|
|
93
|
+
const _storeCustomerAddresses = inject(
|
|
94
|
+
"swCustomerAddresses",
|
|
95
|
+
ref([])
|
|
96
|
+
);
|
|
97
|
+
provide("swCustomerAddresses", _storeCustomerAddresses);
|
|
98
|
+
async function loadCustomerAddresses(parameters = {}) {
|
|
99
|
+
try {
|
|
100
|
+
const result = await apiClient.invoke(
|
|
101
|
+
"listAddress post /account/list-address",
|
|
102
|
+
{
|
|
103
|
+
body: parameters
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
_storeCustomerAddresses.value = result.data.elements;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (error instanceof ApiClientError) {
|
|
109
|
+
if (error.status === 403) {
|
|
110
|
+
_storeCustomerAddresses.value = [];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
return _storeCustomerAddresses.value;
|
|
116
|
+
}
|
|
117
|
+
async function createCustomerAddress(customerAddress) {
|
|
118
|
+
const result = await apiClient.invoke(
|
|
119
|
+
"createCustomerAddress post /account/address",
|
|
120
|
+
{
|
|
121
|
+
body: customerAddress
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
return result.data;
|
|
125
|
+
}
|
|
126
|
+
async function updateCustomerAddress(customerAddress) {
|
|
127
|
+
const result = await apiClient.invoke(
|
|
128
|
+
"updateCustomerAddress patch /account/address/{addressId}",
|
|
129
|
+
{
|
|
130
|
+
pathParams: {
|
|
131
|
+
addressId: customerAddress.id
|
|
132
|
+
},
|
|
133
|
+
body: customerAddress
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
return result.data;
|
|
137
|
+
}
|
|
138
|
+
async function deleteCustomerAddress(addressId) {
|
|
139
|
+
await apiClient.invoke(
|
|
140
|
+
"deleteCustomerAddress delete /account/address/{addressId}",
|
|
141
|
+
{ pathParams: { addressId } }
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
async function setDefaultCustomerBillingAddress(addressId) {
|
|
145
|
+
const result = await apiClient.invoke(
|
|
146
|
+
"defaultBillingAddress patch /account/address/default-billing/{addressId}",
|
|
147
|
+
{
|
|
148
|
+
pathParams: {
|
|
149
|
+
addressId
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
return result.data;
|
|
154
|
+
}
|
|
155
|
+
async function setDefaultCustomerShippingAddress(addressId) {
|
|
156
|
+
const result = await apiClient.invoke(
|
|
157
|
+
"defaultShippingAddress patch /account/address/default-shipping/{addressId}",
|
|
158
|
+
{
|
|
159
|
+
pathParams: { addressId }
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
return result.data;
|
|
163
|
+
}
|
|
164
|
+
function errorMessageBuilder(error) {
|
|
165
|
+
switch (error.code) {
|
|
166
|
+
case "VIOLATION::IS_BLANK_ERROR":
|
|
167
|
+
return `${error?.source?.pointer?.slice(1)} - ${error.detail}`;
|
|
168
|
+
default:
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
customerAddresses: computed(() => _storeCustomerAddresses.value || []),
|
|
174
|
+
loadCustomerAddresses,
|
|
175
|
+
createCustomerAddress,
|
|
176
|
+
updateCustomerAddress,
|
|
177
|
+
deleteCustomerAddress,
|
|
178
|
+
setDefaultCustomerBillingAddress,
|
|
179
|
+
setDefaultCustomerShippingAddress,
|
|
180
|
+
errorMessageBuilder
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function useAddToCart(product) {
|
|
185
|
+
const _product = computed(() => unref(product));
|
|
186
|
+
const { addProduct, cartItems } = useCart();
|
|
187
|
+
const quantity = ref(1);
|
|
188
|
+
async function addToCart() {
|
|
189
|
+
if (!_product.value?.id)
|
|
190
|
+
throw new Error("Product id is required");
|
|
191
|
+
const addToCartResponse = await addProduct({
|
|
192
|
+
id: _product.value?.id,
|
|
193
|
+
quantity: quantity.value
|
|
194
|
+
});
|
|
195
|
+
quantity.value = 1;
|
|
196
|
+
return addToCartResponse;
|
|
197
|
+
}
|
|
198
|
+
const getStock = computed(() => _product.value?.stock);
|
|
199
|
+
const getAvailableStock = computed(() => _product.value?.availableStock);
|
|
200
|
+
const isInCart = computed(
|
|
201
|
+
() => cartItems.value.some(
|
|
202
|
+
(item) => item.referencedId === _product.value?.id
|
|
203
|
+
)
|
|
204
|
+
);
|
|
205
|
+
return {
|
|
206
|
+
addToCart,
|
|
207
|
+
quantity,
|
|
208
|
+
getStock,
|
|
209
|
+
getAvailableStock,
|
|
210
|
+
isInCart,
|
|
211
|
+
count: computed(
|
|
212
|
+
() => cartItems.value.find(
|
|
213
|
+
(item) => item.referencedId === _product.value?.id
|
|
214
|
+
)?.quantity || 0
|
|
215
|
+
)
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function useBreadcrumbs(newBreadcrumbs) {
|
|
220
|
+
const _breadcrumbs = useContext("swBreadcrumb", {
|
|
221
|
+
replace: newBreadcrumbs
|
|
222
|
+
});
|
|
223
|
+
const clearBreadcrumbs = () => {
|
|
224
|
+
_breadcrumbs.value = [];
|
|
225
|
+
};
|
|
226
|
+
const pushBreadcrumb = (breadcrumb) => {
|
|
227
|
+
if (_breadcrumbs.value)
|
|
228
|
+
_breadcrumbs.value.push(breadcrumb);
|
|
229
|
+
else
|
|
230
|
+
_breadcrumbs.value = [breadcrumb];
|
|
231
|
+
};
|
|
232
|
+
const buildDynamicBreadcrumbs = async (breadcrumbs) => {
|
|
233
|
+
_breadcrumbs.value = breadcrumbs.breadcrumbs.map((breadcrumb) => {
|
|
234
|
+
return {
|
|
235
|
+
...breadcrumb,
|
|
236
|
+
path: `/${breadcrumb.path}`
|
|
237
|
+
};
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
return {
|
|
241
|
+
clearBreadcrumbs,
|
|
242
|
+
breadcrumbs: computed(() => _breadcrumbs.value),
|
|
243
|
+
buildDynamicBreadcrumbs,
|
|
244
|
+
pushBreadcrumb
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function useCartFunction() {
|
|
249
|
+
const { apiClient } = useShopwareContext();
|
|
250
|
+
const _storeCart = useContext("swCart");
|
|
251
|
+
const _storeCartErrors = useContext(
|
|
252
|
+
"swCartErrors"
|
|
253
|
+
);
|
|
254
|
+
async function refreshCart(newCart) {
|
|
255
|
+
if (newCart) {
|
|
256
|
+
_storeCart.value = newCart;
|
|
257
|
+
return newCart;
|
|
258
|
+
}
|
|
259
|
+
const { data } = await apiClient.invoke("readCart get /checkout/cart");
|
|
260
|
+
_storeCart.value = data;
|
|
261
|
+
setCartErrors(data);
|
|
262
|
+
return data;
|
|
263
|
+
}
|
|
264
|
+
async function addProduct(params) {
|
|
265
|
+
return addProducts([
|
|
266
|
+
{
|
|
267
|
+
id: params.id,
|
|
268
|
+
quantity: params.quantity ?? 0,
|
|
269
|
+
type: "product"
|
|
270
|
+
}
|
|
271
|
+
]);
|
|
272
|
+
}
|
|
273
|
+
async function addProducts(items) {
|
|
274
|
+
const { data: addToCartResult } = await apiClient.invoke(
|
|
275
|
+
"addLineItem post /checkout/cart/line-item",
|
|
276
|
+
{
|
|
277
|
+
body: {
|
|
278
|
+
items
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
);
|
|
282
|
+
_storeCart.value = addToCartResult;
|
|
283
|
+
setCartErrors(addToCartResult);
|
|
284
|
+
return addToCartResult;
|
|
285
|
+
}
|
|
286
|
+
async function removeItem(lineItem) {
|
|
287
|
+
const { data } = await apiClient.invoke(
|
|
288
|
+
"removeLineItem post /checkout/cart/line-item/delete",
|
|
289
|
+
{
|
|
290
|
+
body: { ids: [lineItem.id] }
|
|
291
|
+
}
|
|
292
|
+
);
|
|
293
|
+
_storeCart.value = data;
|
|
294
|
+
setCartErrors(data);
|
|
295
|
+
return data;
|
|
296
|
+
}
|
|
297
|
+
async function changeProductQuantity(params) {
|
|
298
|
+
const { data } = await apiClient.invoke(
|
|
299
|
+
"updateLineItem patch /checkout/cart/line-item",
|
|
300
|
+
{
|
|
301
|
+
body: {
|
|
302
|
+
items: [
|
|
303
|
+
{
|
|
304
|
+
id: params.id,
|
|
305
|
+
quantity: +params.quantity
|
|
306
|
+
}
|
|
307
|
+
]
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
);
|
|
311
|
+
_storeCart.value = data;
|
|
312
|
+
setCartErrors(data);
|
|
313
|
+
return data;
|
|
314
|
+
}
|
|
315
|
+
async function submitPromotionCode(promotionCode) {
|
|
316
|
+
const { data } = await apiClient.invoke(
|
|
317
|
+
"addLineItem post /checkout/cart/line-item",
|
|
318
|
+
{
|
|
319
|
+
body: {
|
|
320
|
+
items: [
|
|
321
|
+
{
|
|
322
|
+
referencedId: promotionCode,
|
|
323
|
+
type: "promotion"
|
|
324
|
+
}
|
|
325
|
+
]
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
);
|
|
329
|
+
_storeCart.value = data;
|
|
330
|
+
setCartErrors(data);
|
|
331
|
+
return data;
|
|
332
|
+
}
|
|
333
|
+
const appliedPromotionCodes = computed(() => {
|
|
334
|
+
return cartItems.value.filter(
|
|
335
|
+
(cartItem) => cartItem.type === "promotion"
|
|
336
|
+
);
|
|
337
|
+
});
|
|
338
|
+
const cart = computed(
|
|
339
|
+
() => _storeCart.value
|
|
340
|
+
);
|
|
341
|
+
const cartItems = computed(() => {
|
|
342
|
+
return cart.value?.lineItems || [];
|
|
343
|
+
});
|
|
344
|
+
const count = computed(() => {
|
|
345
|
+
return cartItems.value.reduce(
|
|
346
|
+
(accumulator, lineItem) => lineItem.good === true ? lineItem.quantity + accumulator : accumulator,
|
|
347
|
+
0
|
|
348
|
+
);
|
|
349
|
+
});
|
|
350
|
+
const isEmpty = computed(() => count.value <= 0);
|
|
351
|
+
const totalPrice = computed(() => {
|
|
352
|
+
const cartPrice = cart.value?.price?.totalPrice;
|
|
353
|
+
return cartPrice || 0;
|
|
354
|
+
});
|
|
355
|
+
const shippingTotal = computed(() => {
|
|
356
|
+
const shippingTotal2 = cart.value?.deliveries?.[0]?.shippingCosts?.totalPrice;
|
|
357
|
+
return shippingTotal2 || 0;
|
|
358
|
+
});
|
|
359
|
+
const shippingCosts = computed(() => {
|
|
360
|
+
return cart.value?.deliveries || [];
|
|
361
|
+
});
|
|
362
|
+
const subtotal = computed(() => {
|
|
363
|
+
const cartPrice = cart.value?.price?.positionPrice;
|
|
364
|
+
return cartPrice || 0;
|
|
365
|
+
});
|
|
366
|
+
const isVirtualCart = computed(() => {
|
|
367
|
+
return cartItems.value.length > 0 && cartItems.value.filter((element) => element.type !== "promotion").every((item) => item.states.includes("is-download"));
|
|
368
|
+
});
|
|
369
|
+
const setCartErrors = (cart2) => {
|
|
370
|
+
if (Object.keys(cart2.errors || {}).length) {
|
|
371
|
+
_storeCartErrors.value = Object.assign(
|
|
372
|
+
_storeCartErrors.value ? _storeCartErrors.value : {},
|
|
373
|
+
cart2.errors
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
const consumeCartErrors = () => {
|
|
378
|
+
const errors = _storeCartErrors.value ? JSON.parse(JSON.stringify(_storeCartErrors.value)) : null;
|
|
379
|
+
_storeCartErrors.value = null;
|
|
380
|
+
return errors;
|
|
381
|
+
};
|
|
382
|
+
return {
|
|
383
|
+
addProduct,
|
|
384
|
+
addProducts,
|
|
385
|
+
addPromotionCode: submitPromotionCode,
|
|
386
|
+
appliedPromotionCodes,
|
|
387
|
+
cart,
|
|
388
|
+
cartItems,
|
|
389
|
+
changeProductQuantity,
|
|
390
|
+
count,
|
|
391
|
+
refreshCart,
|
|
392
|
+
removeItem,
|
|
393
|
+
totalPrice,
|
|
394
|
+
shippingTotal,
|
|
395
|
+
subtotal,
|
|
396
|
+
isEmpty,
|
|
397
|
+
isVirtualCart,
|
|
398
|
+
consumeCartErrors,
|
|
399
|
+
shippingCosts
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const useCart = createSharedComposable(useCartFunction);
|
|
403
|
+
|
|
404
|
+
function useCartItem(cartItem) {
|
|
405
|
+
if (!cartItem) {
|
|
406
|
+
throw new Error("[useCartItem] mandatory cartItem argument is missing.");
|
|
407
|
+
}
|
|
408
|
+
const { changeProductQuantity, removeItem: cartRemoveItem } = useCart();
|
|
409
|
+
const itemQuantity = computed(() => cartItem.value.quantity);
|
|
410
|
+
const itemImageThumbnailUrl = computed(() => getMainImageUrl(cartItem.value));
|
|
411
|
+
const itemRegularPrice = computed(
|
|
412
|
+
() => cartItem.value?.price?.listPrice?.price || cartItem.value?.price?.unitPrice
|
|
413
|
+
);
|
|
414
|
+
const itemSpecialPrice = computed(
|
|
415
|
+
() => cartItem.value?.price?.listPrice?.price && cartItem.value?.price?.unitPrice
|
|
416
|
+
);
|
|
417
|
+
const itemTotalPrice = computed(() => cartItem.value.price?.totalPrice);
|
|
418
|
+
const itemOptions = computed(
|
|
419
|
+
() => cartItem.value.type === "product" && cartItem.value.payload?.options || []
|
|
420
|
+
);
|
|
421
|
+
const itemStock = computed(
|
|
422
|
+
() => cartItem.value.deliveryInformation.stock
|
|
423
|
+
);
|
|
424
|
+
const isDigital = computed(
|
|
425
|
+
() => !!cartItem.value.states?.includes("is-download")
|
|
426
|
+
);
|
|
427
|
+
const isStackable = computed(() => !!cartItem.value.stackable);
|
|
428
|
+
const itemType = computed(() => cartItem.value.type);
|
|
429
|
+
const isProduct = computed(() => cartItem.value.type === "product");
|
|
430
|
+
const isPromotion = computed(() => cartItem.value.type === "promotion");
|
|
431
|
+
const isRemovable = computed(() => !!cartItem.value.removable);
|
|
432
|
+
async function removeItem() {
|
|
433
|
+
return await cartRemoveItem(cartItem.value);
|
|
434
|
+
}
|
|
435
|
+
async function changeItemQuantity(quantity) {
|
|
436
|
+
const result = changeProductQuantity({
|
|
437
|
+
id: cartItem.value.id,
|
|
438
|
+
quantity: +quantity
|
|
439
|
+
});
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
changeItemQuantity,
|
|
444
|
+
removeItem,
|
|
445
|
+
itemRegularPrice,
|
|
446
|
+
itemSpecialPrice,
|
|
447
|
+
itemTotalPrice,
|
|
448
|
+
itemOptions,
|
|
449
|
+
itemStock,
|
|
450
|
+
itemQuantity,
|
|
451
|
+
itemType,
|
|
452
|
+
itemImageThumbnailUrl,
|
|
453
|
+
isProduct,
|
|
454
|
+
isPromotion,
|
|
455
|
+
isRemovable,
|
|
456
|
+
isStackable,
|
|
457
|
+
isDigital
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
class ContextError extends Error {
|
|
462
|
+
constructor(scope, message) {
|
|
463
|
+
super(`${scope} ${message ? message : "context is not provided"}`);
|
|
464
|
+
this.name = "ContextError";
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function useCategory(category) {
|
|
469
|
+
const _category = useContext("category", { context: category });
|
|
470
|
+
if (!_category.value) {
|
|
471
|
+
throw new ContextError("Category");
|
|
472
|
+
}
|
|
473
|
+
return {
|
|
474
|
+
category: computed(() => _category.value)
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const cmsAssociations = {
|
|
479
|
+
associations: {
|
|
480
|
+
media: {},
|
|
481
|
+
cmsPage: {
|
|
482
|
+
associations: {
|
|
483
|
+
sections: {
|
|
484
|
+
associations: {
|
|
485
|
+
blocks: {
|
|
486
|
+
associations: {
|
|
487
|
+
slots: {
|
|
488
|
+
associations: {
|
|
489
|
+
block: {
|
|
490
|
+
associations: {
|
|
491
|
+
slots: {
|
|
492
|
+
associations: {}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
function useCategorySearch() {
|
|
508
|
+
const { apiClient } = useShopwareContext();
|
|
509
|
+
async function search(categoryId, options) {
|
|
510
|
+
const associations = options?.withCmsAssociations ? cmsAssociations : {};
|
|
511
|
+
const result = await apiClient.invoke(
|
|
512
|
+
"readCategory post /category/{navigationId}",
|
|
513
|
+
{
|
|
514
|
+
pathParams: {
|
|
515
|
+
navigationId: categoryId
|
|
516
|
+
},
|
|
517
|
+
headers: {
|
|
518
|
+
"sw-include-seo-urls": true
|
|
519
|
+
},
|
|
520
|
+
body: {
|
|
521
|
+
associations,
|
|
522
|
+
...options?.query
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
);
|
|
526
|
+
return result.data;
|
|
527
|
+
}
|
|
528
|
+
async function advancedSearch(options) {
|
|
529
|
+
const associations = options?.withCmsAssociations ? cmsAssociations.associations : {};
|
|
530
|
+
const result = await apiClient.invoke("readCategoryList post /category", {
|
|
531
|
+
body: {
|
|
532
|
+
associations,
|
|
533
|
+
...options?.query
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
return result.data.elements ?? [];
|
|
537
|
+
}
|
|
538
|
+
return {
|
|
539
|
+
search,
|
|
540
|
+
advancedSearch
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const shippingMethodsAssociations = {
|
|
545
|
+
associations: {
|
|
546
|
+
prices: {}
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
function useCheckout() {
|
|
550
|
+
const { apiClient } = useShopwareContext();
|
|
551
|
+
const {
|
|
552
|
+
sessionContext,
|
|
553
|
+
selectedPaymentMethod,
|
|
554
|
+
selectedShippingMethod,
|
|
555
|
+
setShippingMethod,
|
|
556
|
+
setPaymentMethod
|
|
557
|
+
} = useSessionContext();
|
|
558
|
+
const storeShippingMethods = inject("swShippingMethods", ref());
|
|
559
|
+
provide("swShippingMethods", storeShippingMethods);
|
|
560
|
+
const storePaymentMethods = inject("swPaymentMethods", ref());
|
|
561
|
+
provide("swPaymentMethods", storePaymentMethods);
|
|
562
|
+
const shippingMethods = computed(() => storeShippingMethods.value || []);
|
|
563
|
+
const paymentMethods = computed(() => storePaymentMethods.value || []);
|
|
564
|
+
async function getShippingMethods({ forceReload } = { forceReload: false }, associations = {}) {
|
|
565
|
+
if (shippingMethods.value.length && !forceReload)
|
|
566
|
+
return shippingMethods;
|
|
567
|
+
const mergedAssociations = defu(
|
|
568
|
+
shippingMethodsAssociations,
|
|
569
|
+
associations
|
|
570
|
+
);
|
|
571
|
+
const response = await apiClient.invoke(
|
|
572
|
+
"readShippingMethod post /shipping-method",
|
|
573
|
+
{
|
|
574
|
+
body: {
|
|
575
|
+
...mergedAssociations
|
|
576
|
+
},
|
|
577
|
+
query: {
|
|
578
|
+
onlyAvailable: true
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
);
|
|
582
|
+
storeShippingMethods.value = response.data.elements?.sort(
|
|
583
|
+
(a, b) => (a.position ?? 0) - (b.position ?? 0)
|
|
584
|
+
) || [];
|
|
585
|
+
return shippingMethods;
|
|
586
|
+
}
|
|
587
|
+
async function getPaymentMethods({ forceReload } = { forceReload: false }) {
|
|
588
|
+
if (paymentMethods.value.length && !forceReload)
|
|
589
|
+
return paymentMethods;
|
|
590
|
+
const response = await apiClient.invoke(
|
|
591
|
+
"readPaymentMethod post /payment-method",
|
|
592
|
+
{
|
|
593
|
+
body: { onlyAvailable: true }
|
|
594
|
+
}
|
|
595
|
+
);
|
|
596
|
+
storePaymentMethods.value = response.data.elements || [];
|
|
597
|
+
return paymentMethods;
|
|
598
|
+
}
|
|
599
|
+
async function createOrder(params = {}) {
|
|
600
|
+
const order = await apiClient.invoke("createOrder post /checkout/order", {
|
|
601
|
+
body: params
|
|
602
|
+
});
|
|
603
|
+
return order.data;
|
|
604
|
+
}
|
|
605
|
+
const shippingAddress = computed(
|
|
606
|
+
() => sessionContext.value?.shippingLocation?.address
|
|
607
|
+
);
|
|
608
|
+
const billingAddress = computed(
|
|
609
|
+
() => sessionContext.value?.customer?.activeBillingAddress
|
|
610
|
+
);
|
|
611
|
+
return {
|
|
612
|
+
getPaymentMethods,
|
|
613
|
+
paymentMethods,
|
|
614
|
+
getShippingMethods,
|
|
615
|
+
shippingMethods,
|
|
616
|
+
createOrder,
|
|
617
|
+
shippingAddress,
|
|
618
|
+
billingAddress,
|
|
619
|
+
selectedShippingMethod,
|
|
620
|
+
setShippingMethod,
|
|
621
|
+
selectedPaymentMethod,
|
|
622
|
+
setPaymentMethod
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function useCmsBlock(content) {
|
|
627
|
+
function getSlotContent(slotName) {
|
|
628
|
+
return content.slots.find((slot) => slot.slot === slotName);
|
|
629
|
+
}
|
|
630
|
+
return {
|
|
631
|
+
block: content,
|
|
632
|
+
getSlotContent
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function useCmsMeta(entity) {
|
|
637
|
+
const meta = computed(() => {
|
|
638
|
+
const entries = [];
|
|
639
|
+
const keywords = getTranslatedProperty(entity, "keywords");
|
|
640
|
+
const description = getTranslatedProperty(entity, "metaDescription");
|
|
641
|
+
const title = getTranslatedProperty(entity, "metaTitle");
|
|
642
|
+
if (keywords) {
|
|
643
|
+
entries.push({ name: "keywords", content: keywords });
|
|
644
|
+
}
|
|
645
|
+
if (description) {
|
|
646
|
+
entries.push({ name: "description", content: description });
|
|
647
|
+
}
|
|
648
|
+
if (title) {
|
|
649
|
+
entries.push({ name: "title", content: title });
|
|
650
|
+
}
|
|
651
|
+
return entries;
|
|
652
|
+
});
|
|
653
|
+
return {
|
|
654
|
+
title: computed(() => getTranslatedProperty(entity, "name")),
|
|
655
|
+
meta
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function useCmsSection(content) {
|
|
660
|
+
function getPositionContent(position) {
|
|
661
|
+
return content.blocks.filter(
|
|
662
|
+
(block) => block.sectionPosition === position
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
return {
|
|
666
|
+
section: content,
|
|
667
|
+
getPositionContent
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function useCmsTranslations() {
|
|
672
|
+
return inject("cmsTranslations", {});
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function useContext(injectionName, params) {
|
|
676
|
+
const isNewContext = !!params?.context;
|
|
677
|
+
const _context = isNewContext ? ref(unref(params?.context)) : injectLocal(injectionName, ref());
|
|
678
|
+
provideLocal(injectionName, _context);
|
|
679
|
+
if (params?.replace) {
|
|
680
|
+
_context.value = unref(params.replace);
|
|
681
|
+
}
|
|
682
|
+
return _context;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
function useCountries() {
|
|
686
|
+
const { apiClient } = useShopwareContext();
|
|
687
|
+
const _sharedCountries = inject("swCountries", ref());
|
|
688
|
+
provide("swCountries", _sharedCountries);
|
|
689
|
+
async function fetchCountries() {
|
|
690
|
+
const result = await apiClient.invoke("readCountry post /country", {
|
|
691
|
+
body: {
|
|
692
|
+
associations: {
|
|
693
|
+
states: {}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
_sharedCountries.value = result.data.elements;
|
|
698
|
+
return result.data;
|
|
699
|
+
}
|
|
700
|
+
const getCountries = computed(() => {
|
|
701
|
+
return _sharedCountries.value ?? [];
|
|
702
|
+
});
|
|
703
|
+
const mountedCallback = async () => {
|
|
704
|
+
if (!_sharedCountries.value) {
|
|
705
|
+
await fetchCountries();
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
const getStatesForCountry = (countryId) => {
|
|
709
|
+
return getCountries.value.find((element) => {
|
|
710
|
+
return element.id === countryId;
|
|
711
|
+
})?.states || null;
|
|
712
|
+
};
|
|
713
|
+
onMounted(mountedCallback);
|
|
714
|
+
return {
|
|
715
|
+
mountedCallback,
|
|
716
|
+
fetchCountries,
|
|
717
|
+
getStatesForCountry,
|
|
718
|
+
getCountries
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function useCustomerOrders() {
|
|
723
|
+
const { apiClient } = useShopwareContext();
|
|
724
|
+
const orders = ref([]);
|
|
725
|
+
const currentPaginationPage = ref(1);
|
|
726
|
+
const limit = ref(15);
|
|
727
|
+
const totalOrderItemsCount = ref(0);
|
|
728
|
+
const currentParams = ref({});
|
|
729
|
+
const loadOrders = async (parameters = {}) => {
|
|
730
|
+
const params = {
|
|
731
|
+
...parameters,
|
|
732
|
+
limit: limit.value
|
|
733
|
+
};
|
|
734
|
+
currentParams.value = params;
|
|
735
|
+
const fetchedOrders = await apiClient.invoke("readOrder post /order", {
|
|
736
|
+
body: { ...params, "total-count-mode": "exact" }
|
|
737
|
+
});
|
|
738
|
+
orders.value = fetchedOrders.data.orders.elements;
|
|
739
|
+
totalOrderItemsCount.value = fetchedOrders.data.orders.total ?? 0;
|
|
740
|
+
currentPaginationPage.value = fetchedOrders.data.orders.page ?? 1;
|
|
741
|
+
};
|
|
742
|
+
const changeCurrentPage = async (pageNumber) => {
|
|
743
|
+
await loadOrders({ ...currentParams.value, page: pageNumber });
|
|
744
|
+
currentPaginationPage.value = pageNumber;
|
|
745
|
+
};
|
|
746
|
+
const currentPage = computed(() => currentPaginationPage.value);
|
|
747
|
+
const totalPages = computed(() => {
|
|
748
|
+
return Math.ceil(totalOrderItemsCount.value / limit.value);
|
|
749
|
+
});
|
|
750
|
+
return {
|
|
751
|
+
orders,
|
|
752
|
+
changeCurrentPage,
|
|
753
|
+
loadOrders,
|
|
754
|
+
currentPage,
|
|
755
|
+
totalPages,
|
|
756
|
+
limit
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
function useCustomerPassword() {
|
|
761
|
+
const { apiClient } = useShopwareContext();
|
|
762
|
+
async function updatePassword(updatePasswordData) {
|
|
763
|
+
const response = await apiClient.invoke(
|
|
764
|
+
"changePassword post /account/change-password",
|
|
765
|
+
{
|
|
766
|
+
body: updatePasswordData
|
|
767
|
+
}
|
|
768
|
+
);
|
|
769
|
+
return response.data;
|
|
770
|
+
}
|
|
771
|
+
async function resetPassword(resetPasswordData) {
|
|
772
|
+
const response = await apiClient.invoke(
|
|
773
|
+
"sendRecoveryMail post /account/recovery-password",
|
|
774
|
+
{ body: resetPasswordData }
|
|
775
|
+
);
|
|
776
|
+
return response.data;
|
|
777
|
+
}
|
|
778
|
+
return {
|
|
779
|
+
updatePassword,
|
|
780
|
+
resetPassword
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function useDefaultOrderAssociations() {
|
|
785
|
+
const orderAssociations = {
|
|
786
|
+
associations: {
|
|
787
|
+
stateMachineState: {},
|
|
788
|
+
lineItems: {
|
|
789
|
+
associations: {
|
|
790
|
+
cover: {},
|
|
791
|
+
downloads: {
|
|
792
|
+
associations: {
|
|
793
|
+
media: {}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
},
|
|
798
|
+
addresses: {},
|
|
799
|
+
deliveries: {
|
|
800
|
+
associations: {
|
|
801
|
+
shippingMethod: {},
|
|
802
|
+
shippingOrderAddress: {},
|
|
803
|
+
stateMachineState: {}
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
transactions: {
|
|
807
|
+
associations: {
|
|
808
|
+
paymentMethod: {},
|
|
809
|
+
stateMachineState: {}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
};
|
|
814
|
+
return orderAssociations;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
function useInternationalization(pathResolver) {
|
|
818
|
+
const { devStorefrontUrl } = useShopwareContext();
|
|
819
|
+
const { apiClient } = useShopwareContext();
|
|
820
|
+
const _storeLanguages = useContext("swLanguages");
|
|
821
|
+
const _storeCurrentLanguage = useContext(
|
|
822
|
+
"swLanguagesCurrentLanguage"
|
|
823
|
+
);
|
|
824
|
+
const _storeCurrentPrefix = useContext("swLanguagesCurrentPrefix");
|
|
825
|
+
function getStorefrontUrl() {
|
|
826
|
+
return devStorefrontUrl ?? window.location.origin ?? "";
|
|
827
|
+
}
|
|
828
|
+
async function getAvailableLanguages() {
|
|
829
|
+
const { data } = await apiClient.invoke("readLanguages post /language");
|
|
830
|
+
_storeLanguages.value = data.elements;
|
|
831
|
+
return data;
|
|
832
|
+
}
|
|
833
|
+
async function changeLanguage(languageId) {
|
|
834
|
+
const { data } = await apiClient.invoke("updateContext patch /context", {
|
|
835
|
+
body: { languageId }
|
|
836
|
+
});
|
|
837
|
+
return data;
|
|
838
|
+
}
|
|
839
|
+
function getLanguageCodeFromId(languageId) {
|
|
840
|
+
return _storeLanguages.value.find((element) => element?.id === languageId)?.translationCode?.code || "";
|
|
841
|
+
}
|
|
842
|
+
function getLanguageIdFromCode(languageCode) {
|
|
843
|
+
return _storeLanguages.value.find(
|
|
844
|
+
(element) => element.translationCode?.code === languageCode
|
|
845
|
+
)?.id || "";
|
|
846
|
+
}
|
|
847
|
+
function replaceToDevStorefront(url) {
|
|
848
|
+
const current = new URL(url);
|
|
849
|
+
return devStorefrontUrl ? url.replace(`${current.protocol}//${current.host}`, devStorefrontUrl) : url;
|
|
850
|
+
}
|
|
851
|
+
function formatLink(link) {
|
|
852
|
+
if (!pathResolver)
|
|
853
|
+
return link;
|
|
854
|
+
if (typeof link === "string") {
|
|
855
|
+
if (urlIsAbsolute(link))
|
|
856
|
+
return link;
|
|
857
|
+
return pathResolver(link);
|
|
858
|
+
}
|
|
859
|
+
if (link.path) {
|
|
860
|
+
link.path = pathResolver(link.path);
|
|
861
|
+
return link;
|
|
862
|
+
}
|
|
863
|
+
return link;
|
|
864
|
+
}
|
|
865
|
+
return {
|
|
866
|
+
getAvailableLanguages,
|
|
867
|
+
getStorefrontUrl,
|
|
868
|
+
changeLanguage,
|
|
869
|
+
getLanguageCodeFromId,
|
|
870
|
+
getLanguageIdFromCode,
|
|
871
|
+
replaceToDevStorefront,
|
|
872
|
+
formatLink,
|
|
873
|
+
languages: _storeLanguages,
|
|
874
|
+
currentLanguage: _storeCurrentLanguage,
|
|
875
|
+
currentPrefix: _storeCurrentPrefix
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function useLandingSearch() {
|
|
880
|
+
const { apiClient } = useShopwareContext();
|
|
881
|
+
const search = async (navigationId, options) => {
|
|
882
|
+
const associations = options?.withCmsAssociations ? cmsAssociations : {};
|
|
883
|
+
const result = await apiClient.invoke(
|
|
884
|
+
"readLandingPage post /landing-page/{landingPageId}",
|
|
885
|
+
{
|
|
886
|
+
pathParams: {
|
|
887
|
+
landingPageId: navigationId
|
|
888
|
+
},
|
|
889
|
+
body: {
|
|
890
|
+
associations
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
);
|
|
894
|
+
return result.data;
|
|
895
|
+
};
|
|
896
|
+
return {
|
|
897
|
+
search
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
function isObject(item) {
|
|
902
|
+
return item && typeof item === "object" && !Array.isArray(item);
|
|
903
|
+
}
|
|
904
|
+
function merge(target, ...sources) {
|
|
905
|
+
if (!sources.length)
|
|
906
|
+
return target;
|
|
907
|
+
const source = sources.shift();
|
|
908
|
+
if (source === void 0) {
|
|
909
|
+
return target;
|
|
910
|
+
}
|
|
911
|
+
if (isObject(target) && isObject(source)) {
|
|
912
|
+
for (const key in source) {
|
|
913
|
+
if (isObject(source[key])) {
|
|
914
|
+
if (!target[key])
|
|
915
|
+
Object.assign(target, { [key]: {} });
|
|
916
|
+
merge(target[key], source[key]);
|
|
917
|
+
} else {
|
|
918
|
+
Object.assign(target, { [key]: source[key] });
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
return merge(target, ...sources);
|
|
923
|
+
}
|
|
924
|
+
function useListing(params) {
|
|
925
|
+
const listingType = params?.listingType || "categoryListing";
|
|
926
|
+
let categoryId = params?.categoryId || null;
|
|
927
|
+
const { apiClient } = useShopwareContext();
|
|
928
|
+
let searchMethod;
|
|
929
|
+
if (listingType === "productSearchListing") {
|
|
930
|
+
searchMethod = async (searchCriteria) => {
|
|
931
|
+
const { data } = await apiClient.invoke("searchPage post /search", {
|
|
932
|
+
headers: {
|
|
933
|
+
"sw-include-seo-urls": true
|
|
934
|
+
},
|
|
935
|
+
body: searchCriteria
|
|
936
|
+
});
|
|
937
|
+
return data;
|
|
938
|
+
};
|
|
939
|
+
} else {
|
|
940
|
+
if (!categoryId) {
|
|
941
|
+
const { category } = useCategory();
|
|
942
|
+
categoryId = category.value?.id;
|
|
943
|
+
}
|
|
944
|
+
searchMethod = async (searchCriteria) => {
|
|
945
|
+
const { data } = await apiClient.invoke(
|
|
946
|
+
"readProductListing post /product-listing/{categoryId}",
|
|
947
|
+
{
|
|
948
|
+
headers: {
|
|
949
|
+
"sw-include-seo-urls": true
|
|
950
|
+
},
|
|
951
|
+
pathParams: {
|
|
952
|
+
categoryId
|
|
953
|
+
// null exception in useCategory,
|
|
954
|
+
},
|
|
955
|
+
body: searchCriteria
|
|
956
|
+
}
|
|
957
|
+
);
|
|
958
|
+
return data;
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
return createListingComposable({
|
|
962
|
+
listingKey: listingType,
|
|
963
|
+
searchMethod,
|
|
964
|
+
searchDefaults: params?.defaultSearchCriteria || {}
|
|
965
|
+
//getDefaults(),
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
const [_createCategoryListingContext, _categoryListingContext] = createInjectionState(
|
|
969
|
+
() => {
|
|
970
|
+
return useListing({ listingType: "categoryListing" });
|
|
971
|
+
},
|
|
972
|
+
{
|
|
973
|
+
injectionKey: "categoryListing"
|
|
974
|
+
}
|
|
975
|
+
);
|
|
976
|
+
const createCategoryListingContext = _createCategoryListingContext;
|
|
977
|
+
const useCategoryListing = () => {
|
|
978
|
+
const listingContext = _categoryListingContext();
|
|
979
|
+
if (!listingContext) {
|
|
980
|
+
throw new Error(
|
|
981
|
+
"[useCategoryListing] Please call `createCategoryListingContext` on the appropriate parent component"
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
return listingContext;
|
|
985
|
+
};
|
|
986
|
+
const useProductSearchListing = createSharedComposable(
|
|
987
|
+
() => useListing({ listingType: "productSearchListing" })
|
|
988
|
+
);
|
|
989
|
+
function createListingComposable({
|
|
990
|
+
searchMethod,
|
|
991
|
+
searchDefaults,
|
|
992
|
+
listingKey
|
|
993
|
+
}) {
|
|
994
|
+
const loading = ref(false);
|
|
995
|
+
const loadingMore = ref(false);
|
|
996
|
+
const _storeInitialListing = inject(`useListingInitial-${listingKey}`, ref(null));
|
|
997
|
+
provide(`useListingInitial-${listingKey}`, _storeInitialListing);
|
|
998
|
+
const _storeAppliedListing = inject(`useListingApplied-${listingKey}`, ref(null));
|
|
999
|
+
provide(`useListingApplied-${listingKey}`, _storeAppliedListing);
|
|
1000
|
+
const getInitialListing = computed(() => _storeInitialListing.value);
|
|
1001
|
+
const setInitialListing = async (initialListing) => {
|
|
1002
|
+
_storeInitialListing.value = initialListing;
|
|
1003
|
+
_storeAppliedListing.value = null;
|
|
1004
|
+
};
|
|
1005
|
+
const initSearch = async (criteria) => {
|
|
1006
|
+
loading.value = true;
|
|
1007
|
+
try {
|
|
1008
|
+
const searchCriteria = merge(
|
|
1009
|
+
{},
|
|
1010
|
+
searchDefaults,
|
|
1011
|
+
criteria
|
|
1012
|
+
);
|
|
1013
|
+
const result = await searchMethod(searchCriteria);
|
|
1014
|
+
return result;
|
|
1015
|
+
} finally {
|
|
1016
|
+
loading.value = false;
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
async function search(criteria) {
|
|
1020
|
+
loading.value = true;
|
|
1021
|
+
try {
|
|
1022
|
+
const searchCriteria = merge(
|
|
1023
|
+
{},
|
|
1024
|
+
searchDefaults,
|
|
1025
|
+
criteria
|
|
1026
|
+
);
|
|
1027
|
+
const result = await searchMethod(searchCriteria);
|
|
1028
|
+
_storeAppliedListing.value = result;
|
|
1029
|
+
} finally {
|
|
1030
|
+
loading.value = false;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
const loadMore = async (criteria) => {
|
|
1034
|
+
loadingMore.value = true;
|
|
1035
|
+
try {
|
|
1036
|
+
const q = criteria ? criteria : {
|
|
1037
|
+
// ...router.currentRoute.query,
|
|
1038
|
+
p: getCurrentPage.value + 1
|
|
1039
|
+
};
|
|
1040
|
+
const searchCriteria = merge(
|
|
1041
|
+
{},
|
|
1042
|
+
searchDefaults,
|
|
1043
|
+
q
|
|
1044
|
+
);
|
|
1045
|
+
const result = await searchMethod(searchCriteria);
|
|
1046
|
+
_storeAppliedListing.value = {
|
|
1047
|
+
...getCurrentListing.value || {},
|
|
1048
|
+
page: result.page,
|
|
1049
|
+
elements: [
|
|
1050
|
+
...getCurrentListing.value?.elements || [],
|
|
1051
|
+
...result.elements ?? []
|
|
1052
|
+
]
|
|
1053
|
+
};
|
|
1054
|
+
} finally {
|
|
1055
|
+
loadingMore.value = false;
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
const getCurrentListing = computed(() => {
|
|
1059
|
+
return _storeAppliedListing.value || getInitialListing.value;
|
|
1060
|
+
});
|
|
1061
|
+
const getElements = computed(() => {
|
|
1062
|
+
return getCurrentListing.value?.elements || [];
|
|
1063
|
+
});
|
|
1064
|
+
const getTotal = computed(() => {
|
|
1065
|
+
return getCurrentListing.value?.total || 0;
|
|
1066
|
+
});
|
|
1067
|
+
const getLimit = computed(() => {
|
|
1068
|
+
return getCurrentListing.value?.limit || searchDefaults?.limit || 10;
|
|
1069
|
+
});
|
|
1070
|
+
const getTotalPagesCount = computed(
|
|
1071
|
+
() => Math.ceil(getTotal.value / getLimit.value)
|
|
1072
|
+
);
|
|
1073
|
+
const getSortingOrders = computed(() => {
|
|
1074
|
+
return getCurrentListing.value?.availableSortings;
|
|
1075
|
+
});
|
|
1076
|
+
const getCurrentSortingOrder = computed(
|
|
1077
|
+
() => getCurrentListing.value?.sorting
|
|
1078
|
+
);
|
|
1079
|
+
async function changeCurrentSortingOrder(order, query) {
|
|
1080
|
+
await search(
|
|
1081
|
+
Object.assign(
|
|
1082
|
+
{
|
|
1083
|
+
order
|
|
1084
|
+
},
|
|
1085
|
+
query
|
|
1086
|
+
)
|
|
1087
|
+
);
|
|
1088
|
+
}
|
|
1089
|
+
const getCurrentPage = computed(() => getCurrentListing.value?.page || 1);
|
|
1090
|
+
const changeCurrentPage = async (page, query) => {
|
|
1091
|
+
await search(
|
|
1092
|
+
Object.assign(
|
|
1093
|
+
{
|
|
1094
|
+
page
|
|
1095
|
+
},
|
|
1096
|
+
query
|
|
1097
|
+
)
|
|
1098
|
+
);
|
|
1099
|
+
};
|
|
1100
|
+
const getInitialFilters = computed(() => {
|
|
1101
|
+
return getListingFilters(getInitialListing.value?.aggregations);
|
|
1102
|
+
});
|
|
1103
|
+
const getAvailableFilters = computed(() => {
|
|
1104
|
+
return getListingFilters(
|
|
1105
|
+
_storeAppliedListing.value?.aggregations || getCurrentListing.value?.aggregations
|
|
1106
|
+
);
|
|
1107
|
+
});
|
|
1108
|
+
const getCurrentFilters = computed(() => {
|
|
1109
|
+
return getCurrentListing.value?.currentFilters;
|
|
1110
|
+
});
|
|
1111
|
+
const setCurrentFilters = (filters) => {
|
|
1112
|
+
const newFilters = {};
|
|
1113
|
+
for (const filter of filters) {
|
|
1114
|
+
Object.assign(newFilters, { [filter.code]: filter.value });
|
|
1115
|
+
}
|
|
1116
|
+
const appliedFilters = Object.assign(
|
|
1117
|
+
{},
|
|
1118
|
+
getCurrentFilters.value,
|
|
1119
|
+
{
|
|
1120
|
+
query: getCurrentFilters.value?.search,
|
|
1121
|
+
manufacturer: getCurrentFilters.value?.manufacturer?.join("|"),
|
|
1122
|
+
properties: getCurrentFilters.value?.properties?.join("|")
|
|
1123
|
+
},
|
|
1124
|
+
{ ...newFilters }
|
|
1125
|
+
);
|
|
1126
|
+
if (_storeAppliedListing.value) {
|
|
1127
|
+
_storeAppliedListing.value.currentFilters = {
|
|
1128
|
+
...appliedFilters,
|
|
1129
|
+
manufacturer: appliedFilters.manufacturer?.split("|"),
|
|
1130
|
+
properties: appliedFilters.properties?.split("|")
|
|
1131
|
+
};
|
|
1132
|
+
}
|
|
1133
|
+
return search(appliedFilters);
|
|
1134
|
+
};
|
|
1135
|
+
const resetFilters = () => {
|
|
1136
|
+
const defaultFilters = Object.assign(
|
|
1137
|
+
{
|
|
1138
|
+
manufacturer: [],
|
|
1139
|
+
properties: [],
|
|
1140
|
+
price: { min: 0, max: 0 },
|
|
1141
|
+
search: getCurrentFilters.value?.search
|
|
1142
|
+
},
|
|
1143
|
+
searchDefaults
|
|
1144
|
+
);
|
|
1145
|
+
if (_storeAppliedListing.value) {
|
|
1146
|
+
_storeAppliedListing.value.currentFilters = defaultFilters;
|
|
1147
|
+
}
|
|
1148
|
+
return search({ search: getCurrentFilters.value?.search || "" });
|
|
1149
|
+
};
|
|
1150
|
+
const filtersToQuery = (filters) => {
|
|
1151
|
+
const queryObject = {};
|
|
1152
|
+
for (const filter in filters) {
|
|
1153
|
+
const currentFilter = filters[filter];
|
|
1154
|
+
if (currentFilter) {
|
|
1155
|
+
if (Array.isArray(currentFilter) && currentFilter.length) {
|
|
1156
|
+
queryObject[filter] = currentFilter.join("|");
|
|
1157
|
+
} else if (!Array.isArray(currentFilter)) {
|
|
1158
|
+
queryObject[filter] = currentFilter;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
return queryObject;
|
|
1163
|
+
};
|
|
1164
|
+
return {
|
|
1165
|
+
changeCurrentPage,
|
|
1166
|
+
changeCurrentSortingOrder,
|
|
1167
|
+
filtersToQuery,
|
|
1168
|
+
getAvailableFilters,
|
|
1169
|
+
getCurrentFilters,
|
|
1170
|
+
getCurrentListing,
|
|
1171
|
+
getCurrentPage,
|
|
1172
|
+
getCurrentSortingOrder,
|
|
1173
|
+
getElements,
|
|
1174
|
+
getInitialFilters,
|
|
1175
|
+
getInitialListing,
|
|
1176
|
+
getLimit,
|
|
1177
|
+
getSortingOrders,
|
|
1178
|
+
getTotal,
|
|
1179
|
+
getTotalPagesCount,
|
|
1180
|
+
initSearch,
|
|
1181
|
+
loadMore,
|
|
1182
|
+
loading: computed(() => loading.value),
|
|
1183
|
+
loadingMore: computed(() => loadingMore.value),
|
|
1184
|
+
resetFilters,
|
|
1185
|
+
search,
|
|
1186
|
+
setCurrentFilters,
|
|
1187
|
+
setInitialListing
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
const _wishlistItems$1 = ref([]);
|
|
1192
|
+
function useLocalWishlist() {
|
|
1193
|
+
const updateStorage = () => {
|
|
1194
|
+
localStorage.setItem(
|
|
1195
|
+
"sw-wishlist-items",
|
|
1196
|
+
JSON.stringify(_wishlistItems$1.value)
|
|
1197
|
+
);
|
|
1198
|
+
};
|
|
1199
|
+
const getFromStorage = () => {
|
|
1200
|
+
if (typeof window !== "undefined" && localStorage) {
|
|
1201
|
+
return JSON.parse(localStorage.getItem("sw-wishlist-items") ?? "[]");
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
async function removeFromWishlist(id) {
|
|
1205
|
+
_wishlistItems$1.value = _wishlistItems$1.value?.filter(
|
|
1206
|
+
(itemId) => itemId !== id
|
|
1207
|
+
);
|
|
1208
|
+
updateStorage();
|
|
1209
|
+
}
|
|
1210
|
+
async function addToWishlist(id) {
|
|
1211
|
+
if (!_wishlistItems$1.value.includes(id)) {
|
|
1212
|
+
_wishlistItems$1.value.push(id);
|
|
1213
|
+
updateStorage();
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
async function clearWishlist() {
|
|
1217
|
+
_wishlistItems$1.value = [];
|
|
1218
|
+
updateStorage();
|
|
1219
|
+
}
|
|
1220
|
+
function getWishlistProducts() {
|
|
1221
|
+
const currentWishlist = getFromStorage();
|
|
1222
|
+
if (Array.isArray(currentWishlist) && currentWishlist.length) {
|
|
1223
|
+
_wishlistItems$1.value = currentWishlist;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
const items = computed(() => _wishlistItems$1.value);
|
|
1227
|
+
const count = computed(() => items.value.length);
|
|
1228
|
+
return {
|
|
1229
|
+
getWishlistProducts,
|
|
1230
|
+
addToWishlist,
|
|
1231
|
+
removeFromWishlist,
|
|
1232
|
+
clearWishlist,
|
|
1233
|
+
items,
|
|
1234
|
+
count
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
function useNavigation(params) {
|
|
1239
|
+
const type = params?.type || "main-navigation";
|
|
1240
|
+
const { apiClient } = useShopwareContext();
|
|
1241
|
+
const sharedElements = inject(
|
|
1242
|
+
`swNavigation-${type}`,
|
|
1243
|
+
ref([])
|
|
1244
|
+
);
|
|
1245
|
+
provide(`swNavigation-${type}`, sharedElements);
|
|
1246
|
+
const navigationElements = computed(() => sharedElements.value);
|
|
1247
|
+
async function loadNavigationElements(params2) {
|
|
1248
|
+
try {
|
|
1249
|
+
const navigationResponse = await apiClient.invoke(
|
|
1250
|
+
"readNavigation post /navigation/{activeId}/{rootId}",
|
|
1251
|
+
{
|
|
1252
|
+
headers: {
|
|
1253
|
+
"sw-include-seo-urls": true
|
|
1254
|
+
},
|
|
1255
|
+
pathParams: {
|
|
1256
|
+
activeId: type,
|
|
1257
|
+
rootId: type
|
|
1258
|
+
},
|
|
1259
|
+
body: params2
|
|
1260
|
+
}
|
|
1261
|
+
);
|
|
1262
|
+
sharedElements.value = navigationResponse.data || [];
|
|
1263
|
+
return sharedElements.value;
|
|
1264
|
+
} catch (e) {
|
|
1265
|
+
sharedElements.value = [];
|
|
1266
|
+
console.error("[useNavigation][loadNavigationElements]", e);
|
|
1267
|
+
return [];
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
return {
|
|
1271
|
+
navigationElements,
|
|
1272
|
+
loadNavigationElements
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
function useNavigationContext(context) {
|
|
1277
|
+
const _context = useContext("navigation", { context });
|
|
1278
|
+
const routeName = computed(() => _context.value?.routeName);
|
|
1279
|
+
const foreignKey = computed(() => _context.value?.foreignKey || "");
|
|
1280
|
+
return {
|
|
1281
|
+
navigationContext: computed(() => _context.value),
|
|
1282
|
+
routeName,
|
|
1283
|
+
foreignKey
|
|
1284
|
+
};
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
function useNavigationSearch() {
|
|
1288
|
+
const { apiClient } = useShopwareContext();
|
|
1289
|
+
const { sessionContext } = useSessionContext();
|
|
1290
|
+
async function resolvePath(path) {
|
|
1291
|
+
if (path === "/") {
|
|
1292
|
+
const categoryId = sessionContext.value?.salesChannel?.navigationCategoryId;
|
|
1293
|
+
return {
|
|
1294
|
+
routeName: "frontend.navigation.page",
|
|
1295
|
+
foreignKey: categoryId
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
const isTechnicalUrl = path.startsWith("/navigation/") || path.startsWith("/detail/") || path.startsWith("/landingPage/");
|
|
1299
|
+
const normalizedPath = isTechnicalUrl ? path : path.substring(1);
|
|
1300
|
+
const seoResult = await apiClient.invoke("readSeoUrl post /seo-url", {
|
|
1301
|
+
body: {
|
|
1302
|
+
filter: [
|
|
1303
|
+
{
|
|
1304
|
+
type: "equals",
|
|
1305
|
+
field: isTechnicalUrl ? "pathInfo" : "seoPathInfo",
|
|
1306
|
+
value: normalizedPath
|
|
1307
|
+
}
|
|
1308
|
+
]
|
|
1309
|
+
}
|
|
1310
|
+
});
|
|
1311
|
+
return seoResult.data.elements?.[0];
|
|
1312
|
+
}
|
|
1313
|
+
return {
|
|
1314
|
+
resolvePath
|
|
1315
|
+
};
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
function useNewsletter() {
|
|
1319
|
+
const { apiClient } = useShopwareContext();
|
|
1320
|
+
const { getStorefrontUrl } = useInternationalization();
|
|
1321
|
+
const newsletterStatus = ref("undefined");
|
|
1322
|
+
async function newsletterSubscribe(params) {
|
|
1323
|
+
const result = await apiClient.invoke(
|
|
1324
|
+
"subscribeToNewsletter post /newsletter/subscribe",
|
|
1325
|
+
{
|
|
1326
|
+
body: {
|
|
1327
|
+
...params,
|
|
1328
|
+
storefrontUrl: getStorefrontUrl()
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
);
|
|
1332
|
+
return result.data;
|
|
1333
|
+
}
|
|
1334
|
+
async function newsletterUnsubscribe(email) {
|
|
1335
|
+
await apiClient.invoke(
|
|
1336
|
+
"unsubscribeToNewsletter post /newsletter/unsubscribe",
|
|
1337
|
+
{
|
|
1338
|
+
body: { email }
|
|
1339
|
+
}
|
|
1340
|
+
);
|
|
1341
|
+
}
|
|
1342
|
+
async function getNewsletterStatus() {
|
|
1343
|
+
const response = await apiClient.invoke(
|
|
1344
|
+
"readNewsletterRecipient post /account/newsletter-recipient"
|
|
1345
|
+
);
|
|
1346
|
+
newsletterStatus.value = response.data.status;
|
|
1347
|
+
return response.data;
|
|
1348
|
+
}
|
|
1349
|
+
const isNewsletterSubscriber = computed(
|
|
1350
|
+
() => !["optOut", "undefined"].includes(newsletterStatus.value)
|
|
1351
|
+
);
|
|
1352
|
+
const confirmationNeeded = computed(
|
|
1353
|
+
() => newsletterStatus.value === "notSet"
|
|
1354
|
+
);
|
|
1355
|
+
return {
|
|
1356
|
+
newsletterSubscribe,
|
|
1357
|
+
newsletterUnsubscribe,
|
|
1358
|
+
isNewsletterSubscriber,
|
|
1359
|
+
getNewsletterStatus,
|
|
1360
|
+
newsletterStatus,
|
|
1361
|
+
confirmationNeeded
|
|
1362
|
+
};
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
function useNotifications() {
|
|
1366
|
+
const _notifications = inject(
|
|
1367
|
+
"swNotifications",
|
|
1368
|
+
ref([])
|
|
1369
|
+
);
|
|
1370
|
+
provide("swNotifications", _notifications);
|
|
1371
|
+
function removeOne(notificationId) {
|
|
1372
|
+
_notifications.value = _notifications.value?.filter(({ id }) => id !== notificationId) || [];
|
|
1373
|
+
}
|
|
1374
|
+
function removeAll() {
|
|
1375
|
+
_notifications.value = [];
|
|
1376
|
+
}
|
|
1377
|
+
function geterateId() {
|
|
1378
|
+
return +`${(/* @__PURE__ */ new Date()).getTime()}${Math.random() * 1e3}`;
|
|
1379
|
+
}
|
|
1380
|
+
async function pushNotification(message, options) {
|
|
1381
|
+
const timeout = options.timeout || 2500;
|
|
1382
|
+
const persistent = !!options.persistent;
|
|
1383
|
+
_notifications.value = _notifications.value || [];
|
|
1384
|
+
const messageId = geterateId();
|
|
1385
|
+
_notifications.value.push({
|
|
1386
|
+
id: messageId,
|
|
1387
|
+
type: options.type,
|
|
1388
|
+
message
|
|
1389
|
+
});
|
|
1390
|
+
if (!persistent) {
|
|
1391
|
+
setTimeout(() => {
|
|
1392
|
+
removeOne(messageId);
|
|
1393
|
+
}, timeout);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
return {
|
|
1397
|
+
removeOne,
|
|
1398
|
+
removeAll,
|
|
1399
|
+
pushInfo: (message, options = {}) => pushNotification(message, { ...options, type: "info" }),
|
|
1400
|
+
pushSuccess: (message, options = {}) => pushNotification(message, { ...options, type: "success" }),
|
|
1401
|
+
pushWarning: (message, options = {}) => pushNotification(message, { ...options, type: "warning" }),
|
|
1402
|
+
pushError: (message, options = {}) => pushNotification(message, { ...options, type: "danger" }),
|
|
1403
|
+
notifications: computed(() => _notifications.value || [])
|
|
1404
|
+
};
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
function useOrderDetails(orderId, associations) {
|
|
1408
|
+
const { apiClient } = useShopwareContext();
|
|
1409
|
+
const paymentChangeableList = ref({});
|
|
1410
|
+
const _sharedOrder = inject(
|
|
1411
|
+
"swOrderDetails",
|
|
1412
|
+
ref()
|
|
1413
|
+
);
|
|
1414
|
+
provide("swOrderDetails", _sharedOrder);
|
|
1415
|
+
const orderAssociations = useDefaultOrderAssociations();
|
|
1416
|
+
const paymentMethod = computed(
|
|
1417
|
+
() => _sharedOrder.value?.transactions?.length ? _sharedOrder.value.transactions[_sharedOrder.value.transactions.length - 1].paymentMethod : void 0
|
|
1418
|
+
);
|
|
1419
|
+
const shippingMethod = computed(
|
|
1420
|
+
() => _sharedOrder.value?.deliveries?.length ? _sharedOrder.value.deliveries[_sharedOrder.value.deliveries.length - 1].shippingMethod : void 0
|
|
1421
|
+
);
|
|
1422
|
+
const paymentUrl = ref();
|
|
1423
|
+
const personalDetails = computed(() => ({
|
|
1424
|
+
email: _sharedOrder.value?.orderCustomer?.email,
|
|
1425
|
+
firstName: _sharedOrder.value?.orderCustomer?.firstName,
|
|
1426
|
+
lastName: _sharedOrder.value?.orderCustomer?.lastName
|
|
1427
|
+
}));
|
|
1428
|
+
const billingAddress = computed(
|
|
1429
|
+
() => _sharedOrder.value?.addresses?.find(
|
|
1430
|
+
({ id }) => id === _sharedOrder.value?.billingAddressId
|
|
1431
|
+
)
|
|
1432
|
+
);
|
|
1433
|
+
const shippingAddress = computed(
|
|
1434
|
+
() => _sharedOrder.value?.deliveries?.[0]?.shippingOrderAddress
|
|
1435
|
+
);
|
|
1436
|
+
const shippingCosts = computed(() => _sharedOrder.value?.shippingTotal);
|
|
1437
|
+
const subtotal = computed(() => _sharedOrder.value?.price?.positionPrice);
|
|
1438
|
+
const total = computed(() => _sharedOrder.value?.price?.totalPrice);
|
|
1439
|
+
const status = computed(
|
|
1440
|
+
() => _sharedOrder.value?.stateMachineState?.translated.name
|
|
1441
|
+
);
|
|
1442
|
+
const statusTechnicalName = computed(
|
|
1443
|
+
() => _sharedOrder.value?.stateMachineState?.technicalName
|
|
1444
|
+
);
|
|
1445
|
+
async function loadOrderDetails() {
|
|
1446
|
+
const mergedAssociations = defu(
|
|
1447
|
+
orderAssociations,
|
|
1448
|
+
associations ? associations : {}
|
|
1449
|
+
);
|
|
1450
|
+
const params = {
|
|
1451
|
+
filter: [
|
|
1452
|
+
{
|
|
1453
|
+
type: "equals",
|
|
1454
|
+
field: "id",
|
|
1455
|
+
value: orderId
|
|
1456
|
+
}
|
|
1457
|
+
],
|
|
1458
|
+
associations: mergedAssociations.associations,
|
|
1459
|
+
checkPromotion: true
|
|
1460
|
+
};
|
|
1461
|
+
const orderDetailsResponse = await apiClient.invoke(
|
|
1462
|
+
"readOrder post /order",
|
|
1463
|
+
{
|
|
1464
|
+
body: params
|
|
1465
|
+
}
|
|
1466
|
+
);
|
|
1467
|
+
_sharedOrder.value = orderDetailsResponse.data.orders?.elements?.[0] ?? null;
|
|
1468
|
+
paymentChangeableList.value = orderDetailsResponse.data.paymentChangeable ?? {};
|
|
1469
|
+
return orderDetailsResponse.data;
|
|
1470
|
+
}
|
|
1471
|
+
async function handlePayment(finishUrl, errorUrl) {
|
|
1472
|
+
const resp = await apiClient.invoke(
|
|
1473
|
+
"handlePaymentMethod post /handle-payment",
|
|
1474
|
+
{
|
|
1475
|
+
body: {
|
|
1476
|
+
orderId,
|
|
1477
|
+
finishUrl,
|
|
1478
|
+
errorUrl
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
);
|
|
1482
|
+
paymentUrl.value = resp.data.redirectUrl;
|
|
1483
|
+
}
|
|
1484
|
+
async function cancel() {
|
|
1485
|
+
const resp = await apiClient.invoke(
|
|
1486
|
+
"cancelOrder post /order/state/cancel",
|
|
1487
|
+
{
|
|
1488
|
+
body: {
|
|
1489
|
+
orderId
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
);
|
|
1493
|
+
await loadOrderDetails();
|
|
1494
|
+
return resp.data;
|
|
1495
|
+
}
|
|
1496
|
+
async function changePaymentMethod(paymentMethodId) {
|
|
1497
|
+
const response = await apiClient.invoke(
|
|
1498
|
+
"orderSetPayment post /order/payment",
|
|
1499
|
+
{
|
|
1500
|
+
body: {
|
|
1501
|
+
orderId,
|
|
1502
|
+
paymentMethodId
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
);
|
|
1506
|
+
await loadOrderDetails();
|
|
1507
|
+
return response.data;
|
|
1508
|
+
}
|
|
1509
|
+
async function getMediaFile(downloadId) {
|
|
1510
|
+
const response = await apiClient.invoke(
|
|
1511
|
+
"orderDownloadFile get /order/download/{orderId}/{downloadId}",
|
|
1512
|
+
{
|
|
1513
|
+
accept: "application/octet-stream",
|
|
1514
|
+
pathParams: {
|
|
1515
|
+
orderId,
|
|
1516
|
+
downloadId
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
);
|
|
1520
|
+
return response.data;
|
|
1521
|
+
}
|
|
1522
|
+
async function getDocumentFile(documentId, deepLinkCode) {
|
|
1523
|
+
const response = await apiClient.invoke(
|
|
1524
|
+
"download post /document/download/{documentId}/{deepLinkCode}",
|
|
1525
|
+
{
|
|
1526
|
+
pathParams: {
|
|
1527
|
+
documentId,
|
|
1528
|
+
deepLinkCode
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
);
|
|
1532
|
+
return response.data;
|
|
1533
|
+
}
|
|
1534
|
+
const hasDocuments = computed(() => !!_sharedOrder.value?.documents.length);
|
|
1535
|
+
const documents = computed(() => _sharedOrder.value?.documents || []);
|
|
1536
|
+
const paymentChangeable = computed(() => {
|
|
1537
|
+
return Object.keys(paymentChangeableList.value).length ? paymentChangeableList.value[orderId] : false;
|
|
1538
|
+
});
|
|
1539
|
+
const getPaymentMethods = async () => {
|
|
1540
|
+
const response = await apiClient.invoke(
|
|
1541
|
+
"readPaymentMethod post /payment-method",
|
|
1542
|
+
{
|
|
1543
|
+
body: { onlyAvailable: true }
|
|
1544
|
+
}
|
|
1545
|
+
);
|
|
1546
|
+
return response.data.elements || [];
|
|
1547
|
+
};
|
|
1548
|
+
return {
|
|
1549
|
+
order: computed(() => _sharedOrder.value),
|
|
1550
|
+
status,
|
|
1551
|
+
statusTechnicalName,
|
|
1552
|
+
total,
|
|
1553
|
+
subtotal,
|
|
1554
|
+
shippingCosts,
|
|
1555
|
+
shippingAddress,
|
|
1556
|
+
billingAddress,
|
|
1557
|
+
personalDetails,
|
|
1558
|
+
paymentUrl,
|
|
1559
|
+
shippingMethod,
|
|
1560
|
+
paymentMethod,
|
|
1561
|
+
hasDocuments,
|
|
1562
|
+
documents,
|
|
1563
|
+
loadOrderDetails,
|
|
1564
|
+
handlePayment,
|
|
1565
|
+
cancel,
|
|
1566
|
+
changePaymentMethod,
|
|
1567
|
+
getMediaFile,
|
|
1568
|
+
getDocumentFile,
|
|
1569
|
+
paymentChangeable,
|
|
1570
|
+
getPaymentMethods
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
function useOrderPayment(order) {
|
|
1575
|
+
const { apiClient } = useShopwareContext();
|
|
1576
|
+
const activeTransaction = computed(
|
|
1577
|
+
() => order.value?.transactions?.find((t) => t.paymentMethod?.active === true)
|
|
1578
|
+
);
|
|
1579
|
+
const paymentMethod = computed(() => activeTransaction.value?.paymentMethod);
|
|
1580
|
+
const paymentUrl = ref();
|
|
1581
|
+
const state = computed(() => activeTransaction.value?.stateMachineState);
|
|
1582
|
+
const isAsynchronous = computed(
|
|
1583
|
+
() => activeTransaction.value?.paymentMethod?.asynchronous && activeTransaction.value?.paymentMethod?.afterOrderEnabled
|
|
1584
|
+
);
|
|
1585
|
+
async function handlePayment(finishUrl, errorUrl) {
|
|
1586
|
+
if (!order.value) {
|
|
1587
|
+
return;
|
|
1588
|
+
}
|
|
1589
|
+
const resp = await apiClient.invoke(
|
|
1590
|
+
"handlePaymentMethod post /handle-payment",
|
|
1591
|
+
{
|
|
1592
|
+
body: {
|
|
1593
|
+
orderId: order.value.id,
|
|
1594
|
+
errorUrl,
|
|
1595
|
+
finishUrl
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
);
|
|
1599
|
+
paymentUrl.value = resp.data.redirectUrl;
|
|
1600
|
+
return resp;
|
|
1601
|
+
}
|
|
1602
|
+
async function changePaymentMethod(paymentMethodId) {
|
|
1603
|
+
if (!order.value) {
|
|
1604
|
+
return;
|
|
1605
|
+
}
|
|
1606
|
+
const response = await apiClient.invoke(
|
|
1607
|
+
"orderSetPayment post /order/payment",
|
|
1608
|
+
{
|
|
1609
|
+
body: {
|
|
1610
|
+
orderId: order.value.id,
|
|
1611
|
+
paymentMethodId
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
);
|
|
1615
|
+
return response.data;
|
|
1616
|
+
}
|
|
1617
|
+
return {
|
|
1618
|
+
isAsynchronous,
|
|
1619
|
+
activeTransaction,
|
|
1620
|
+
state,
|
|
1621
|
+
paymentUrl,
|
|
1622
|
+
paymentMethod,
|
|
1623
|
+
handlePayment,
|
|
1624
|
+
changePaymentMethod
|
|
1625
|
+
};
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
function _usePrice(params) {
|
|
1629
|
+
const { sessionContext } = useSessionContext();
|
|
1630
|
+
const currencyLocale = ref();
|
|
1631
|
+
const currencyCode = ref("");
|
|
1632
|
+
if (params) {
|
|
1633
|
+
currencyCode.value = params.currencyCode;
|
|
1634
|
+
currencyLocale.value = params.localeCode;
|
|
1635
|
+
}
|
|
1636
|
+
function update(params2) {
|
|
1637
|
+
_setCurrencyCode(params2.currencyCode);
|
|
1638
|
+
_setLocaleCode(
|
|
1639
|
+
params2.localeCode || currencyLocale.value || typeof navigator !== "undefined" && navigator?.language || "en-US"
|
|
1640
|
+
);
|
|
1641
|
+
}
|
|
1642
|
+
function _setCurrencyCode(code) {
|
|
1643
|
+
currencyCode.value = code;
|
|
1644
|
+
}
|
|
1645
|
+
function _setLocaleCode(locale) {
|
|
1646
|
+
currencyLocale.value = locale;
|
|
1647
|
+
}
|
|
1648
|
+
function getFormattedPrice(value) {
|
|
1649
|
+
if (typeof value === "undefined") {
|
|
1650
|
+
return "";
|
|
1651
|
+
}
|
|
1652
|
+
if (!currencyLocale.value) {
|
|
1653
|
+
return value.toString();
|
|
1654
|
+
}
|
|
1655
|
+
return new Intl.NumberFormat(currencyLocale.value, {
|
|
1656
|
+
style: "currency",
|
|
1657
|
+
currency: currencyCode.value
|
|
1658
|
+
}).format(+value);
|
|
1659
|
+
}
|
|
1660
|
+
watch(
|
|
1661
|
+
() => sessionContext.value?.currency,
|
|
1662
|
+
(newCurrency) => {
|
|
1663
|
+
if (newCurrency)
|
|
1664
|
+
update({
|
|
1665
|
+
// locale code is read only once on SSR because it's unavailable in the context
|
|
1666
|
+
currencyCode: newCurrency?.isoCode
|
|
1667
|
+
});
|
|
1668
|
+
},
|
|
1669
|
+
{
|
|
1670
|
+
immediate: true
|
|
1671
|
+
}
|
|
1672
|
+
);
|
|
1673
|
+
return {
|
|
1674
|
+
getFormattedPrice,
|
|
1675
|
+
update,
|
|
1676
|
+
currencyCode: computed(() => currencyCode.value)
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
const usePrice = createSharedComposable(_usePrice);
|
|
1680
|
+
|
|
1681
|
+
function useProduct(product, configurator) {
|
|
1682
|
+
const _product = useContext("product", { context: product });
|
|
1683
|
+
if (!_product.value) {
|
|
1684
|
+
throw new ContextError("Product");
|
|
1685
|
+
}
|
|
1686
|
+
const _configurator = useContext("configurator", {
|
|
1687
|
+
context: product && configurator
|
|
1688
|
+
});
|
|
1689
|
+
function changeVariant(variant) {
|
|
1690
|
+
if (!variant) {
|
|
1691
|
+
console.warn("[useProduct][changeVariant]: Provided variant is empty");
|
|
1692
|
+
return;
|
|
1693
|
+
}
|
|
1694
|
+
_product.value = Object.assign({}, _product.value, variant);
|
|
1695
|
+
}
|
|
1696
|
+
return {
|
|
1697
|
+
product: computed(() => _product.value),
|
|
1698
|
+
configurator: computed(() => _configurator.value),
|
|
1699
|
+
changeVariant
|
|
1700
|
+
};
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
function useProductAssociations(product, options) {
|
|
1704
|
+
if (!product.value)
|
|
1705
|
+
throw new Error("[useProductAssociations]: Product is not provided.");
|
|
1706
|
+
options.associationContext;
|
|
1707
|
+
const { apiClient } = useShopwareContext();
|
|
1708
|
+
const isLoading = ref(false);
|
|
1709
|
+
const associations = ref([]);
|
|
1710
|
+
const loadAssociations = async () => {
|
|
1711
|
+
isLoading.value = true;
|
|
1712
|
+
try {
|
|
1713
|
+
const response = await apiClient.invoke(
|
|
1714
|
+
"readProductCrossSellings post /product/{productId}/cross-selling",
|
|
1715
|
+
{
|
|
1716
|
+
pathParams: { productId: product.value.id }
|
|
1717
|
+
}
|
|
1718
|
+
);
|
|
1719
|
+
associations.value = response.data;
|
|
1720
|
+
} catch (error) {
|
|
1721
|
+
console.error(
|
|
1722
|
+
"[useProductAssociations][loadAssociations][error]:",
|
|
1723
|
+
error
|
|
1724
|
+
);
|
|
1725
|
+
} finally {
|
|
1726
|
+
isLoading.value = false;
|
|
1727
|
+
}
|
|
1728
|
+
};
|
|
1729
|
+
return {
|
|
1730
|
+
isLoading: computed(() => isLoading.value),
|
|
1731
|
+
productAssociations: computed(() => associations.value),
|
|
1732
|
+
loadAssociations
|
|
1733
|
+
};
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
function useProductConfigurator() {
|
|
1737
|
+
const { apiClient } = useShopwareContext();
|
|
1738
|
+
const { configurator, product } = useProduct();
|
|
1739
|
+
const selected = ref({});
|
|
1740
|
+
const isLoadingOptions = ref(!!product.value.options?.length);
|
|
1741
|
+
const parentProductId = computed(() => product.value?.parentId);
|
|
1742
|
+
const getOptionGroups = computed(() => {
|
|
1743
|
+
return configurator.value || [];
|
|
1744
|
+
});
|
|
1745
|
+
const findGroupCodeForOption = (optionId) => {
|
|
1746
|
+
const group = getOptionGroups.value.find((optionGroup) => {
|
|
1747
|
+
const optionFound = optionGroup.options?.find((option) => {
|
|
1748
|
+
return option.id === optionId;
|
|
1749
|
+
});
|
|
1750
|
+
return !!optionFound;
|
|
1751
|
+
});
|
|
1752
|
+
return getTranslatedProperty(group, "name");
|
|
1753
|
+
};
|
|
1754
|
+
for (const optionId of product.value.optionIds || []) {
|
|
1755
|
+
const optionGroupCode = findGroupCodeForOption(optionId);
|
|
1756
|
+
if (optionGroupCode) {
|
|
1757
|
+
selected.value[optionGroupCode] = optionId;
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
async function findVariantForSelectedOptions(options) {
|
|
1761
|
+
const filter = [
|
|
1762
|
+
{
|
|
1763
|
+
type: "equals",
|
|
1764
|
+
field: "parentId",
|
|
1765
|
+
value: parentProductId.value
|
|
1766
|
+
},
|
|
1767
|
+
...Object.values(options || selected.value).map(
|
|
1768
|
+
(id) => ({
|
|
1769
|
+
type: "equals",
|
|
1770
|
+
field: "optionIds",
|
|
1771
|
+
value: id
|
|
1772
|
+
})
|
|
1773
|
+
)
|
|
1774
|
+
];
|
|
1775
|
+
try {
|
|
1776
|
+
const response = await apiClient.invoke("readProduct post /product", {
|
|
1777
|
+
body: {
|
|
1778
|
+
filter,
|
|
1779
|
+
limit: 1,
|
|
1780
|
+
includes: {
|
|
1781
|
+
product: ["id", "translated", "productNumber", "seoUrls"],
|
|
1782
|
+
seo_url: ["seoPathInfo"]
|
|
1783
|
+
},
|
|
1784
|
+
associations: {
|
|
1785
|
+
seoUrls: {}
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
});
|
|
1789
|
+
return response.data.elements?.[0];
|
|
1790
|
+
} catch (e) {
|
|
1791
|
+
console.error("SwProductDetails:findVariantForSelectedOptions", e);
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
const handleChange = async (group, option, onChangeHandled) => {
|
|
1795
|
+
selected.value = Object.assign({}, selected.value, {
|
|
1796
|
+
[group]: option
|
|
1797
|
+
});
|
|
1798
|
+
if (typeof onChangeHandled === "function") {
|
|
1799
|
+
await onChangeHandled();
|
|
1800
|
+
}
|
|
1801
|
+
};
|
|
1802
|
+
return {
|
|
1803
|
+
handleChange,
|
|
1804
|
+
findVariantForSelectedOptions,
|
|
1805
|
+
isLoadingOptions,
|
|
1806
|
+
getOptionGroups,
|
|
1807
|
+
getSelectedOptions: computed(() => selected.value)
|
|
1808
|
+
};
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
function useProductPrice(product) {
|
|
1812
|
+
const _cheapest = computed(() => product.value?.calculatedCheapestPrice);
|
|
1813
|
+
const _real = computed(
|
|
1814
|
+
() => (product.value?.calculatedPrices?.length ?? 0) > 0 ? product.value?.calculatedPrices?.[0] : product.value?.calculatedPrice
|
|
1815
|
+
);
|
|
1816
|
+
const referencePrice = computed(() => _real?.value?.referencePrice);
|
|
1817
|
+
const displayFrom = computed(() => {
|
|
1818
|
+
return (product.value?.calculatedPrices?.length ?? 0) > 1;
|
|
1819
|
+
});
|
|
1820
|
+
const displayFromVariants = computed(
|
|
1821
|
+
() => {
|
|
1822
|
+
return !!product.value?.parentId && product.value?.calculatedCheapestPrice?.hasRange && _real?.value?.unitPrice !== _cheapest?.value?.unitPrice && _cheapest?.value?.unitPrice;
|
|
1823
|
+
}
|
|
1824
|
+
);
|
|
1825
|
+
const _price = computed(
|
|
1826
|
+
() => {
|
|
1827
|
+
if (displayFrom.value && getProductTierPrices(product.value).length > 1) {
|
|
1828
|
+
return product.value?.calculatedPrices?.reduce((previous, current) => {
|
|
1829
|
+
return current.unitPrice < previous.unitPrice ? current : previous;
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
return _real.value;
|
|
1833
|
+
}
|
|
1834
|
+
);
|
|
1835
|
+
const unitPrice = computed(
|
|
1836
|
+
() => _price.value?.unitPrice
|
|
1837
|
+
);
|
|
1838
|
+
const totalPrice = computed(
|
|
1839
|
+
() => _price.value?.totalPrice
|
|
1840
|
+
);
|
|
1841
|
+
const price = computed(
|
|
1842
|
+
() => _price.value
|
|
1843
|
+
);
|
|
1844
|
+
const isListPrice = computed(() => {
|
|
1845
|
+
return !!_price.value?.listPrice?.percentage;
|
|
1846
|
+
});
|
|
1847
|
+
const regulationPrice = computed(
|
|
1848
|
+
() => product.value?.calculatedPrice?.regulationPrice?.price
|
|
1849
|
+
);
|
|
1850
|
+
const tierPrices = computed(() => getProductTierPrices(product.value));
|
|
1851
|
+
return {
|
|
1852
|
+
price,
|
|
1853
|
+
totalPrice,
|
|
1854
|
+
unitPrice,
|
|
1855
|
+
displayFromVariants,
|
|
1856
|
+
displayFrom,
|
|
1857
|
+
tierPrices,
|
|
1858
|
+
referencePrice,
|
|
1859
|
+
isListPrice,
|
|
1860
|
+
regulationPrice
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
function useProductReviews(product) {
|
|
1865
|
+
const { apiClient } = useShopwareContext();
|
|
1866
|
+
const productReviews = ref([]);
|
|
1867
|
+
const loadProductReviews = async (parameters = {}) => {
|
|
1868
|
+
const fetchedReviews = await apiClient.invoke(
|
|
1869
|
+
"readProductReviews post /product/{productId}/reviews",
|
|
1870
|
+
{
|
|
1871
|
+
pathParams: { productId: product.value.id },
|
|
1872
|
+
body: parameters
|
|
1873
|
+
}
|
|
1874
|
+
);
|
|
1875
|
+
productReviews.value = fetchedReviews.data.elements ?? [];
|
|
1876
|
+
return fetchedReviews.data;
|
|
1877
|
+
};
|
|
1878
|
+
const addReview = async (data) => {
|
|
1879
|
+
await apiClient.invoke(
|
|
1880
|
+
"saveProductReview post /product/{productId}/review",
|
|
1881
|
+
{
|
|
1882
|
+
pathParams: { productId: product.value.id },
|
|
1883
|
+
body: data
|
|
1884
|
+
}
|
|
1885
|
+
);
|
|
1886
|
+
};
|
|
1887
|
+
return {
|
|
1888
|
+
productReviews: computed(() => productReviews.value),
|
|
1889
|
+
loadProductReviews,
|
|
1890
|
+
addReview
|
|
1891
|
+
};
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
function useProductSearch() {
|
|
1895
|
+
const { apiClient } = useShopwareContext();
|
|
1896
|
+
const search = async (productId, options) => {
|
|
1897
|
+
const associations = defu(
|
|
1898
|
+
options?.withCmsAssociations ? cmsAssociations : {},
|
|
1899
|
+
options?.criteria,
|
|
1900
|
+
{ associations: options?.associations ?? {} }
|
|
1901
|
+
);
|
|
1902
|
+
const result = await apiClient.invoke(
|
|
1903
|
+
"readProductDetail post /product/{productId}",
|
|
1904
|
+
{
|
|
1905
|
+
headers: {
|
|
1906
|
+
"sw-include-seo-urls": true
|
|
1907
|
+
},
|
|
1908
|
+
pathParams: { productId },
|
|
1909
|
+
body: associations
|
|
1910
|
+
}
|
|
1911
|
+
);
|
|
1912
|
+
return result.data;
|
|
1913
|
+
};
|
|
1914
|
+
return {
|
|
1915
|
+
search
|
|
1916
|
+
};
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
function useProductSearchSuggest() {
|
|
1920
|
+
const searchTerm = ref("");
|
|
1921
|
+
const listingComposable = useProductSearchListing();
|
|
1922
|
+
const search = async (additionalCriteria = {}) => {
|
|
1923
|
+
const searchCriteria = {
|
|
1924
|
+
...additionalCriteria,
|
|
1925
|
+
search: searchTerm.value
|
|
1926
|
+
};
|
|
1927
|
+
return listingComposable.search(searchCriteria);
|
|
1928
|
+
};
|
|
1929
|
+
return {
|
|
1930
|
+
searchTerm,
|
|
1931
|
+
loading: listingComposable.loading,
|
|
1932
|
+
search,
|
|
1933
|
+
loadMore: listingComposable.loadMore,
|
|
1934
|
+
getProducts: listingComposable.getElements,
|
|
1935
|
+
getTotal: listingComposable.getTotal
|
|
1936
|
+
};
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
function useProductWishlist(productId) {
|
|
1940
|
+
const { isLoggedIn } = useUser();
|
|
1941
|
+
const {
|
|
1942
|
+
addToWishlist: addItem,
|
|
1943
|
+
removeFromWishlist: removeItem,
|
|
1944
|
+
items
|
|
1945
|
+
} = useLocalWishlist();
|
|
1946
|
+
const {
|
|
1947
|
+
addToWishlistSync: addItemSync,
|
|
1948
|
+
removeFromWishlistSync: removeItemSync,
|
|
1949
|
+
items: itemsSync,
|
|
1950
|
+
getWishlistProducts
|
|
1951
|
+
} = useSyncWishlist();
|
|
1952
|
+
async function removeFromWishlist() {
|
|
1953
|
+
if (isLoggedIn.value) {
|
|
1954
|
+
await removeItemSync(productId);
|
|
1955
|
+
await getWishlistProducts();
|
|
1956
|
+
} else {
|
|
1957
|
+
await removeItem(productId);
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
async function addToWishlist() {
|
|
1961
|
+
if (isLoggedIn.value) {
|
|
1962
|
+
await addItemSync(productId);
|
|
1963
|
+
await getWishlistProducts();
|
|
1964
|
+
} else {
|
|
1965
|
+
await addItem(productId);
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
const isInWishlist = computed(
|
|
1969
|
+
() => isLoggedIn.value ? itemsSync.value?.includes(productId) : items.value?.includes(productId)
|
|
1970
|
+
);
|
|
1971
|
+
return {
|
|
1972
|
+
addToWishlist,
|
|
1973
|
+
removeFromWishlist,
|
|
1974
|
+
isInWishlist
|
|
1975
|
+
};
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
function useSalutations() {
|
|
1979
|
+
const { apiClient } = useShopwareContext();
|
|
1980
|
+
const _salutations = inject("swSalutations", ref());
|
|
1981
|
+
provide("swSalutations", _salutations);
|
|
1982
|
+
const fetchSalutations = async () => {
|
|
1983
|
+
const result = await apiClient.invoke("readSalutation post /salutation");
|
|
1984
|
+
_salutations.value = result.data.elements;
|
|
1985
|
+
return result.data;
|
|
1986
|
+
};
|
|
1987
|
+
const mountedCallback = async () => {
|
|
1988
|
+
if (!_salutations.value) {
|
|
1989
|
+
await fetchSalutations();
|
|
1990
|
+
}
|
|
1991
|
+
};
|
|
1992
|
+
const getSalutations = computed(() => {
|
|
1993
|
+
return _salutations.value || [];
|
|
1994
|
+
});
|
|
1995
|
+
onMounted(mountedCallback);
|
|
1996
|
+
return {
|
|
1997
|
+
fetchSalutations,
|
|
1998
|
+
getSalutations
|
|
1999
|
+
};
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
function useSessionContext(newContext) {
|
|
2003
|
+
const { apiClient } = useShopwareContext();
|
|
2004
|
+
const _sessionContext = useContext("swSessionContext", {
|
|
2005
|
+
replace: newContext
|
|
2006
|
+
});
|
|
2007
|
+
const sessionContext = computed(() => _sessionContext.value);
|
|
2008
|
+
const refreshSessionContext = async () => {
|
|
2009
|
+
try {
|
|
2010
|
+
const { data } = await apiClient.invoke("readContext get /context");
|
|
2011
|
+
_sessionContext.value = data;
|
|
2012
|
+
} catch (e) {
|
|
2013
|
+
console.error("[UseSessionContext][refreshSessionContext]", e);
|
|
2014
|
+
}
|
|
2015
|
+
};
|
|
2016
|
+
const selectedShippingMethod = computed(
|
|
2017
|
+
() => sessionContext.value?.shippingMethod || null
|
|
2018
|
+
);
|
|
2019
|
+
const setShippingMethod = async (shippingMethod) => {
|
|
2020
|
+
if (!shippingMethod?.id) {
|
|
2021
|
+
throw new Error(
|
|
2022
|
+
"You need to provide shipping method id in order to set shipping method."
|
|
2023
|
+
);
|
|
2024
|
+
}
|
|
2025
|
+
await apiClient.invoke("updateContext patch /context", {
|
|
2026
|
+
body: { shippingMethodId: shippingMethod.id }
|
|
2027
|
+
});
|
|
2028
|
+
await refreshSessionContext();
|
|
2029
|
+
};
|
|
2030
|
+
const selectedPaymentMethod = computed(
|
|
2031
|
+
() => sessionContext.value?.paymentMethod || null
|
|
2032
|
+
);
|
|
2033
|
+
const setPaymentMethod = async (paymentMethod) => {
|
|
2034
|
+
if (!paymentMethod?.id) {
|
|
2035
|
+
throw new Error(
|
|
2036
|
+
"You need to provide payment method id in order to set payment method."
|
|
2037
|
+
);
|
|
2038
|
+
}
|
|
2039
|
+
await apiClient.invoke("updateContext patch /context", {
|
|
2040
|
+
body: { paymentMethodId: paymentMethod.id }
|
|
2041
|
+
});
|
|
2042
|
+
await refreshSessionContext();
|
|
2043
|
+
};
|
|
2044
|
+
const currency = computed(() => sessionContext.value?.currency || null);
|
|
2045
|
+
const setCurrency = async (currency2) => {
|
|
2046
|
+
if (!currency2.id) {
|
|
2047
|
+
console.error(
|
|
2048
|
+
"You need to provide currency id in order to set currency.",
|
|
2049
|
+
currency2
|
|
2050
|
+
);
|
|
2051
|
+
return;
|
|
2052
|
+
}
|
|
2053
|
+
await apiClient.invoke("updateContext patch /context", {
|
|
2054
|
+
body: {
|
|
2055
|
+
currencyId: currency2.id
|
|
2056
|
+
}
|
|
2057
|
+
});
|
|
2058
|
+
await refreshSessionContext();
|
|
2059
|
+
};
|
|
2060
|
+
const setLanguage = async (language) => {
|
|
2061
|
+
if (!language.id) {
|
|
2062
|
+
return;
|
|
2063
|
+
}
|
|
2064
|
+
await apiClient.invoke("updateContext patch /context", {
|
|
2065
|
+
body: {
|
|
2066
|
+
languageId: language.id
|
|
2067
|
+
}
|
|
2068
|
+
});
|
|
2069
|
+
await refreshSessionContext();
|
|
2070
|
+
};
|
|
2071
|
+
const setCountry = async (countryId2) => {
|
|
2072
|
+
await apiClient.invoke("updateContext patch /context", {
|
|
2073
|
+
body: {
|
|
2074
|
+
countryId: countryId2
|
|
2075
|
+
}
|
|
2076
|
+
});
|
|
2077
|
+
await refreshSessionContext();
|
|
2078
|
+
};
|
|
2079
|
+
const activeShippingAddress = computed(
|
|
2080
|
+
() => sessionContext.value?.customer?.activeShippingAddress || sessionContext.value?.shippingLocation?.address || null
|
|
2081
|
+
);
|
|
2082
|
+
const setActiveShippingAddress = async (address) => {
|
|
2083
|
+
if (!address?.id) {
|
|
2084
|
+
throw new Error(
|
|
2085
|
+
"You need to provide address id in order to set the address."
|
|
2086
|
+
);
|
|
2087
|
+
}
|
|
2088
|
+
await apiClient.invoke("updateContext patch /context", {
|
|
2089
|
+
body: {
|
|
2090
|
+
shippingAddressId: address.id
|
|
2091
|
+
}
|
|
2092
|
+
});
|
|
2093
|
+
refreshSessionContext();
|
|
2094
|
+
};
|
|
2095
|
+
const activeBillingAddress = computed(
|
|
2096
|
+
() => sessionContext.value?.customer?.activeBillingAddress || null
|
|
2097
|
+
);
|
|
2098
|
+
const setActiveBillingAddress = async (address) => {
|
|
2099
|
+
if (!address?.id) {
|
|
2100
|
+
throw new Error(
|
|
2101
|
+
"You need to provide address id in order to set the address."
|
|
2102
|
+
);
|
|
2103
|
+
}
|
|
2104
|
+
await apiClient.invoke("updateContext patch /context", {
|
|
2105
|
+
body: {
|
|
2106
|
+
billingAddressId: address.id
|
|
2107
|
+
}
|
|
2108
|
+
});
|
|
2109
|
+
refreshSessionContext();
|
|
2110
|
+
};
|
|
2111
|
+
const setContext = (context) => {
|
|
2112
|
+
_sessionContext.value = context;
|
|
2113
|
+
};
|
|
2114
|
+
const countryId = computed(
|
|
2115
|
+
() => sessionContext.value?.shippingLocation?.country?.id
|
|
2116
|
+
);
|
|
2117
|
+
const salesChannelCountryId = computed(
|
|
2118
|
+
() => sessionContext.value?.salesChannel?.countryId
|
|
2119
|
+
);
|
|
2120
|
+
const languageId = computed(
|
|
2121
|
+
() => sessionContext.value?.salesChannel?.languageId
|
|
2122
|
+
);
|
|
2123
|
+
const languageIdChain = computed(
|
|
2124
|
+
() => sessionContext.value?.context?.languageIdChain?.[0] || ""
|
|
2125
|
+
);
|
|
2126
|
+
const taxState = computed(() => sessionContext.value?.context?.taxState);
|
|
2127
|
+
const userFromContext = computed(() => sessionContext.value?.customer);
|
|
2128
|
+
return {
|
|
2129
|
+
sessionContext,
|
|
2130
|
+
refreshSessionContext,
|
|
2131
|
+
selectedShippingMethod,
|
|
2132
|
+
setShippingMethod,
|
|
2133
|
+
selectedPaymentMethod,
|
|
2134
|
+
setPaymentMethod,
|
|
2135
|
+
currency,
|
|
2136
|
+
setCurrency,
|
|
2137
|
+
activeShippingAddress,
|
|
2138
|
+
setActiveShippingAddress,
|
|
2139
|
+
activeBillingAddress,
|
|
2140
|
+
setActiveBillingAddress,
|
|
2141
|
+
countryId,
|
|
2142
|
+
salesChannelCountryId,
|
|
2143
|
+
taxState,
|
|
2144
|
+
userFromContext,
|
|
2145
|
+
setLanguage,
|
|
2146
|
+
languageId,
|
|
2147
|
+
languageIdChain,
|
|
2148
|
+
setCountry,
|
|
2149
|
+
setContext
|
|
2150
|
+
};
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
function useShopwareContext() {
|
|
2154
|
+
const shopwareContext = inject("shopware", null);
|
|
2155
|
+
const apiClient = inject("apiClient");
|
|
2156
|
+
if (!shopwareContext || !apiClient) {
|
|
2157
|
+
console.error("[Error][Shopware] API Client is not provided.");
|
|
2158
|
+
throw new ContextError("Shopware or apiClient");
|
|
2159
|
+
}
|
|
2160
|
+
return {
|
|
2161
|
+
apiClient,
|
|
2162
|
+
devStorefrontUrl: shopwareContext.devStorefrontUrl
|
|
2163
|
+
};
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
const _wishlistItems = ref([]);
|
|
2167
|
+
const _currentPage = ref(1);
|
|
2168
|
+
const totalWishlistItemsCount = ref(0);
|
|
2169
|
+
function useSyncWishlist() {
|
|
2170
|
+
const { apiClient } = useShopwareContext();
|
|
2171
|
+
async function addToWishlistSync(id) {
|
|
2172
|
+
await apiClient.invoke(
|
|
2173
|
+
"addProductOnWishlist post /customer/wishlist/add/{productId}",
|
|
2174
|
+
{
|
|
2175
|
+
pathParams: { productId: id }
|
|
2176
|
+
}
|
|
2177
|
+
);
|
|
2178
|
+
}
|
|
2179
|
+
async function removeFromWishlistSync(id) {
|
|
2180
|
+
await apiClient.invoke(
|
|
2181
|
+
"deleteProductOnWishlist delete /customer/wishlist/delete/{productId}",
|
|
2182
|
+
{
|
|
2183
|
+
pathParams: { productId: id }
|
|
2184
|
+
}
|
|
2185
|
+
);
|
|
2186
|
+
}
|
|
2187
|
+
async function getWishlistProducts(defaultSearchCriteria) {
|
|
2188
|
+
try {
|
|
2189
|
+
const response = await apiClient.invoke(
|
|
2190
|
+
"readCustomerWishlist post /customer/wishlist",
|
|
2191
|
+
{ body: { ...defaultSearchCriteria, "total-count-mode": "exact" } }
|
|
2192
|
+
);
|
|
2193
|
+
_wishlistItems.value = [
|
|
2194
|
+
...response.data.products.elements.map((element) => element.id)
|
|
2195
|
+
];
|
|
2196
|
+
totalWishlistItemsCount.value = response.data.products.total ?? 0;
|
|
2197
|
+
_currentPage.value = response.data.products.page ?? 1;
|
|
2198
|
+
} catch (e) {
|
|
2199
|
+
if (e instanceof ApiClientError) {
|
|
2200
|
+
if (e.status !== 404)
|
|
2201
|
+
console.error(e);
|
|
2202
|
+
_wishlistItems.value = [];
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
async function mergeWishlistProducts(productIds) {
|
|
2207
|
+
await apiClient.invoke(
|
|
2208
|
+
"mergeProductOnWishlist post /customer/wishlist/merge",
|
|
2209
|
+
{
|
|
2210
|
+
body: { productIds }
|
|
2211
|
+
}
|
|
2212
|
+
);
|
|
2213
|
+
}
|
|
2214
|
+
const items = computed(() => _wishlistItems.value);
|
|
2215
|
+
const count = computed(() => totalWishlistItemsCount.value);
|
|
2216
|
+
const currentPage = computed(() => _currentPage.value);
|
|
2217
|
+
return {
|
|
2218
|
+
getWishlistProducts,
|
|
2219
|
+
addToWishlistSync,
|
|
2220
|
+
removeFromWishlistSync,
|
|
2221
|
+
mergeWishlistProducts,
|
|
2222
|
+
items,
|
|
2223
|
+
count,
|
|
2224
|
+
currentPage
|
|
2225
|
+
};
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
function useUrlResolver() {
|
|
2229
|
+
const getUrlPrefix = () => inject("urlPrefix", "");
|
|
2230
|
+
const resolveUrl = (url) => {
|
|
2231
|
+
if (url.length > 2083) {
|
|
2232
|
+
throw new Error("URL Input too long");
|
|
2233
|
+
}
|
|
2234
|
+
const navigationPattern = /[a-zA-Z0-9]+\/navigation\/[a-zA-Z0-9]+/;
|
|
2235
|
+
const urlPrefix = getUrlPrefix();
|
|
2236
|
+
if (navigationPattern.test(url)) {
|
|
2237
|
+
const newUrl = url.split("/").slice(1);
|
|
2238
|
+
if (newUrl.length > 0) {
|
|
2239
|
+
return `${buildUrlPrefix(newUrl.join("/"), urlPrefix)}`;
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
return url;
|
|
2243
|
+
};
|
|
2244
|
+
return {
|
|
2245
|
+
resolveUrl,
|
|
2246
|
+
getUrlPrefix
|
|
2247
|
+
};
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
function useUser() {
|
|
2251
|
+
const { apiClient } = useShopwareContext();
|
|
2252
|
+
const { userFromContext, refreshSessionContext } = useSessionContext();
|
|
2253
|
+
const _user = useContext("customer");
|
|
2254
|
+
syncRefs(userFromContext, _user, {
|
|
2255
|
+
immediate: true
|
|
2256
|
+
});
|
|
2257
|
+
const { getStorefrontUrl } = useInternationalization();
|
|
2258
|
+
const { refreshCart } = useCart();
|
|
2259
|
+
const userDefaultPaymentMethod = computed(
|
|
2260
|
+
() => user.value?.defaultPaymentMethod?.translated || null
|
|
2261
|
+
);
|
|
2262
|
+
const userDefaultBillingAddress = computed(
|
|
2263
|
+
() => user.value?.defaultBillingAddress || null
|
|
2264
|
+
);
|
|
2265
|
+
const userDefaultShippingAddress = computed(
|
|
2266
|
+
() => user.value?.defaultShippingAddress || null
|
|
2267
|
+
);
|
|
2268
|
+
const country = ref(null);
|
|
2269
|
+
const salutation = ref(null);
|
|
2270
|
+
const user = computed(() => _user.value);
|
|
2271
|
+
async function login({
|
|
2272
|
+
username,
|
|
2273
|
+
password
|
|
2274
|
+
}) {
|
|
2275
|
+
await apiClient.invoke("loginCustomer post /account/login", {
|
|
2276
|
+
body: {
|
|
2277
|
+
username,
|
|
2278
|
+
password
|
|
2279
|
+
}
|
|
2280
|
+
});
|
|
2281
|
+
await refreshSessionContext();
|
|
2282
|
+
refreshCart();
|
|
2283
|
+
}
|
|
2284
|
+
async function register(params) {
|
|
2285
|
+
const { data } = await apiClient.invoke("register post /account/register", {
|
|
2286
|
+
body: {
|
|
2287
|
+
...params,
|
|
2288
|
+
storefrontUrl: getStorefrontUrl()
|
|
2289
|
+
}
|
|
2290
|
+
});
|
|
2291
|
+
if (data.active && !data.doubleOptInRegistration) {
|
|
2292
|
+
_user.value = data;
|
|
2293
|
+
}
|
|
2294
|
+
await refreshSessionContext();
|
|
2295
|
+
return data;
|
|
2296
|
+
}
|
|
2297
|
+
async function logout() {
|
|
2298
|
+
const response = await apiClient.invoke(
|
|
2299
|
+
"logoutCustomer post /account/logout"
|
|
2300
|
+
);
|
|
2301
|
+
await refreshSessionContext();
|
|
2302
|
+
refreshCart();
|
|
2303
|
+
return response.data;
|
|
2304
|
+
}
|
|
2305
|
+
async function refreshUser(params = {}) {
|
|
2306
|
+
try {
|
|
2307
|
+
const response = await apiClient.invoke(
|
|
2308
|
+
"readCustomer post /account/customer",
|
|
2309
|
+
{ body: params }
|
|
2310
|
+
);
|
|
2311
|
+
_user.value = response.data;
|
|
2312
|
+
return response.data;
|
|
2313
|
+
} catch (e) {
|
|
2314
|
+
_user.value = void 0;
|
|
2315
|
+
throw e;
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
async function loadCountry(countryId) {
|
|
2319
|
+
const countries = await apiClient.invoke("readCountry post /country", {
|
|
2320
|
+
body: {
|
|
2321
|
+
filter: [
|
|
2322
|
+
{
|
|
2323
|
+
field: "id",
|
|
2324
|
+
type: "equals",
|
|
2325
|
+
value: countryId
|
|
2326
|
+
}
|
|
2327
|
+
]
|
|
2328
|
+
}
|
|
2329
|
+
});
|
|
2330
|
+
country.value = countries.data.elements?.[0] ?? null;
|
|
2331
|
+
return countries.data;
|
|
2332
|
+
}
|
|
2333
|
+
async function loadSalutation(salutationId) {
|
|
2334
|
+
const salutations = await apiClient.invoke(
|
|
2335
|
+
"readSalutation post /salutation",
|
|
2336
|
+
{
|
|
2337
|
+
body: {
|
|
2338
|
+
filter: [
|
|
2339
|
+
{
|
|
2340
|
+
field: "id",
|
|
2341
|
+
type: "equals",
|
|
2342
|
+
value: salutationId
|
|
2343
|
+
}
|
|
2344
|
+
]
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
);
|
|
2348
|
+
salutation.value = salutations.data.elements?.[0] ?? null;
|
|
2349
|
+
return salutations.data;
|
|
2350
|
+
}
|
|
2351
|
+
async function updatePersonalInfo(personals) {
|
|
2352
|
+
await apiClient.invoke("changeProfile post /account/change-profile", {
|
|
2353
|
+
body: personals
|
|
2354
|
+
});
|
|
2355
|
+
}
|
|
2356
|
+
async function updateEmail(updateEmailData) {
|
|
2357
|
+
await apiClient.invoke("changeEmail post /account/change-email", {
|
|
2358
|
+
body: updateEmailData
|
|
2359
|
+
});
|
|
2360
|
+
}
|
|
2361
|
+
async function setDefaultPaymentMethod(paymentMethodId) {
|
|
2362
|
+
await apiClient.invoke(
|
|
2363
|
+
"changePaymentMethod post /account/change-payment-method/{paymentMethodId}",
|
|
2364
|
+
{
|
|
2365
|
+
pathParams: { paymentMethodId }
|
|
2366
|
+
}
|
|
2367
|
+
);
|
|
2368
|
+
}
|
|
2369
|
+
const defaultBillingAddressId = computed(
|
|
2370
|
+
() => user.value?.defaultBillingAddressId || null
|
|
2371
|
+
);
|
|
2372
|
+
const defaultShippingAddressId = computed(
|
|
2373
|
+
() => user.value?.defaultShippingAddressId || null
|
|
2374
|
+
);
|
|
2375
|
+
const isLoggedIn = computed(
|
|
2376
|
+
() => !!user.value?.id && !!user.value.active && !user.value.guest
|
|
2377
|
+
);
|
|
2378
|
+
const isCustomerSession = computed(
|
|
2379
|
+
() => !!user.value?.id && !user.value.guest
|
|
2380
|
+
);
|
|
2381
|
+
const isGuestSession = computed(() => !!user.value?.guest);
|
|
2382
|
+
return {
|
|
2383
|
+
login,
|
|
2384
|
+
register,
|
|
2385
|
+
user,
|
|
2386
|
+
isLoggedIn,
|
|
2387
|
+
isCustomerSession,
|
|
2388
|
+
isGuestSession,
|
|
2389
|
+
refreshUser,
|
|
2390
|
+
logout,
|
|
2391
|
+
updateEmail,
|
|
2392
|
+
updatePersonalInfo,
|
|
2393
|
+
setDefaultPaymentMethod,
|
|
2394
|
+
loadSalutation,
|
|
2395
|
+
salutation,
|
|
2396
|
+
loadCountry,
|
|
2397
|
+
country,
|
|
2398
|
+
defaultBillingAddressId,
|
|
2399
|
+
defaultShippingAddressId,
|
|
2400
|
+
userDefaultPaymentMethod,
|
|
2401
|
+
userDefaultBillingAddress,
|
|
2402
|
+
userDefaultShippingAddress
|
|
2403
|
+
};
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
function useWishlist() {
|
|
2407
|
+
const { isLoggedIn, isGuestSession } = useUser();
|
|
2408
|
+
const canSyncWishlist = computed(
|
|
2409
|
+
() => isLoggedIn.value && !isGuestSession.value
|
|
2410
|
+
);
|
|
2411
|
+
const {
|
|
2412
|
+
getWishlistProducts: getWishlistProductsLocal,
|
|
2413
|
+
items: itemsLocal,
|
|
2414
|
+
clearWishlist: clearWishlistLocal
|
|
2415
|
+
} = useLocalWishlist();
|
|
2416
|
+
const {
|
|
2417
|
+
getWishlistProducts: getWishlistProductsSync,
|
|
2418
|
+
items: itemsSync,
|
|
2419
|
+
mergeWishlistProducts: mergeWishlistProductsSync,
|
|
2420
|
+
removeFromWishlistSync,
|
|
2421
|
+
count: countSync,
|
|
2422
|
+
currentPage
|
|
2423
|
+
} = useSyncWishlist();
|
|
2424
|
+
const limit = ref(15);
|
|
2425
|
+
const getWishlistProducts = async (query) => {
|
|
2426
|
+
const wishlistQuery = {};
|
|
2427
|
+
if (query) {
|
|
2428
|
+
defu(wishlistQuery, query);
|
|
2429
|
+
}
|
|
2430
|
+
if (query?.limit) {
|
|
2431
|
+
limit.value = query.limit;
|
|
2432
|
+
}
|
|
2433
|
+
if (canSyncWishlist.value) {
|
|
2434
|
+
await getWishlistProductsSync(query);
|
|
2435
|
+
} else {
|
|
2436
|
+
getWishlistProductsLocal();
|
|
2437
|
+
}
|
|
2438
|
+
};
|
|
2439
|
+
const items = computed(
|
|
2440
|
+
() => canSyncWishlist.value ? itemsSync.value : itemsLocal.value
|
|
2441
|
+
);
|
|
2442
|
+
const totalPagesCount = computed(() => Math.ceil(count.value / limit.value));
|
|
2443
|
+
const clearWishlist = async () => {
|
|
2444
|
+
if (canSyncWishlist.value) {
|
|
2445
|
+
await Promise.all(items.value.map((id) => removeFromWishlistSync(id)));
|
|
2446
|
+
await getWishlistProductsSync();
|
|
2447
|
+
} else {
|
|
2448
|
+
clearWishlistLocal();
|
|
2449
|
+
}
|
|
2450
|
+
};
|
|
2451
|
+
const mergeWishlistProducts = async () => {
|
|
2452
|
+
if (itemsLocal.value?.length) {
|
|
2453
|
+
await mergeWishlistProductsSync(itemsLocal.value);
|
|
2454
|
+
clearWishlist();
|
|
2455
|
+
}
|
|
2456
|
+
await getWishlistProductsSync();
|
|
2457
|
+
};
|
|
2458
|
+
const count = computed(() => {
|
|
2459
|
+
if (canSyncWishlist.value) {
|
|
2460
|
+
return countSync.value;
|
|
2461
|
+
}
|
|
2462
|
+
return items.value.length;
|
|
2463
|
+
});
|
|
2464
|
+
return {
|
|
2465
|
+
mergeWishlistProducts,
|
|
2466
|
+
getWishlistProducts,
|
|
2467
|
+
clearWishlist,
|
|
2468
|
+
items,
|
|
2469
|
+
currentPage,
|
|
2470
|
+
totalPagesCount,
|
|
2471
|
+
count,
|
|
2472
|
+
canSyncWishlist
|
|
2473
|
+
};
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
function useB2bQuoteManagement() {
|
|
2477
|
+
const { apiClient } = useShopwareContext();
|
|
2478
|
+
const getQuoteList = async () => {
|
|
2479
|
+
const response = await apiClient.invoke("readQuotes post /quotes");
|
|
2480
|
+
return response.data.elements ?? [];
|
|
2481
|
+
};
|
|
2482
|
+
const getQuote = async (quoteId) => {
|
|
2483
|
+
const response = await apiClient.invoke(
|
|
2484
|
+
"readQuote post /quote/detail/{id}",
|
|
2485
|
+
{
|
|
2486
|
+
pathParams: {
|
|
2487
|
+
id: quoteId
|
|
2488
|
+
},
|
|
2489
|
+
body: {
|
|
2490
|
+
associations: {
|
|
2491
|
+
lineItems: {},
|
|
2492
|
+
comments: {},
|
|
2493
|
+
stateMachineState: {}
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
}
|
|
2497
|
+
);
|
|
2498
|
+
return response.data;
|
|
2499
|
+
};
|
|
2500
|
+
async function declineQuote(quoteId, comment) {
|
|
2501
|
+
await apiClient.invoke("declineQuote post /quote/{id}/decline", {
|
|
2502
|
+
pathParams: {
|
|
2503
|
+
id: quoteId
|
|
2504
|
+
},
|
|
2505
|
+
body: {
|
|
2506
|
+
comment
|
|
2507
|
+
}
|
|
2508
|
+
});
|
|
2509
|
+
}
|
|
2510
|
+
async function requestChangeQuote(quoteId, comment) {
|
|
2511
|
+
await apiClient.invoke(
|
|
2512
|
+
"requestChangeQuote post /quote/{id}/request-change",
|
|
2513
|
+
{
|
|
2514
|
+
pathParams: {
|
|
2515
|
+
id: quoteId
|
|
2516
|
+
},
|
|
2517
|
+
body: { comment }
|
|
2518
|
+
}
|
|
2519
|
+
);
|
|
2520
|
+
}
|
|
2521
|
+
const requestQuote = async (comment) => {
|
|
2522
|
+
const response = await apiClient.invoke(
|
|
2523
|
+
"requestQuote post /quote/request",
|
|
2524
|
+
{ body: { comment } }
|
|
2525
|
+
);
|
|
2526
|
+
return response.data;
|
|
2527
|
+
};
|
|
2528
|
+
const changePaymentShippingMethod = async (params) => {
|
|
2529
|
+
const body = {};
|
|
2530
|
+
if (params.paymentMethodId)
|
|
2531
|
+
body.paymentMethodId = params.paymentMethodId;
|
|
2532
|
+
if (params.shippingMethodId)
|
|
2533
|
+
body.shippingMethodId = params.shippingMethodId;
|
|
2534
|
+
await apiClient.invoke(
|
|
2535
|
+
"switchPaymentOrShippingMethod post /quote/{id}/configure",
|
|
2536
|
+
{
|
|
2537
|
+
pathParams: { id: params.quoteId },
|
|
2538
|
+
body
|
|
2539
|
+
}
|
|
2540
|
+
);
|
|
2541
|
+
};
|
|
2542
|
+
async function changeShippingMethod(quoteId, shippingMethodId) {
|
|
2543
|
+
await changePaymentShippingMethod({ quoteId, shippingMethodId });
|
|
2544
|
+
}
|
|
2545
|
+
async function changePaymentMethod(quoteId, paymentMethodId) {
|
|
2546
|
+
await changePaymentShippingMethod({ quoteId, paymentMethodId });
|
|
2547
|
+
}
|
|
2548
|
+
const createOrderFromQuote = async (quoteId, comment) => {
|
|
2549
|
+
const response = await apiClient.invoke(
|
|
2550
|
+
"createOrderFromQuote post /quote/order/{id}",
|
|
2551
|
+
{
|
|
2552
|
+
pathParams: { id: quoteId },
|
|
2553
|
+
body: { customerComment: comment }
|
|
2554
|
+
}
|
|
2555
|
+
);
|
|
2556
|
+
return response.data;
|
|
2557
|
+
};
|
|
2558
|
+
return {
|
|
2559
|
+
getQuoteList,
|
|
2560
|
+
getQuote,
|
|
2561
|
+
declineQuote,
|
|
2562
|
+
requestChangeQuote,
|
|
2563
|
+
changeShippingMethod,
|
|
2564
|
+
changePaymentMethod,
|
|
2565
|
+
requestQuote,
|
|
2566
|
+
createOrderFromQuote
|
|
2567
|
+
};
|
|
2568
|
+
}
|
|
2569
|
+
|
|
2570
|
+
const successCodes = ["promotion-discount-added"];
|
|
2571
|
+
function useCartNotification() {
|
|
2572
|
+
const { pushError, pushSuccess } = useNotifications();
|
|
2573
|
+
const { consumeCartErrors } = useCart();
|
|
2574
|
+
const codeErrorsNotification = () => {
|
|
2575
|
+
const errors = consumeCartErrors();
|
|
2576
|
+
if (!errors || Array.isArray(errors))
|
|
2577
|
+
return;
|
|
2578
|
+
for (const element of Object.keys(errors)) {
|
|
2579
|
+
if (successCodes.includes(errors[element].messageKey)) {
|
|
2580
|
+
pushSuccess(errors[element].message);
|
|
2581
|
+
} else {
|
|
2582
|
+
pushError(errors[element].message);
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
};
|
|
2586
|
+
const getErrorsCodes = () => {
|
|
2587
|
+
const errors = consumeCartErrors();
|
|
2588
|
+
if (!errors || Array.isArray(errors))
|
|
2589
|
+
return [];
|
|
2590
|
+
return Object.keys(errors).reduce(
|
|
2591
|
+
(acc, element) => {
|
|
2592
|
+
if (!successCodes.includes(errors[element].messageKey))
|
|
2593
|
+
acc.push(errors[element]);
|
|
2594
|
+
return acc;
|
|
2595
|
+
},
|
|
2596
|
+
[]
|
|
2597
|
+
);
|
|
2598
|
+
};
|
|
2599
|
+
return {
|
|
2600
|
+
codeErrorsNotification,
|
|
2601
|
+
getErrorsCodes
|
|
2602
|
+
};
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
function useCartErrorParamsResolver() {
|
|
2606
|
+
const resolveCartError = (errorObject) => {
|
|
2607
|
+
let params = null;
|
|
2608
|
+
let messageKey = errorObject.messageKey;
|
|
2609
|
+
const { cartItems } = useCart();
|
|
2610
|
+
const extractProductId = (message, error) => {
|
|
2611
|
+
return message.replace(error, "");
|
|
2612
|
+
};
|
|
2613
|
+
const buildProductStockReached = () => {
|
|
2614
|
+
const productId = extractProductId(
|
|
2615
|
+
errorObject.key,
|
|
2616
|
+
"product-stock-reached"
|
|
2617
|
+
);
|
|
2618
|
+
const product = getItem(productId);
|
|
2619
|
+
return {
|
|
2620
|
+
name: product?.label || "",
|
|
2621
|
+
quantity: product?.quantityInformation?.maxPurchase || null
|
|
2622
|
+
};
|
|
2623
|
+
};
|
|
2624
|
+
const getItem = (id) => {
|
|
2625
|
+
return cartItems.value.find((item) => item.id === id);
|
|
2626
|
+
};
|
|
2627
|
+
const buildShippingMethodBlocked = () => {
|
|
2628
|
+
return errorObject.message?.replace("shipping-method-blocked-", "") || "";
|
|
2629
|
+
};
|
|
2630
|
+
switch (errorObject.messageKey) {
|
|
2631
|
+
case "product-stock-reached":
|
|
2632
|
+
params = buildProductStockReached();
|
|
2633
|
+
if (!params.name || !params.quantity) {
|
|
2634
|
+
messageKey = "product-stock-reached-empty";
|
|
2635
|
+
params = null;
|
|
2636
|
+
}
|
|
2637
|
+
break;
|
|
2638
|
+
case "shipping-method-blocked":
|
|
2639
|
+
params = {
|
|
2640
|
+
name: buildShippingMethodBlocked()
|
|
2641
|
+
};
|
|
2642
|
+
break;
|
|
2643
|
+
default:
|
|
2644
|
+
params = { ...errorObject };
|
|
2645
|
+
}
|
|
2646
|
+
return {
|
|
2647
|
+
params,
|
|
2648
|
+
messageKey
|
|
2649
|
+
};
|
|
2650
|
+
};
|
|
2651
|
+
return {
|
|
2652
|
+
resolveCartError
|
|
2653
|
+
};
|
|
2654
|
+
}
|
|
2655
|
+
|
|
2656
|
+
function resolveCmsComponent(content) {
|
|
2657
|
+
const componentName = content.type;
|
|
2658
|
+
const type = content.apiAlias === "cms_block" ? "Block" : content.apiAlias === "cms_section" ? "Section" : "Element";
|
|
2659
|
+
const componentNameToResolve = pascalCase(`Cms-${type}-${componentName}`);
|
|
2660
|
+
try {
|
|
2661
|
+
const resolvedComponent = resolveComponent(componentNameToResolve);
|
|
2662
|
+
return {
|
|
2663
|
+
componentName,
|
|
2664
|
+
componentNameToResolve,
|
|
2665
|
+
isResolved: resolvedComponent !== componentName,
|
|
2666
|
+
resolvedComponent: typeof resolvedComponent !== "string" ? resolvedComponent : void 0
|
|
2667
|
+
};
|
|
2668
|
+
} catch (e) {
|
|
2669
|
+
return {
|
|
2670
|
+
componentName,
|
|
2671
|
+
componentNameToResolve,
|
|
2672
|
+
resolvedComponent: void 0,
|
|
2673
|
+
resolved: false,
|
|
2674
|
+
isResolved: false,
|
|
2675
|
+
error: e.message
|
|
2676
|
+
};
|
|
2677
|
+
}
|
|
2678
|
+
}
|
|
2679
|
+
function getDefaultApiParams() {
|
|
2680
|
+
console.error(
|
|
2681
|
+
"[@shopware-pwa/composables] `getDefaultApiParams` is deprecated and will be removed in the next major release."
|
|
2682
|
+
);
|
|
2683
|
+
return {};
|
|
2684
|
+
}
|
|
2685
|
+
|
|
2686
|
+
export { createCategoryListingContext, createListingComposable, createShopwareContext, getDefaultApiParams, resolveCmsComponent, useAddToCart, useAddress, useB2bQuoteManagement, useBreadcrumbs, useCart, useCartErrorParamsResolver, useCartFunction, useCartItem, useCartNotification, useCategory, useCategoryListing, useCategorySearch, useCheckout, useCmsBlock, useCmsElementConfig, useCmsElementImage, useCmsMeta, useCmsSection, useCmsTranslations, useContext, useCountries, useCustomerOrders, useCustomerPassword, useDefaultOrderAssociations, useInternationalization, useLandingSearch, useListing, useLocalWishlist, useNavigation, useNavigationContext, useNavigationSearch, useNewsletter, useNotifications, useOrderDetails, useOrderPayment, usePrice, useProduct, useProductAssociations, useProductConfigurator, useProductPrice, useProductReviews, useProductSearch, useProductSearchListing, useProductSearchSuggest, useProductWishlist, useSalutations, useSessionContext, useShopwareContext, useSyncWishlist, useUrlResolver, useUser, useWishlist };
|