mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Added new normalizer for the cart to the UI
This commit is contained in:
@@ -69,22 +69,6 @@ export function normalizeProduct(productNode: any): Product {
|
||||
}
|
||||
|
||||
export function normalizeCart(data: BigcommerceCart): Cart {
|
||||
// const d: BaseCart = data && {
|
||||
// id: data.id,
|
||||
// customerId: String(data.customer_id),
|
||||
// email: data.email,
|
||||
// createdAt: data.created_time,
|
||||
// currency: data.currency,
|
||||
// taxesIncluded: data.tax_included,
|
||||
// lineItems: data.line_items as any,
|
||||
// lineItemsSubtotalPrice: data.base_amount,
|
||||
// subtotalPrice: data.base_amount + data.discount_amount,
|
||||
// totalPrice: data.cart_amount,
|
||||
// discounts: data.discounts?.map((discount) => ({
|
||||
// value: discount.discounted_amount,
|
||||
// })),
|
||||
// }
|
||||
|
||||
return {
|
||||
id: data.id,
|
||||
customerId: String(data.customer_id),
|
||||
@@ -92,7 +76,7 @@ export function normalizeCart(data: BigcommerceCart): Cart {
|
||||
createdAt: data.created_time,
|
||||
currency: data.currency,
|
||||
taxesIncluded: data.tax_included,
|
||||
lineItems: data.line_items.physical_items.map(itemsToProducts) as any,
|
||||
lineItems: data.line_items.physical_items.map(normalizeLineItem),
|
||||
lineItemsSubtotalPrice: data.base_amount,
|
||||
subtotalPrice: data.base_amount + data.discount_amount,
|
||||
totalPrice: data.cart_amount,
|
||||
@@ -100,52 +84,28 @@ export function normalizeCart(data: BigcommerceCart): Cart {
|
||||
value: discount.discounted_amount,
|
||||
})),
|
||||
}
|
||||
|
||||
// return update(data as any, {
|
||||
// $auto: {
|
||||
// items: { $set: data?.line_items?.physical_items?.map(itemsToProducts) },
|
||||
// subTotal: { $set: data?.base_amount },
|
||||
// total: { $set: data?.cart_amount },
|
||||
// },
|
||||
// $unset: ['created_time', 'coupons', 'line_items', 'email'],
|
||||
// })
|
||||
}
|
||||
|
||||
function itemsToProducts(item: any): CartItem {
|
||||
const {
|
||||
id,
|
||||
name,
|
||||
quantity,
|
||||
product_id,
|
||||
variant_id,
|
||||
image_url,
|
||||
list_price,
|
||||
sale_price,
|
||||
extended_list_price,
|
||||
extended_sale_price,
|
||||
...rest
|
||||
} = item
|
||||
|
||||
return update(item, {
|
||||
$auto: {
|
||||
prices: {
|
||||
$auto: {
|
||||
listPrice: { $set: list_price },
|
||||
salePrice: { $set: sale_price },
|
||||
extendedListPrice: { $set: extended_list_price },
|
||||
extendedSalePrice: { $set: extended_sale_price },
|
||||
},
|
||||
function normalizeLineItem(item: any): LineItem {
|
||||
return {
|
||||
id: item.id,
|
||||
variantId: String(item.variant_id),
|
||||
name: item.name,
|
||||
quantity: item.quantity,
|
||||
variant: {
|
||||
id: String(item.variant_id),
|
||||
sku: item.sku,
|
||||
name: item.name,
|
||||
image: {
|
||||
url: item.image_url,
|
||||
},
|
||||
images: {
|
||||
$set: [
|
||||
{
|
||||
alt: name,
|
||||
url: image_url,
|
||||
},
|
||||
],
|
||||
},
|
||||
productId: { $set: product_id },
|
||||
variantId: { $set: variant_id },
|
||||
requiresShipping: item.is_require_shipping,
|
||||
price: item.sale_price,
|
||||
listPrice: item.list_price,
|
||||
},
|
||||
})
|
||||
path: item.url.split('/')[3],
|
||||
discounts: item.discounts.map((discount: any) => ({
|
||||
value: discount.discounted_amount,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
6
framework/bigcommerce/types.d.ts
vendored
6
framework/bigcommerce/types.d.ts
vendored
@@ -1 +1,5 @@
|
||||
interface Cart extends BaseCart {}
|
||||
interface Cart extends BaseCart {
|
||||
lineItems: LineItem[]
|
||||
}
|
||||
|
||||
interface LineItem extends BaseLineItem {}
|
||||
|
20
framework/commerce/types.d.ts
vendored
20
framework/commerce/types.d.ts
vendored
@@ -56,6 +56,8 @@ interface BaseLineItem {
|
||||
name: string
|
||||
quantity: number
|
||||
discounts: DiscountBase[]
|
||||
// A human-friendly unique string automatically generated from the product’s name
|
||||
path: string
|
||||
variant: BaseProductVariant
|
||||
}
|
||||
|
||||
@@ -77,16 +79,20 @@ interface BaseProductVariant {
|
||||
sku: string
|
||||
// The product variant’s title, or the product's name.
|
||||
name: string
|
||||
// Indicates whether this product variant is in stock.
|
||||
isInStock: boolean
|
||||
// Indicates if the product variant is available for sale.
|
||||
availableForSale: boolean
|
||||
// Whether a customer needs to provide a shipping address when placing
|
||||
// an order for the product variant.
|
||||
requiresShipping: boolean
|
||||
// Image associated with the product variant. Falls back to the product image
|
||||
// if no image is available.
|
||||
image: Image
|
||||
// Whether a customer needs to provide a shipping address when placing
|
||||
// an order for the product variant.
|
||||
requiresShipping: boolean
|
||||
// The product variant’s price after all discounts are applied.
|
||||
price: number
|
||||
// Product variant’s price, as quoted by the manufacturer/distributor.
|
||||
listPrice: number
|
||||
// Indicates whether this product variant is in stock.
|
||||
isInStock?: boolean
|
||||
// Indicates if the product variant is available for sale.
|
||||
availableForSale?: boolean
|
||||
// The variant's weight. If a weight was not explicitly specified on the
|
||||
// variant this will be the product's weight.
|
||||
weight?: Measurement
|
||||
|
Reference in New Issue
Block a user