Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

feat(client): allow to add multiple cart items #1599

Merged
merged 2 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<LineItem>[], contextInstance?: ShopwareApiInstance): Promise<any>;

// @beta
export function addProductReview(productId: string, productReviewData: {
title: string;
Expand Down
32 changes: 32 additions & 0 deletions docs/landing/resources/api/shopware-6-client.addcartitems.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/shopware-6-client](./shopware-6-client.md) &gt; [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.

<b>Signature:</b>

```typescript
export declare function addCartItems(items: Partial<LineItem>[], contextInstance?: ShopwareApiInstance): Promise<any>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| items | Partial&lt;LineItem&gt;\[\] | |
| contextInstance | [ShopwareApiInstance](./shopware-6-client.shopwareapiinstance.md) | |

<b>Returns:</b>

Promise&lt;any&gt;

## Exceptions

ClientApiError

1 change: 1 addition & 0 deletions docs/landing/resources/api/shopware-6-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

| Function | Description |
| --- | --- |
| [addCartItems(items, contextInstance)](./shopware-6-client.addcartitems.md) | <b><i>(BETA)</i></b> Adds multiple items to the cart. Accepts every type of cart item. |
| [addProductReview(productId, productReviewData, contextInstance)](./shopware-6-client.addproductreview.md) | <b><i>(BETA)</i></b> Add a review to specific product by its ID |
| [addProductToCart(productId, quantity, contextInstance)](./shopware-6-client.addproducttocart.md) | <b><i>(BETA)</i></b> TODO: https://github.com/vuestorefront/shopware-pwa/issues/1449<!-- -->Adds 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) | <b><i>(BETA)</i></b> Adds new promotion code to the cart by its code.<!-- -->Promotion code is being added as separate cart item line. |
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
},
],
});
});
});
21 changes: 21 additions & 0 deletions packages/shopware-6-client/src/services/cartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LineItem>[],
contextInstance: ShopwareApiInstance = defaultInstance
) {
const resp = await contextInstance.invoke.post(
getCheckoutCartLineItemEndpoint(),
{
items,
}
);

return resp.data;
}