Skip to content
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
44 changes: 44 additions & 0 deletions src/endpoints/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 32 additions & 1 deletion src/types/cart.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,6 +59,7 @@ export interface Cart {
links?: {}
meta?: {
display_price?: {
discount?: FormattedPrice
with_tax?: FormattedPrice
without_tax?: FormattedPrice
tax?: FormattedPrice
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -199,7 +206,7 @@ export interface CartTaxItemObject {
}
}

export type CartInclude = 'items' | 'tax_items'
export type CartInclude = 'items' | 'tax_items' | 'promotions'

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

export interface CartIncluded {
items: CartItem[]
promotions: Promotion[]
}

export interface CartAdditionalHeaders {
Expand Down Expand Up @@ -385,6 +393,21 @@ export interface CartEndpoint
additionalHeaders?: CartAdditionalHeaders
): Promise<CartItemsResponse>

/**
* 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<CartItemsResponse>

RemoveAllItems(): Promise<CartItemsResponse>

/**
Expand Down Expand Up @@ -418,6 +441,14 @@ export interface CartEndpoint
*/
AddPromotion(code: string): Promise<CartItemsResponse>

/**
* 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.
Expand Down