Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit e639977

Browse files
feat: request scoped headers and merge cart (#785)
* Add optional header parameter to the addProduct function in cart module * Add the request scoped header to product module * Added bulk add item tax * Added the request scoped header as a parameter in cart checkout * Added request scoped header to updateItem in cart for multicurrency * Feat: Merging Carts --------- Co-authored-by: Sam Blacklock <[email protected]>
1 parent cf13b55 commit e639977

File tree

5 files changed

+123
-12
lines changed

5 files changed

+123
-12
lines changed

src/endpoints/cart.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,18 @@ class CartEndpoint extends BaseExtend {
141141
return this.request.send(`${this.endpoint}/${this.cartId}/items`, 'DELETE')
142142
}
143143

144-
UpdateItem(itemId, quantity, data = {}) {
144+
UpdateItem(itemId, quantity, data = {}, additionalHeaders = {}) {
145145
const body = buildCartItemData(itemId, quantity)
146146

147147
return this.request.send(
148148
`${this.endpoint}/${this.cartId}/items/${itemId}`,
149149
'PUT',
150-
{ ...body, ...data }
150+
{ ...body, ...data },
151+
null,
152+
null,
153+
true,
154+
null,
155+
additionalHeaders
151156
)
152157
}
153158

@@ -185,6 +190,13 @@ class CartEndpoint extends BaseExtend {
185190
)
186191
}
187192

193+
BulkAddItemTax(body, options) {
194+
return this.request.send(`${this.endpoint}/${this.cartId}/taxes`, 'POST', {
195+
data: body,
196+
...(options && { options })
197+
})
198+
}
199+
188200
UpdateItemTax(itemId, taxItemId, taxData) {
189201
const body = Object.assign(taxData, {
190202
type: 'tax_item'
@@ -204,7 +216,12 @@ class CartEndpoint extends BaseExtend {
204216
)
205217
}
206218

207-
Checkout(customer, billing_address, shipping_address = billing_address) {
219+
Checkout(
220+
customer,
221+
billing_address,
222+
shipping_address = billing_address,
223+
additionalHeaders = {}
224+
) {
208225
const body = buildCartCheckoutData(
209226
customer,
210227
billing_address,
@@ -214,7 +231,29 @@ class CartEndpoint extends BaseExtend {
214231
return this.request.send(
215232
`${this.endpoint}/${this.cartId}/checkout`,
216233
'POST',
217-
body
234+
body,
235+
null,
236+
null,
237+
true,
238+
null,
239+
additionalHeaders
240+
)
241+
}
242+
243+
Merge(cartId, token, options = {}) {
244+
const body = {
245+
type: 'cart_items',
246+
cart_id: `${cartId}`
247+
}
248+
249+
return this.request.send(
250+
`${this.endpoint}/${this.cartId}/items`,
251+
'POST',
252+
{
253+
data: body,
254+
...(options && { options })
255+
},
256+
token
218257
)
219258
}
220259

src/endpoints/catalogs.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,24 @@ class Products extends CRUDExtend {
104104
`catalogs/${this.endpoint}`,
105105
'GET',
106106
undefined,
107-
token
107+
token,
108+
undefined,
109+
true,
110+
null,
111+
additionalHeaders
108112
)
109113
}
110114

111-
Get({ productId, token = null }) {
115+
Get({ productId, token = null, additionalHeaders = {} }) {
112116
return this.request.send(
113117
`catalogs/${this.endpoint}/${productId}`,
114118
'GET',
115119
undefined,
116-
token
120+
token,
121+
undefined,
122+
true,
123+
null,
124+
additionalHeaders
117125
)
118126
}
119127

@@ -122,7 +130,11 @@ class Products extends CRUDExtend {
122130
`catalogs/${catalogId}/releases/${releaseId}/${this.endpoint}/${productId}`,
123131
'GET',
124132
undefined,
125-
token
133+
token,
134+
undefined,
135+
true,
136+
null,
137+
additionalHeaders
126138
)
127139
}
128140

@@ -152,7 +164,11 @@ class Products extends CRUDExtend {
152164
`catalogs/nodes/${nodeId}/relationships/${this.endpoint}`,
153165
'GET',
154166
undefined,
155-
token
167+
token,
168+
undefined,
169+
true,
170+
null,
171+
additionalHeaders
156172
)
157173
}
158174

src/types/cart.d.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Resource, QueryableResource } from './core'
88
import { Address } from './address'
99
import { Price, FormattedPrice } from './price'
1010
import { Order } from './order'
11+
import { PcmProductResponse } from './pcm'
1112

1213
export interface CheckoutCustomer {
1314
id: string
@@ -143,6 +144,9 @@ export interface BulkAddOptions {
143144
add_all_or_nothing: boolean
144145
}
145146

147+
export interface MergeCartOptions {
148+
add_all_or_nothing: boolean
149+
}
146150
export interface CartItemObject {
147151
type: string
148152
name?: string
@@ -154,6 +158,22 @@ export interface CartItemObject {
154158
code?: string
155159
}
156160

161+
export interface CartTaxItemObject {
162+
type: string
163+
name: string
164+
jurisdiction: string
165+
code: string
166+
rate: number
167+
relationships: {
168+
item: {
169+
data: {
170+
type: string
171+
id: string
172+
}
173+
}
174+
}
175+
}
176+
157177
export type CartInclude = 'items' | 'tax_items'
158178

159179
interface CartQueryableResource<R, F, S>
@@ -282,7 +302,8 @@ export interface CartEndpoint
282302
UpdateItem(
283303
itemId: string,
284304
quantity: number,
285-
customData?: any
305+
customData?: any,
306+
additionalHeaders?: CartAdditionalHeaders
286307
): Promise<CartItemsResponse>
287308

288309
/**
@@ -429,6 +450,17 @@ export interface CartEndpoint
429450
taxData: ItemTaxObject
430451
): Promise<Resource<ItemTaxObjectResponse>>
431452

453+
/**
454+
* Bulk Add Items tax to Cart
455+
* Description: When you enable the bulk add feature, a shopper can add an array of items to their cart in one action, rather than adding each item one at a time.
456+
* DOCS: https://documentation.elasticpath.com/commerce-cloud/docs/api/carts-and-orders/carts/bulk-add-to-cart.html
457+
* @param data Cart items or custom items
458+
* @param options Optional config object for add to cart behaviour
459+
*/
460+
BulkAddItemTax(
461+
data: CartTaxItemObject[],
462+
options?: BulkAddOptions
463+
): Promise<CartTaxItemObject[]>
432464
/**
433465
* Update a Tax Item
434466
* DOCS: https://documentation.elasticpath.com/commerce-cloud/docs/api/carts-and-orders/carts/cart-items/tax-items/update-a-tax-item.html
@@ -463,9 +495,25 @@ export interface CartEndpoint
463495
Checkout(
464496
customer: string | CheckoutCustomer | CheckoutCustomerObject,
465497
billingAddress: Partial<Address>,
466-
shippingAddress?: Partial<Address>
498+
shippingAddress?: Partial<Address>,
499+
additionalHeaders?: CartAdditionalHeaders
467500
): Promise<Resource<Order>>
468501

502+
/**
503+
* Merge
504+
* Description: Allows to merge two carts. Moves the cart items from one cart to another.
505+
* If both the cart items are same, the cart items quantity will be increased
506+
* DOCS: https://elasticpath.dev/docs/carts/cart-items/merging-carts.html
507+
* @param cartId the cart Id of the cart to be merged.
508+
* @param token the customer token of the cart to whom it is associated or to be associated with
509+
* @param options When true, if an error occurs for any item, no items are added to the cart. When false, valid items are added to the cart and the items with errors are reported in the response. Default is true
510+
*/
511+
512+
Merge(
513+
cartId: string,
514+
token?: string,
515+
options?: MergeCartOptions
516+
): Promise<PcmProductResponse[]>
469517
/**
470518
* Delete a Cart
471519
* Description: You can easily remove all items from a cart.

src/types/catalog.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ type ShopperCatalogProductsInclude =
7979
interface ShopperCatalogAdditionalHeaders {
8080
'EP-Context-Tag'?: string
8181
'EP-Channel'?: string
82+
'X-MOLTIN-LANGUAGE'?: string
83+
'X-MOLTIN-CURRENCY'?: string
8284
}
8385

8486
interface ShopperCatalogProductsQueryableResource<

src/types/catalogs-products.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,23 @@ export interface CatalogsProductsEndpoint {
134134

135135
Filter(filter: ProductFilter): CatalogsProductsEndpoint
136136

137-
All(options: { token?: string }): Promise<ResourceList<ProductResponse>>
137+
All(options: {
138+
token?: string
139+
additionalHeaders?: any
140+
}): Promise<ResourceList<ProductResponse>>
138141

139142
Get(options: {
140143
productId: string
141144
token?: string
145+
additionalHeaders?: any
142146
}): Promise<Resource<ProductResponse>>
143147

144148
GetProduct(options: {
145149
catalogId: string
146150
releaseId: string
147151
productId: string
148152
token?: string
153+
additionalHeaders?: any
149154
}): Promise<Resource<ProductResponse>>
150155

151156
GetCatalogNodeProducts(options: {
@@ -159,6 +164,7 @@ export interface CatalogsProductsEndpoint {
159164
GetProductsByNode(options: {
160165
nodeId: string
161166
token?: string
167+
additionalHeaders?: any
162168
}): Promise<ResourceList<ProductResponse>>
163169

164170
GetCatalogProducts(options: {

0 commit comments

Comments
 (0)