@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
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
import type { ComputedRef } from "vue";
|
|
3
|
+
import { useContext } from "#imports";
|
|
4
|
+
import type { Schemas, operations } from "#shopware";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export type Breadcrumb =
|
|
10
|
+
| {
|
|
11
|
+
name: string;
|
|
12
|
+
path?: string;
|
|
13
|
+
}
|
|
14
|
+
| Schemas["Breadcrumb"];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export type UseBreadcrumbsReturn = {
|
|
20
|
+
/**
|
|
21
|
+
* Clear breadcrumbs store
|
|
22
|
+
*/
|
|
23
|
+
clearBreadcrumbs(): void;
|
|
24
|
+
/**
|
|
25
|
+
* List of breadcrumbs
|
|
26
|
+
*/
|
|
27
|
+
breadcrumbs: ComputedRef<Breadcrumb[]>;
|
|
28
|
+
/**
|
|
29
|
+
* Build breadcrumbs dynamically for a category by fetching them from the API
|
|
30
|
+
*
|
|
31
|
+
* @param {operations["readBreadcrumb get /breadcrumb/{id}"]["response"]} breadcrumbs
|
|
32
|
+
*/
|
|
33
|
+
buildDynamicBreadcrumbs(
|
|
34
|
+
breadcrumbs: operations["readBreadcrumb get /breadcrumb/{id}"]["response"],
|
|
35
|
+
): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Add a breadcrumb to the breadcrumbs list
|
|
38
|
+
*
|
|
39
|
+
* @param {Breadcrumb} breadcrumb
|
|
40
|
+
*/
|
|
41
|
+
pushBreadcrumb(breadcrumb: Breadcrumb): void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Composable for breadcrumbs management.
|
|
46
|
+
* Read the [guide](https://frontends.shopware.com/getting-started/page-elements/breadcrumbs.html#building-breadcrumbs-for-cms-pages).
|
|
47
|
+
*
|
|
48
|
+
* @public
|
|
49
|
+
* @category CMS (Shopping Experiences)
|
|
50
|
+
*/
|
|
51
|
+
export function useBreadcrumbs(
|
|
52
|
+
newBreadcrumbs?: Breadcrumb[],
|
|
53
|
+
): UseBreadcrumbsReturn {
|
|
54
|
+
// Store for breadcrumbs
|
|
55
|
+
const _breadcrumbs = useContext<Breadcrumb[]>("swBreadcrumb", {
|
|
56
|
+
replace: newBreadcrumbs,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Clear breadcrumbs store
|
|
61
|
+
*/
|
|
62
|
+
const clearBreadcrumbs = () => {
|
|
63
|
+
_breadcrumbs.value = [];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const pushBreadcrumb = (breadcrumb: Breadcrumb) => {
|
|
67
|
+
if (_breadcrumbs.value) _breadcrumbs.value.push(breadcrumb);
|
|
68
|
+
else _breadcrumbs.value = [breadcrumb];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const buildDynamicBreadcrumbs = async (
|
|
72
|
+
breadcrumbs: operations["readBreadcrumb get /breadcrumb/{id}"]["response"],
|
|
73
|
+
) => {
|
|
74
|
+
_breadcrumbs.value = breadcrumbs.breadcrumbs.map((breadcrumb) => {
|
|
75
|
+
// Adjust path to be compatible with the router
|
|
76
|
+
return {
|
|
77
|
+
...breadcrumb,
|
|
78
|
+
path: `/${breadcrumb.path}`,
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
clearBreadcrumbs,
|
|
85
|
+
breadcrumbs: computed(() => _breadcrumbs.value),
|
|
86
|
+
buildDynamicBreadcrumbs,
|
|
87
|
+
pushBreadcrumb,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { Schemas, operations } from "#shopware";
|
|
3
|
+
import { useSetup } from "../_test";
|
|
4
|
+
import Cart from "../mocks/Cart";
|
|
5
|
+
import { useCart } from "./useCart";
|
|
6
|
+
|
|
7
|
+
describe("useCart", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.clearAllMocks();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const itemsMock: operations["addLineItem post /checkout/cart/line-item"]["body"] =
|
|
13
|
+
{
|
|
14
|
+
items: [
|
|
15
|
+
{
|
|
16
|
+
id: "01893ed931d571718e8138e7df7d68d1",
|
|
17
|
+
quantity: 1,
|
|
18
|
+
referencedId: "e05e9340aff4484f9009646dfd572df9",
|
|
19
|
+
type: "product",
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: "01893ed931d571718e8138e7dfda5264",
|
|
23
|
+
quantity: 1,
|
|
24
|
+
referencedId: "482ce99edecd4579ab1a2c1807bf27d7",
|
|
25
|
+
type: "product",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: "01893ed931d571718e8138e7dffd39c6",
|
|
29
|
+
quantity: 2,
|
|
30
|
+
referencedId: "b369bdcbfd0846acbbd8657b8dd18787",
|
|
31
|
+
type: "product",
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const { vm, injections } = useSetup(useCart);
|
|
37
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
38
|
+
data: {},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("add single product", async () => {
|
|
42
|
+
expect(vm.cartItems).toEqual([]);
|
|
43
|
+
expect(vm.totalPrice).toBe(0);
|
|
44
|
+
expect(vm.subtotal).toBe(0);
|
|
45
|
+
await vm.addProduct({
|
|
46
|
+
id: itemsMock.items[0].referencedId as string,
|
|
47
|
+
quantity: itemsMock.items[0].quantity,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
51
|
+
expect.stringContaining("addLineItem"),
|
|
52
|
+
expect.objectContaining({
|
|
53
|
+
body: {
|
|
54
|
+
items: [
|
|
55
|
+
{
|
|
56
|
+
type: itemsMock.items[0].type,
|
|
57
|
+
id: itemsMock.items[0].referencedId,
|
|
58
|
+
quantity: itemsMock.items[0].quantity,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
await vm.addProduct({
|
|
66
|
+
id: itemsMock.items[0].referencedId as string,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
70
|
+
expect.stringContaining("addLineItem"),
|
|
71
|
+
expect.objectContaining({
|
|
72
|
+
body: {
|
|
73
|
+
items: [
|
|
74
|
+
{
|
|
75
|
+
type: itemsMock.items[0].type,
|
|
76
|
+
id: itemsMock.items[0].referencedId,
|
|
77
|
+
quantity: 0,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("refresh the cart", async () => {
|
|
86
|
+
await vm.refreshCart();
|
|
87
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
88
|
+
expect.stringContaining("readCart"),
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("refresh the cart - new cart", async () => {
|
|
93
|
+
expect(await vm.refreshCart(Cart as unknown as Schemas["Cart"])).toEqual(
|
|
94
|
+
Cart,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
expect(vm.shippingTotal).toBe(0);
|
|
98
|
+
expect(vm.subtotal).toBe(16.11);
|
|
99
|
+
expect(vm.isVirtualCart).toBe(false);
|
|
100
|
+
expect(vm.totalPrice).toBe(16.11);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("add set of products", async () => {
|
|
104
|
+
await vm.addProducts(itemsMock.items);
|
|
105
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
106
|
+
expect.stringContaining("addLineItem"),
|
|
107
|
+
expect.objectContaining({
|
|
108
|
+
body: itemsMock,
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("change product quantity", async () => {
|
|
114
|
+
await vm.changeProductQuantity({
|
|
115
|
+
id: itemsMock.items[0].referencedId as string,
|
|
116
|
+
quantity: 4,
|
|
117
|
+
});
|
|
118
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
119
|
+
expect.stringContaining("updateLineItem"),
|
|
120
|
+
expect.objectContaining({
|
|
121
|
+
body: {
|
|
122
|
+
items: [
|
|
123
|
+
{
|
|
124
|
+
id: itemsMock.items[0].referencedId,
|
|
125
|
+
quantity: 4,
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("remove item", async () => {
|
|
134
|
+
await vm.removeItem({
|
|
135
|
+
id: "01893ed931d571718e8138e7df7d68d1",
|
|
136
|
+
} as Schemas["LineItem"]);
|
|
137
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
138
|
+
expect.stringContaining("removeLineItem"),
|
|
139
|
+
expect.objectContaining({
|
|
140
|
+
body: {
|
|
141
|
+
ids: ["01893ed931d571718e8138e7df7d68d1"],
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("submitPromotionCode", async () => {
|
|
148
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
149
|
+
data: {
|
|
150
|
+
lineItems: [
|
|
151
|
+
{
|
|
152
|
+
type: "promotion",
|
|
153
|
+
payload: {
|
|
154
|
+
code: "3a64e872ca404522a2c5d43ebc751e6b",
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
type: "product",
|
|
159
|
+
good: true,
|
|
160
|
+
quantity: 1,
|
|
161
|
+
id: "e05e9340aff4484f9009646dfd572df9",
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
await vm.addPromotionCode("PROMO_CODE");
|
|
168
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
169
|
+
expect.stringContaining("addLineItem"),
|
|
170
|
+
expect.objectContaining({
|
|
171
|
+
body: {
|
|
172
|
+
items: [
|
|
173
|
+
{
|
|
174
|
+
referencedId: "PROMO_CODE",
|
|
175
|
+
type: "promotion",
|
|
176
|
+
},
|
|
177
|
+
],
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
expect(vm.appliedPromotionCodes).toEqual([
|
|
183
|
+
{
|
|
184
|
+
type: "promotion",
|
|
185
|
+
payload: {
|
|
186
|
+
code: "3a64e872ca404522a2c5d43ebc751e6b",
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
]);
|
|
190
|
+
expect(vm.count).toBe(1);
|
|
191
|
+
expect(vm.totalPrice).toBe(0);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("handle api cart error", async () => {
|
|
195
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
196
|
+
data: {
|
|
197
|
+
errors: [
|
|
198
|
+
{
|
|
199
|
+
status: 400,
|
|
200
|
+
code: "CHECKOUT__CART_ITEM_NOT_FOUND",
|
|
201
|
+
detail: "Line item not found",
|
|
202
|
+
},
|
|
203
|
+
],
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
await vm.refreshCart();
|
|
207
|
+
await vm.addProducts(itemsMock.items);
|
|
208
|
+
expect(vm.consumeCartErrors()).toEqual({
|
|
209
|
+
"0": {
|
|
210
|
+
status: 400,
|
|
211
|
+
code: "CHECKOUT__CART_ITEM_NOT_FOUND",
|
|
212
|
+
detail: "Line item not found",
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
expect(vm.consumeCartErrors()).toEqual(null);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should return shipping costs", async () => {
|
|
220
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
221
|
+
data: {
|
|
222
|
+
deliveries: [
|
|
223
|
+
{
|
|
224
|
+
shippingMethod: {
|
|
225
|
+
id: "a9d9cc502b3547f4a89eb2830c032c78",
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
shippingMethod: {
|
|
230
|
+
id: "a9d9cc502b3547f4a89eb2830c032c78",
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
await vm.refreshCart();
|
|
237
|
+
|
|
238
|
+
expect(vm.shippingCosts.length).toEqual(2);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("should return empty shipping costs", async () => {
|
|
242
|
+
injections.apiClient.invoke.mockResolvedValue({
|
|
243
|
+
data: {
|
|
244
|
+
deliveries: null,
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
await vm.refreshCart();
|
|
248
|
+
|
|
249
|
+
expect(vm.shippingCosts.length).toEqual(0);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { createSharedComposable } from "@vueuse/core";
|
|
2
|
+
import { computed } from "vue";
|
|
3
|
+
import type { ComputedRef } from "vue";
|
|
4
|
+
import { useContext, useShopwareContext } from "#imports";
|
|
5
|
+
import type { Schemas, operations } from "#shopware";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Composable to manage cart
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
* @category Cart & Checkout
|
|
12
|
+
*/
|
|
13
|
+
export type UseCartReturn = {
|
|
14
|
+
/**
|
|
15
|
+
* Add product by id and quantity
|
|
16
|
+
*/
|
|
17
|
+
addProduct(params: {
|
|
18
|
+
id: string;
|
|
19
|
+
quantity?: number;
|
|
20
|
+
}): Promise<Schemas["Cart"]>;
|
|
21
|
+
/**
|
|
22
|
+
* Add products by array of items
|
|
23
|
+
*/
|
|
24
|
+
addProducts(
|
|
25
|
+
items: operations["addLineItem post /checkout/cart/line-item"]["body"]["items"],
|
|
26
|
+
): Promise<Schemas["Cart"]>;
|
|
27
|
+
/**
|
|
28
|
+
* Adds a promotion code to the cart
|
|
29
|
+
*/
|
|
30
|
+
addPromotionCode(promotionCode: string): Promise<Schemas["Cart"]>;
|
|
31
|
+
/**
|
|
32
|
+
* Lists all applied and active promotion codes
|
|
33
|
+
*/
|
|
34
|
+
appliedPromotionCodes: ComputedRef<Schemas["LineItem"][]>;
|
|
35
|
+
/**
|
|
36
|
+
* Current Cart object
|
|
37
|
+
*/
|
|
38
|
+
cart: ComputedRef<Schemas["Cart"] | undefined>;
|
|
39
|
+
/**
|
|
40
|
+
* All items in the cart
|
|
41
|
+
*/
|
|
42
|
+
cartItems: ComputedRef<Schemas["LineItem"][]>;
|
|
43
|
+
/**
|
|
44
|
+
* Changes the quantity of a product in the cart
|
|
45
|
+
*/
|
|
46
|
+
changeProductQuantity(params: {
|
|
47
|
+
id: string;
|
|
48
|
+
quantity: number;
|
|
49
|
+
}): Promise<Schemas["Cart"]>;
|
|
50
|
+
/**
|
|
51
|
+
* The number of items in the cart
|
|
52
|
+
*/
|
|
53
|
+
count: ComputedRef<number>;
|
|
54
|
+
/**
|
|
55
|
+
* Refreshes the cart object and related data
|
|
56
|
+
* If @param newCart is provided, it will be used as a new cart object
|
|
57
|
+
*/
|
|
58
|
+
refreshCart(newCart?: Schemas["Cart"]): Promise<Schemas["Cart"]>;
|
|
59
|
+
/**
|
|
60
|
+
* Removes the provided LineItem from the cart
|
|
61
|
+
*/
|
|
62
|
+
removeItem(lineItem: Schemas["LineItem"]): Promise<Schemas["Cart"]>;
|
|
63
|
+
/**
|
|
64
|
+
* The total price of the cart (including calculated costs like shipping)
|
|
65
|
+
*/
|
|
66
|
+
totalPrice: ComputedRef<number>;
|
|
67
|
+
/**
|
|
68
|
+
* Shipping price
|
|
69
|
+
*
|
|
70
|
+
* @deprecated Use `shippingCosts` instead
|
|
71
|
+
*/
|
|
72
|
+
shippingTotal: ComputedRef<number>;
|
|
73
|
+
/**
|
|
74
|
+
* Shipping costs
|
|
75
|
+
*/
|
|
76
|
+
shippingCosts: ComputedRef<Schemas["CartDelivery"][]>;
|
|
77
|
+
/**
|
|
78
|
+
* The total price of all cart items
|
|
79
|
+
*/
|
|
80
|
+
subtotal: ComputedRef<number>;
|
|
81
|
+
/**
|
|
82
|
+
* `true` if the cart contains no items
|
|
83
|
+
*/
|
|
84
|
+
isEmpty: ComputedRef<boolean>;
|
|
85
|
+
/**
|
|
86
|
+
* `true` if cart contains only digital items
|
|
87
|
+
*/
|
|
88
|
+
isVirtualCart: ComputedRef<boolean>;
|
|
89
|
+
/**
|
|
90
|
+
* Get cart errors
|
|
91
|
+
*/
|
|
92
|
+
consumeCartErrors(): Schemas["Cart"]["errors"];
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Cart management logic.
|
|
97
|
+
*
|
|
98
|
+
* Used as [Shared](https://frontends.shopware.com/framework/composables/shared-composables.html) Composable `useCart`
|
|
99
|
+
*
|
|
100
|
+
* @category Cart & Checkout
|
|
101
|
+
*/
|
|
102
|
+
export function useCartFunction(): UseCartReturn {
|
|
103
|
+
const { apiClient } = useShopwareContext();
|
|
104
|
+
|
|
105
|
+
const _storeCart = useContext<Schemas["Cart"]>("swCart");
|
|
106
|
+
const _storeCartErrors = useContext<Schemas["Cart"]["errors"] | null>(
|
|
107
|
+
"swCartErrors",
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
async function refreshCart(
|
|
111
|
+
newCart?: Schemas["Cart"],
|
|
112
|
+
): Promise<Schemas["Cart"]> {
|
|
113
|
+
if (newCart) {
|
|
114
|
+
_storeCart.value = newCart;
|
|
115
|
+
return newCart;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const { data } = await apiClient.invoke("readCart get /checkout/cart");
|
|
119
|
+
_storeCart.value = data;
|
|
120
|
+
setCartErrors(data);
|
|
121
|
+
return data;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function addProduct(params: {
|
|
125
|
+
id: string;
|
|
126
|
+
quantity?: number;
|
|
127
|
+
}): Promise<Schemas["Cart"]> {
|
|
128
|
+
return addProducts([
|
|
129
|
+
{
|
|
130
|
+
id: params.id,
|
|
131
|
+
quantity: params.quantity ?? 0,
|
|
132
|
+
type: "product",
|
|
133
|
+
},
|
|
134
|
+
]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Add multiple products to the cart
|
|
139
|
+
*
|
|
140
|
+
* @param {operations["addLineItem post /checkout/cart/line-item"]["body"]["items"]} items
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
async function addProducts(
|
|
144
|
+
items: operations["addLineItem post /checkout/cart/line-item"]["body"]["items"],
|
|
145
|
+
): Promise<Schemas["Cart"]> {
|
|
146
|
+
const { data: addToCartResult } = await apiClient.invoke(
|
|
147
|
+
"addLineItem post /checkout/cart/line-item",
|
|
148
|
+
{
|
|
149
|
+
body: {
|
|
150
|
+
items,
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
_storeCart.value = addToCartResult;
|
|
155
|
+
setCartErrors(addToCartResult);
|
|
156
|
+
return addToCartResult;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function removeItem(lineItem: Schemas["LineItem"]) {
|
|
160
|
+
const { data } = await apiClient.invoke(
|
|
161
|
+
"removeLineItem post /checkout/cart/line-item/delete",
|
|
162
|
+
{
|
|
163
|
+
body: { ids: [lineItem.id] },
|
|
164
|
+
},
|
|
165
|
+
);
|
|
166
|
+
_storeCart.value = data;
|
|
167
|
+
setCartErrors(data);
|
|
168
|
+
return data;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function changeProductQuantity(params: {
|
|
172
|
+
id: string;
|
|
173
|
+
quantity: number;
|
|
174
|
+
}) {
|
|
175
|
+
const { data } = await apiClient.invoke(
|
|
176
|
+
"updateLineItem patch /checkout/cart/line-item",
|
|
177
|
+
{
|
|
178
|
+
body: {
|
|
179
|
+
items: [
|
|
180
|
+
{
|
|
181
|
+
id: params.id,
|
|
182
|
+
quantity: +params.quantity,
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
);
|
|
188
|
+
_storeCart.value = data;
|
|
189
|
+
setCartErrors(data);
|
|
190
|
+
|
|
191
|
+
return data;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function submitPromotionCode(promotionCode: string) {
|
|
195
|
+
const { data } = await apiClient.invoke(
|
|
196
|
+
"addLineItem post /checkout/cart/line-item",
|
|
197
|
+
{
|
|
198
|
+
body: {
|
|
199
|
+
items: [
|
|
200
|
+
{
|
|
201
|
+
referencedId: promotionCode,
|
|
202
|
+
type: "promotion",
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
);
|
|
208
|
+
_storeCart.value = data;
|
|
209
|
+
setCartErrors(data);
|
|
210
|
+
return data;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const appliedPromotionCodes = computed(() => {
|
|
214
|
+
return cartItems.value.filter(
|
|
215
|
+
(cartItem: Schemas["LineItem"]) => cartItem.type === "promotion",
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const cart: ComputedRef<Schemas["Cart"] | undefined> = computed(
|
|
220
|
+
() => _storeCart.value,
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
const cartItems = computed<Schemas["LineItem"][]>(() => {
|
|
224
|
+
return cart.value?.lineItems || [];
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const count = computed(() => {
|
|
228
|
+
return cartItems.value.reduce(
|
|
229
|
+
(accumulator: number, lineItem: Schemas["LineItem"]) =>
|
|
230
|
+
lineItem.good === true ? lineItem.quantity + accumulator : accumulator,
|
|
231
|
+
0,
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const isEmpty = computed(() => count.value <= 0);
|
|
236
|
+
|
|
237
|
+
const totalPrice = computed(() => {
|
|
238
|
+
const cartPrice = cart.value?.price?.totalPrice;
|
|
239
|
+
return cartPrice || 0;
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
const shippingTotal = computed(() => {
|
|
243
|
+
const shippingTotal =
|
|
244
|
+
cart.value?.deliveries?.[0]?.shippingCosts?.totalPrice;
|
|
245
|
+
return shippingTotal || 0;
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const shippingCosts = computed(() => {
|
|
249
|
+
return cart.value?.deliveries || [];
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const subtotal = computed(() => {
|
|
253
|
+
const cartPrice = cart.value?.price?.positionPrice;
|
|
254
|
+
return cartPrice || 0;
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const isVirtualCart = computed(() => {
|
|
258
|
+
return (
|
|
259
|
+
cartItems.value.length > 0 &&
|
|
260
|
+
cartItems.value
|
|
261
|
+
.filter((element) => element.type !== "promotion")
|
|
262
|
+
.every((item) => item.states.includes("is-download"))
|
|
263
|
+
);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Add cart errors to the sharable variable
|
|
268
|
+
*
|
|
269
|
+
* @param {Cart} cart
|
|
270
|
+
*/
|
|
271
|
+
const setCartErrors = (cart: Schemas["Cart"]) => {
|
|
272
|
+
if (Object.keys(cart.errors || {}).length) {
|
|
273
|
+
_storeCartErrors.value = Object.assign(
|
|
274
|
+
_storeCartErrors.value ? _storeCartErrors.value : {},
|
|
275
|
+
cart.errors,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Get cart errors and clear variable
|
|
282
|
+
*
|
|
283
|
+
* @returns {CartErrors}
|
|
284
|
+
*/
|
|
285
|
+
const consumeCartErrors = () => {
|
|
286
|
+
const errors = _storeCartErrors.value
|
|
287
|
+
? JSON.parse(JSON.stringify(_storeCartErrors.value))
|
|
288
|
+
: null;
|
|
289
|
+
_storeCartErrors.value = null;
|
|
290
|
+
return errors;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
addProduct,
|
|
295
|
+
addProducts,
|
|
296
|
+
addPromotionCode: submitPromotionCode,
|
|
297
|
+
appliedPromotionCodes,
|
|
298
|
+
cart,
|
|
299
|
+
cartItems,
|
|
300
|
+
changeProductQuantity,
|
|
301
|
+
count,
|
|
302
|
+
refreshCart,
|
|
303
|
+
removeItem,
|
|
304
|
+
totalPrice,
|
|
305
|
+
shippingTotal,
|
|
306
|
+
subtotal,
|
|
307
|
+
isEmpty,
|
|
308
|
+
isVirtualCart,
|
|
309
|
+
consumeCartErrors,
|
|
310
|
+
shippingCosts,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export const useCart = createSharedComposable(useCartFunction);
|