mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 05:56:59 +00:00
feat: cart handling
This commit is contained in:
parent
3c797b2d4f
commit
184f008f6f
@ -1,20 +1,14 @@
|
|||||||
import { createCart, getCart } from 'lib/shopify';
|
import { getCart } from 'lib/shopware';
|
||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
import CartModal from './modal';
|
import CartModal from './modal';
|
||||||
|
|
||||||
export default async function Cart() {
|
export default async function Cart() {
|
||||||
// const cartId = cookies().get('cartId')?.value;
|
const cartId = cookies().get('sw-context-token')?.value;
|
||||||
// let cartIdUpdated = false;
|
let cartIdUpdated = true;
|
||||||
// let cart;
|
const cart = await getCart();
|
||||||
// if (cartId) {
|
|
||||||
// cart = await getCart(cartId);
|
if (cartId !== cart.id) {
|
||||||
// }
|
cartIdUpdated = true;
|
||||||
// // If the `cartId` from the cookie is not set or the cart is empty
|
}
|
||||||
// // (old carts becomes `null` when you checkout), then get a new `cartId`
|
return <CartModal cart={cart} cartIdUpdated={cartIdUpdated} />;
|
||||||
// // and re-fetch the cart.
|
}
|
||||||
// if (!cartId || !cart) {
|
|
||||||
// cart = await createCart();
|
|
||||||
// cartIdUpdated = true;
|
|
||||||
// }
|
|
||||||
// return <CartModal cart={cart} cartIdUpdated={cartIdUpdated} />;
|
|
||||||
}
|
|
@ -4,9 +4,9 @@ import {
|
|||||||
ExtendedCategory,
|
ExtendedCategory,
|
||||||
ExtendedCriteria,
|
ExtendedCriteria,
|
||||||
ExtendedCrossSellingElementCollection,
|
ExtendedCrossSellingElementCollection,
|
||||||
ExtendedProductListingResult,
|
|
||||||
extendedOperations,
|
extendedOperations,
|
||||||
extendedPaths
|
extendedPaths,
|
||||||
|
ExtendedProductListingResult
|
||||||
} from './api-extended';
|
} from './api-extended';
|
||||||
import {
|
import {
|
||||||
CategoryListingResultSW,
|
CategoryListingResultSW,
|
||||||
@ -142,3 +142,7 @@ export async function requestCrossSell(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function requestCart() {
|
||||||
|
return apiInstance.invoke('readCart get /checkout/cart?name', {});
|
||||||
|
}
|
@ -1,11 +1,17 @@
|
|||||||
|
import { Cart } from 'lib/shopify/types';
|
||||||
import {
|
import {
|
||||||
ApiSchemas,
|
requestCart,
|
||||||
Menu,
|
requestCategory,
|
||||||
Page,
|
requestCategoryList,
|
||||||
Product,
|
requestCategoryProductsCollection,
|
||||||
ProductListingCriteria,
|
requestCrossSell,
|
||||||
StoreNavigationTypeSW
|
requestNavigation,
|
||||||
} from './types';
|
requestProductsCollection,
|
||||||
|
requestSearchCollectionProducts,
|
||||||
|
requestSeoUrl,
|
||||||
|
requestSeoUrls
|
||||||
|
} from './api';
|
||||||
|
import { ExtendedCategory, ExtendedProduct, ExtendedProductListingResult } from './api-extended';
|
||||||
import {
|
import {
|
||||||
getDefaultCategoryCriteria,
|
getDefaultCategoryCriteria,
|
||||||
getDefaultCategoryWithCmsCriteria,
|
getDefaultCategoryWithCmsCriteria,
|
||||||
@ -16,17 +22,6 @@ import {
|
|||||||
getSortingCriteria,
|
getSortingCriteria,
|
||||||
getStaticCollectionCriteria
|
getStaticCollectionCriteria
|
||||||
} from './criteria';
|
} from './criteria';
|
||||||
import {
|
|
||||||
requestCategory,
|
|
||||||
requestCategoryList,
|
|
||||||
requestCategoryProductsCollection,
|
|
||||||
requestCrossSell,
|
|
||||||
requestNavigation,
|
|
||||||
requestProductsCollection,
|
|
||||||
requestSearchCollectionProducts,
|
|
||||||
requestSeoUrl,
|
|
||||||
requestSeoUrls
|
|
||||||
} from './api';
|
|
||||||
import {
|
import {
|
||||||
transformCollection,
|
transformCollection,
|
||||||
transformHandle,
|
transformHandle,
|
||||||
@ -36,7 +31,14 @@ import {
|
|||||||
transformProducts,
|
transformProducts,
|
||||||
transformStaticCollection
|
transformStaticCollection
|
||||||
} from './transform';
|
} from './transform';
|
||||||
import { ExtendedCategory, ExtendedProduct, ExtendedProductListingResult } from './api-extended';
|
import {
|
||||||
|
ApiSchemas,
|
||||||
|
Menu,
|
||||||
|
Page,
|
||||||
|
Product,
|
||||||
|
ProductListingCriteria,
|
||||||
|
StoreNavigationTypeSW
|
||||||
|
} from './types';
|
||||||
|
|
||||||
export async function getMenu(params?: {
|
export async function getMenu(params?: {
|
||||||
type?: StoreNavigationTypeSW;
|
type?: StoreNavigationTypeSW;
|
||||||
@ -214,3 +216,63 @@ export async function getProductRecommendations(productId: string): Promise<Prod
|
|||||||
|
|
||||||
return products ? transformProducts(products) : [];
|
return products ? transformProducts(products) : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getCart(): Promise<Cart> {
|
||||||
|
const cartData = await requestCart();
|
||||||
|
|
||||||
|
let cart: Cart = {
|
||||||
|
checkoutUrl: 'https://frontends-demo.vercel.app',
|
||||||
|
cost: {
|
||||||
|
subtotalAmount: {
|
||||||
|
amount: cartData.price?.positionPrice?.toString() || '0',
|
||||||
|
currencyCode: 'EUR'
|
||||||
|
},
|
||||||
|
totalAmount: {
|
||||||
|
amount: cartData.price?.totalPrice?.toString() || '0',
|
||||||
|
currencyCode: 'EUR'
|
||||||
|
},
|
||||||
|
totalTaxAmount: {
|
||||||
|
amount: '0',
|
||||||
|
currencyCode: 'EUR'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
id: cartData.token || '',
|
||||||
|
lines:
|
||||||
|
cartData.lineItems?.map((lineItem) => ({
|
||||||
|
id: lineItem.id || '',
|
||||||
|
quantity: lineItem.quantity,
|
||||||
|
cost: {
|
||||||
|
totalAmount: {
|
||||||
|
amount: (lineItem as any)?.price?.totalPrice || ''
|
||||||
|
}
|
||||||
|
},
|
||||||
|
merchandise: {
|
||||||
|
id: lineItem.referencedId,
|
||||||
|
title: lineItem.label,
|
||||||
|
selectedOptions: [],
|
||||||
|
product: {
|
||||||
|
description: lineItem.description,
|
||||||
|
descriptionHtml: lineItem.description,
|
||||||
|
id: lineItem.referencedId,
|
||||||
|
images: [],
|
||||||
|
seo: {
|
||||||
|
description: lineItem.description,
|
||||||
|
title: lineItem.label
|
||||||
|
},
|
||||||
|
availableForSale: true,
|
||||||
|
featuredImage: (lineItem as any).cover?.url,
|
||||||
|
handle: '',
|
||||||
|
options: [],
|
||||||
|
variants: [],
|
||||||
|
priceRange: {},
|
||||||
|
tags: [],
|
||||||
|
title: lineItem.label,
|
||||||
|
updatedAt: (lineItem as any)?.payload?.updatedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})) || [],
|
||||||
|
totalQuantity: cartData.lineItems?.length || 0
|
||||||
|
};
|
||||||
|
|
||||||
|
return cart;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user