diff --git a/api/shopware-6-client.api.md b/api/shopware-6-client.api.md index 4ca8859d0..d3836dc9b 100644 --- a/api/shopware-6-client.api.md +++ b/api/shopware-6-client.api.md @@ -16,6 +16,7 @@ import { CustomerAddress } from '@shopware-pwa/commons/interfaces/models/checkou import { CustomerRegistrationParams } from '@shopware-pwa/commons/interfaces/request/CustomerRegistrationParams'; import { EntityResult } from '@shopware-pwa/commons/interfaces/response/EntityResult'; import { Language } from '@shopware-pwa/commons/interfaces/models/framework/language/Language'; +import { LineItem } from '@shopware-pwa/commons/interfaces/models/checkout/cart/line-item/LineItem'; import { Order } from '@shopware-pwa/commons/interfaces/models/checkout/order/Order'; import { PageResolverProductResult } from '@shopware-pwa/commons/interfaces/models/content/cms/CmsPage'; import { PageResolverResult } from '@shopware-pwa/commons/interfaces/models/content/cms/CmsPage'; @@ -32,6 +33,9 @@ import { ShopwareSearchParams } from '@shopware-pwa/commons/interfaces/search/Se import { StoreNavigationElement } from '@shopware-pwa/commons/interfaces/models/content/navigation/Navigation'; import { StoreNavigationType } from '@shopware-pwa/commons/interfaces/models/content/navigation/Navigation'; +// @beta +export function addCartItems(items: Partial[], contextInstance?: ShopwareApiInstance): Promise; + // @beta export function addProductReview(productId: string, productReviewData: { title: string; diff --git a/docs/landing/resources/api/shopware-6-client.addcartitems.md b/docs/landing/resources/api/shopware-6-client.addcartitems.md new file mode 100644 index 000000000..a2f1aae42 --- /dev/null +++ b/docs/landing/resources/api/shopware-6-client.addcartitems.md @@ -0,0 +1,32 @@ + + +[Home](./index.md) > [@shopware-pwa/shopware-6-client](./shopware-6-client.md) > [addCartItems](./shopware-6-client.addcartitems.md) + +## addCartItems() function + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Adds multiple items to the cart. Accepts every type of cart item. + +Signature: + +```typescript +export declare function addCartItems(items: Partial[], contextInstance?: ShopwareApiInstance): Promise; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| items | Partial<LineItem>\[\] | | +| contextInstance | [ShopwareApiInstance](./shopware-6-client.shopwareapiinstance.md) | | + +Returns: + +Promise<any> + +## Exceptions + +ClientApiError + diff --git a/docs/landing/resources/api/shopware-6-client.md b/docs/landing/resources/api/shopware-6-client.md index 67f120f43..7a90a1975 100644 --- a/docs/landing/resources/api/shopware-6-client.md +++ b/docs/landing/resources/api/shopware-6-client.md @@ -8,6 +8,7 @@ | Function | Description | | --- | --- | +| [addCartItems(items, contextInstance)](./shopware-6-client.addcartitems.md) | (BETA) Adds multiple items to the cart. Accepts every type of cart item. | | [addProductReview(productId, productReviewData, contextInstance)](./shopware-6-client.addproductreview.md) | (BETA) Add a review to specific product by its ID | | [addProductToCart(productId, quantity, contextInstance)](./shopware-6-client.addproducttocart.md) | (BETA) TODO: https://github.com/vuestorefront/shopware-pwa/issues/1449Adds specific quantity of the product to the cart by productId. It creates a new cart line item.Warning: This method does not change the state of the cart in any way if productId already exists in a cart. For changing the quantity use addQuantityToCartLineItem() or changeCartLineItemQuantity() methods. | | [addPromotionCode(promotionCode, contextInstance)](./shopware-6-client.addpromotioncode.md) | (BETA) Adds new promotion code to the cart by its code.Promotion code is being added as separate cart item line. | diff --git a/packages/shopware-6-client/__tests__/services/CartService/addCartItems.spec.ts b/packages/shopware-6-client/__tests__/services/CartService/addCartItems.spec.ts new file mode 100644 index 000000000..cfc238a2b --- /dev/null +++ b/packages/shopware-6-client/__tests__/services/CartService/addCartItems.spec.ts @@ -0,0 +1,43 @@ +import { addCartItems } from "../../../src"; +import { defaultInstance } from "../../../src/apiService"; + +jest.mock("../../../src/apiService"); +const mockedApiInstance = defaultInstance as jest.Mocked< + typeof defaultInstance +>; + +describe("CartService - addCartItems", () => { + const mockedPost = jest.fn(); + beforeEach(() => { + jest.resetAllMocks(); + mockedApiInstance.invoke = { + post: mockedPost, + } as any; + }); + + it("should call valid endpoint and return a cart", async () => { + mockedPost.mockResolvedValueOnce({ + data: {}, + }); + + await addCartItems([ + { + type: "product", + }, + { + type: "credit", + }, + ]); + expect(mockedPost).toBeCalledTimes(1); + expect(mockedPost).toBeCalledWith("/store-api/checkout/cart/line-item", { + items: [ + { + type: "product", + }, + { + type: "credit", + }, + ], + }); + }); +}); diff --git a/packages/shopware-6-client/src/services/cartService.ts b/packages/shopware-6-client/src/services/cartService.ts index 34603fc34..219e5ff68 100644 --- a/packages/shopware-6-client/src/services/cartService.ts +++ b/packages/shopware-6-client/src/services/cartService.ts @@ -144,3 +144,24 @@ export async function addPromotionCode( return resp.data; } + +/** + * Adds multiple items to the cart. + * Accepts every type of cart item. + * + * @throws ClientApiError + * @beta + */ +export async function addCartItems( + items: Partial[], + contextInstance: ShopwareApiInstance = defaultInstance +) { + const resp = await contextInstance.invoke.post( + getCheckoutCartLineItemEndpoint(), + { + items, + } + ); + + return resp.data; +}