mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Merge remote-tracking branch 'origin' into CPP-153
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
ADD_ON_PRODUCT_TYPES,
|
||||
AVAILABILITY_FILTER_ID,
|
||||
HIDDEN_PRODUCT_TAG,
|
||||
MAKE_FILTER_ID,
|
||||
@@ -41,7 +42,6 @@ import {
|
||||
Address,
|
||||
Cart,
|
||||
CartAttributeInput,
|
||||
CartItem,
|
||||
Collection,
|
||||
Connection,
|
||||
Customer,
|
||||
@@ -84,6 +84,7 @@ import {
|
||||
ShopifyRemoveFromCartOperation,
|
||||
ShopifySetCartAttributesOperation,
|
||||
ShopifyUpdateCartOperation,
|
||||
TransmissionType,
|
||||
ShopifyCustomer,
|
||||
ShopifyOrder,
|
||||
ShopifyAddress,
|
||||
@@ -260,7 +261,7 @@ const reshapeCart = (cart: ShopifyCart): Cart => {
|
||||
...lineItem,
|
||||
merchandise: {
|
||||
...lineItem.merchandise,
|
||||
product: reshapeProduct(lineItem.merchandise.product)
|
||||
product: lineItem.merchandise.product
|
||||
}
|
||||
}))
|
||||
};
|
||||
@@ -273,6 +274,7 @@ const reshapeCollection = (collection: ShopifyCollection): Collection | undefine
|
||||
|
||||
return {
|
||||
...collection,
|
||||
helpfulLinks: parseMetaFieldValue<string[]>(collection.helpfulLinks),
|
||||
path: `/search/${collection.handle}`
|
||||
};
|
||||
};
|
||||
@@ -374,18 +376,29 @@ const reshapeImages = (images: Connection<Image>, productTitle: string) => {
|
||||
};
|
||||
|
||||
const reshapeVariants = (variants: ShopifyProductVariant[]): ProductVariant[] => {
|
||||
return variants.map((variant) => ({
|
||||
return variants.map(({ addOnProductId, addOnQuantity, ...variant }) => ({
|
||||
...variant,
|
||||
waiverAvailable: parseMetaFieldValue<boolean>(variant.waiverAvailable),
|
||||
coreVariantId: variant.coreVariantId?.value || null,
|
||||
coreCharge: parseMetaFieldValue<Money>(variant.coreCharge),
|
||||
mileage: variant.mileage?.value ?? null,
|
||||
estimatedDelivery: variant.estimatedDelivery?.value || null,
|
||||
condition: variant.condition?.value || null
|
||||
condition: variant.condition?.value || null,
|
||||
...(addOnProductId
|
||||
? {
|
||||
addOnProduct: {
|
||||
id: addOnProductId.value,
|
||||
quantity: addOnQuantity?.value ? Number(addOnQuantity.value) : 1
|
||||
}
|
||||
}
|
||||
: {})
|
||||
}));
|
||||
};
|
||||
|
||||
const reshapeProduct = (product: ShopifyProduct, filterHiddenProducts: boolean = true) => {
|
||||
const reshapeProduct = (
|
||||
product: ShopifyProduct,
|
||||
filterHiddenProducts: boolean = true
|
||||
): Product | undefined => {
|
||||
if (!product || (filterHiddenProducts && product.tags.includes(HIDDEN_PRODUCT_TAG))) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -393,6 +406,13 @@ const reshapeProduct = (product: ShopifyProduct, filterHiddenProducts: boolean =
|
||||
const { images, variants, ...rest } = product;
|
||||
return {
|
||||
...rest,
|
||||
transmissionCode: parseMetaFieldValue<string[]>(product.transmissionCode),
|
||||
transmissionSpeeds: parseMetaFieldValue<number[]>(product.transmissionSpeeds),
|
||||
transmissionTag: parseMetaFieldValue<string[]>(product.transmissionTag),
|
||||
driveType: parseMetaFieldValue<string[]>(product.driveType),
|
||||
transmissionType: product.transmissionType
|
||||
? (product.transmissionType.value as TransmissionType)
|
||||
: null,
|
||||
engineCylinders: parseMetaFieldValue<number[]>(product.engineCylinders),
|
||||
fuelType: product.fuelType?.value || null,
|
||||
images: reshapeImages(images, product.title),
|
||||
@@ -633,20 +653,34 @@ export async function getCart(cartId: string): Promise<Cart | undefined> {
|
||||
const cart = reshapeCart(res.body.data.cart);
|
||||
|
||||
// attach core charge as an additional attribute of a cart line, and remove the core charge line from cart
|
||||
const extendedCartLines = cart?.lines.reduce((lines, item) => {
|
||||
const coreVariantId = item.merchandise.coreVariantId?.value;
|
||||
if (coreVariantId) {
|
||||
const relatedCoreCharge = cart.lines.find((line) => line.merchandise.id === coreVariantId);
|
||||
return lines.concat([
|
||||
{
|
||||
...item,
|
||||
coreCharge: relatedCoreCharge
|
||||
}
|
||||
]);
|
||||
}
|
||||
const extendedCartLines = cart?.lines
|
||||
.map((item) => {
|
||||
const coreVariantId = item.merchandise.coreVariantId?.value;
|
||||
const addOnProductId = item.merchandise.addOnProductId;
|
||||
const _item = { ...item };
|
||||
|
||||
return lines;
|
||||
}, [] as CartItem[]);
|
||||
if (coreVariantId) {
|
||||
const relatedCoreCharge = cart.lines.find((line) => line.merchandise.id === coreVariantId);
|
||||
_item.coreCharge = relatedCoreCharge;
|
||||
}
|
||||
|
||||
if (addOnProductId) {
|
||||
const relatedAddOnProduct = cart.lines.find(
|
||||
(line) => line.merchandise.id === addOnProductId.value
|
||||
);
|
||||
_item.addOnProduct = relatedAddOnProduct
|
||||
? {
|
||||
...relatedAddOnProduct,
|
||||
quantity: item.merchandise.addOnQuantity
|
||||
? Number(item.merchandise.addOnQuantity.value)
|
||||
: 1
|
||||
}
|
||||
: undefined;
|
||||
}
|
||||
return _item;
|
||||
})
|
||||
// core charge shouldn't present as a dedicated product as it's tightly coupled with the product
|
||||
.filter((item) => item.merchandise.product.productType !== ADD_ON_PRODUCT_TYPES.coreCharge);
|
||||
|
||||
const totalQuantity = extendedCartLines.reduce((sum, line) => sum + line.quantity, 0);
|
||||
|
||||
@@ -730,7 +764,8 @@ export async function getCollections(): Promise<Collection[]> {
|
||||
description: 'All products'
|
||||
},
|
||||
path: '/search',
|
||||
updatedAt: new Date().toISOString()
|
||||
updatedAt: new Date().toISOString(),
|
||||
helpfulLinks: null
|
||||
},
|
||||
// Filter out the `hidden` collections.
|
||||
// Collections that start with `hidden-*` need to be hidden on the search page.
|
||||
|
Reference in New Issue
Block a user