diff --git a/src/endpoints/cart.js b/src/endpoints/cart.js index 7c69bbb..2a058f2 100644 --- a/src/endpoints/cart.js +++ b/src/endpoints/cart.js @@ -7,9 +7,14 @@ import { } from '../utils/helpers' class CartEndpoint extends BaseExtend { + /** + * @param {import('../factories/request').default} request - The RequestFactory instance + * @param {string} id - The cart ID + */ constructor(request, id) { super(...arguments) + /** @type {import('../factories/request').default} */ this.request = request this.cartId = id this.endpoint = 'carts' @@ -88,6 +93,38 @@ class CartEndpoint extends BaseExtend { ) } + AddProducts(products, options, token = null, additionalHeaders = {}) { + const items = products.map(product => { + const { id, sku, quantity = 1, ...rest } = product + const item = { + type: 'cart_item', + quantity, + ...rest + } + + if (id) { + item.id = id + } else if (sku) { + item.sku = sku + } + + return item + }) + + const body = options ? { data: items, options } : { data: items } + + return this.request.send( + `${this.endpoint}/${this.cartId}/items`, + 'POST', + body, + token, + null, + false, + null, + additionalHeaders + ) + } + AddCustomItem(body) { const itemObject = Object.assign(body, { type: 'custom_item' @@ -176,6 +213,13 @@ class CartEndpoint extends BaseExtend { ) } + RemovePromotion(promoCode) { + return this.request.send( + `${this.endpoint}/${this.cartId}/discounts/${promoCode}`, + 'DELETE' + ) + } + BulkAdd(body, options) { return this.request.send(`${this.endpoint}/${this.cartId}/items`, 'POST', { data: body, diff --git a/src/types/cart.d.ts b/src/types/cart.d.ts index 3002d33..78a71fd 100644 --- a/src/types/cart.d.ts +++ b/src/types/cart.d.ts @@ -15,6 +15,7 @@ import { Address } from './address' import { Price, FormattedPrice } from './price' import { Order } from './order' import { PcmProductResponse } from './pcm' +import { Promotion } from './promotions' export interface CheckoutCustomer { id: string @@ -58,6 +59,7 @@ export interface Cart { links?: {} meta?: { display_price?: { + discount?: FormattedPrice with_tax?: FormattedPrice without_tax?: FormattedPrice tax?: FormattedPrice @@ -165,6 +167,11 @@ export interface BulkAddOptions { add_all_or_nothing: boolean } +export type AddProductsItem = { + quantity?: number + [key: string]: any +} & ({ id: string; sku?: never } | { sku: string; id?: never }) + export interface BulkCustomDiscountOptions { add_all_or_nothing: boolean } @@ -199,7 +206,7 @@ export interface CartTaxItemObject { } } -export type CartInclude = 'items' | 'tax_items' +export type CartInclude = 'items' | 'tax_items' | 'promotions' interface CartQueryableResource extends QueryableResource { @@ -208,6 +215,7 @@ interface CartQueryableResource export interface CartIncluded { items: CartItem[] + promotions: Promotion[] } export interface CartAdditionalHeaders { @@ -385,6 +393,21 @@ export interface CartEndpoint additionalHeaders?: CartAdditionalHeaders ): Promise + /** + * Add Multiple Products to Cart + * Description: Add multiple products to cart in a single request. Each product can be identified by either ID or SKU. + * @param products Array of products to add, each with either an id or sku field + * @param options Optional bulk add options + * @param token Optional customer token + * @param additionalHeaders Optional additional headers + */ + AddProducts( + products: AddProductsItem[], + options?: BulkAddOptions, + token?: string, + additionalHeaders?: CartAdditionalHeaders + ): Promise + RemoveAllItems(): Promise /** @@ -418,6 +441,14 @@ export interface CartEndpoint */ AddPromotion(code: string): Promise + /** + * Remove promotion from Cart + * Description: Removes a manually applied promotion code from a cart. Does not work if the promotion is applied automatically. + * DOCS: https://developer.elasticpath.com/docs/api/carts/delete-a-promotion-via-promotion-code + * @param promoCode the promotion code to remove. + */ + RemovePromotion(promoCode: string): Promise<{}> + /** * Bulk Update Items to Cart * Description: When you enable the bulk update feature, a shopper can update an array of items to their cart in one action, rather than updating each item one at a time.