Skip to content

Commit f6d8298

Browse files
committed
feat: support adding products and removing promotions from cart
1 parent db13300 commit f6d8298

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/endpoints/cart.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import {
77
} from '../utils/helpers'
88

99
class CartEndpoint extends BaseExtend {
10+
/**
11+
* @param {import('../factories/request').default} request - The RequestFactory instance
12+
* @param {string} id - The cart ID
13+
*/
1014
constructor(request, id) {
1115
super(...arguments)
1216

17+
/** @type {import('../factories/request').default} */
1318
this.request = request
1419
this.cartId = id
1520
this.endpoint = 'carts'
@@ -88,6 +93,38 @@ class CartEndpoint extends BaseExtend {
8893
)
8994
}
9095

96+
AddProducts(products, options, token = null, additionalHeaders = {}) {
97+
const items = products.map(product => {
98+
const { id, sku, quantity = 1, ...rest } = product
99+
const item = {
100+
type: 'cart_item',
101+
quantity,
102+
...rest
103+
}
104+
105+
if (id) {
106+
item.id = id
107+
} else if (sku) {
108+
item.sku = sku
109+
}
110+
111+
return item
112+
})
113+
114+
const body = options ? { data: items, options } : { data: items }
115+
116+
return this.request.send(
117+
`${this.endpoint}/${this.cartId}/items`,
118+
'POST',
119+
body,
120+
token,
121+
null,
122+
false,
123+
null,
124+
additionalHeaders
125+
)
126+
}
127+
91128
AddCustomItem(body) {
92129
const itemObject = Object.assign(body, {
93130
type: 'custom_item'
@@ -176,6 +213,13 @@ class CartEndpoint extends BaseExtend {
176213
)
177214
}
178215

216+
RemovePromotion(promoCode) {
217+
return this.request.send(
218+
`${this.endpoint}/${this.cartId}/discounts/${promoCode}`,
219+
'DELETE'
220+
)
221+
}
222+
179223
BulkAdd(body, options) {
180224
return this.request.send(`${this.endpoint}/${this.cartId}/items`, 'POST', {
181225
data: body,

src/types/cart.d.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Address } from './address'
1515
import { Price, FormattedPrice } from './price'
1616
import { Order } from './order'
1717
import { PcmProductResponse } from './pcm'
18+
import { Promotion } from './promotions'
1819

1920
export interface CheckoutCustomer {
2021
id: string
@@ -58,6 +59,7 @@ export interface Cart {
5859
links?: {}
5960
meta?: {
6061
display_price?: {
62+
discount?: FormattedPrice
6163
with_tax?: FormattedPrice
6264
without_tax?: FormattedPrice
6365
tax?: FormattedPrice
@@ -165,6 +167,11 @@ export interface BulkAddOptions {
165167
add_all_or_nothing: boolean
166168
}
167169

170+
export type AddProductsItem = {
171+
quantity?: number
172+
[key: string]: any
173+
} & ({ id: string; sku?: never } | { sku: string; id?: never })
174+
168175
export interface BulkCustomDiscountOptions {
169176
add_all_or_nothing: boolean
170177
}
@@ -199,7 +206,7 @@ export interface CartTaxItemObject {
199206
}
200207
}
201208

202-
export type CartInclude = 'items' | 'tax_items'
209+
export type CartInclude = 'items' | 'tax_items' | 'promotions'
203210

204211
interface CartQueryableResource<R, F, S>
205212
extends QueryableResource<Cart, F, S, CartInclude> {
@@ -208,6 +215,7 @@ interface CartQueryableResource<R, F, S>
208215

209216
export interface CartIncluded {
210217
items: CartItem[]
218+
promotions: Promotion[]
211219
}
212220

213221
export interface CartAdditionalHeaders {
@@ -385,6 +393,21 @@ export interface CartEndpoint
385393
additionalHeaders?: CartAdditionalHeaders
386394
): Promise<CartItemsResponse>
387395

396+
/**
397+
* Add Multiple Products to Cart
398+
* Description: Add multiple products to cart in a single request. Each product can be identified by either ID or SKU.
399+
* @param products Array of products to add, each with either an id or sku field
400+
* @param options Optional bulk add options
401+
* @param token Optional customer token
402+
* @param additionalHeaders Optional additional headers
403+
*/
404+
AddProducts(
405+
products: AddProductsItem[],
406+
options?: BulkAddOptions,
407+
token?: string,
408+
additionalHeaders?: CartAdditionalHeaders
409+
): Promise<CartItemsResponse>
410+
388411
RemoveAllItems(): Promise<CartItemsResponse>
389412

390413
/**
@@ -418,6 +441,14 @@ export interface CartEndpoint
418441
*/
419442
AddPromotion(code: string): Promise<CartItemsResponse>
420443

444+
/**
445+
* Remove promotion from Cart
446+
* Description: Removes a manually applied promotion code from a cart. Does not work if the promotion is applied automatically.
447+
* DOCS: https://developer.elasticpath.com/docs/api/carts/delete-a-promotion-via-promotion-code
448+
* @param promoCode the promotion code to remove.
449+
*/
450+
RemovePromotion(promoCode: string): Promise<{}>
451+
421452
/**
422453
* Bulk Update Items to Cart
423454
* 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.

0 commit comments

Comments
 (0)