diff --git a/src/components/cells/ProductDetailsHeader/ProductDetailsHeader.tsx b/src/components/cells/ProductDetailsHeader/ProductDetailsHeader.tsx index 5505c4d2..1d34ced1 100644 --- a/src/components/cells/ProductDetailsHeader/ProductDetailsHeader.tsx +++ b/src/components/cells/ProductDetailsHeader/ProductDetailsHeader.tsx @@ -68,6 +68,7 @@ export const ProductDetailsHeader = ({ variantId: variantId, quantity: 1, countryCode: locale, + sellerHandle: product.seller?.handle ?? '' }) setIsAdding(false) diff --git a/src/lib/data/cart.ts b/src/lib/data/cart.ts index dad61212..e0815b34 100644 --- a/src/lib/data/cart.ts +++ b/src/lib/data/cart.ts @@ -14,6 +14,7 @@ import { setCartId, } from "./cookies" import { getRegion } from "./regions" +import { getSellerByHandle } from "./seller" export async function quickOrder({ region_id, @@ -68,13 +69,19 @@ export async function retrieveCart(cartId?: string) { .catch(() => null) } -export async function getOrSetCart(countryCode: string) { +export async function getOrSetCart(countryCode: string, sellerHandle: string) { const region = await getRegion(countryCode) if (!region) { throw new Error(`Region not found for country code: ${countryCode}`) } + const seller = await getSellerByHandle(sellerHandle) + + if (!seller) { + throw new Error(`Seller not found for handle: ${sellerHandle}`) + } + let cart = await retrieveCart() const headers = { @@ -83,7 +90,9 @@ export async function getOrSetCart(countryCode: string) { if (!cart) { const cartResp = await sdk.store.cart.create( - { region_id: region.id }, + // TODO: remove expect error directive when @medusa/js-sdk types are updated + // @ts-expect-error incomplete type as endpoint accepts additional_data + { region_id: region.id, additional_data: { seller_id: seller.id } }, {}, headers ) @@ -129,16 +138,22 @@ export async function addToCart({ variantId, quantity, countryCode, + sellerHandle, }: { variantId: string quantity: number countryCode: string + sellerHandle: string }) { if (!variantId) { throw new Error("Missing variant ID when adding to cart") } - const cart = await getOrSetCart(countryCode) + if (!sellerHandle) { + throw new Error("Missing seller handle when adding to cart") + } + + const cart = await getOrSetCart(countryCode, sellerHandle) if (!cart) { throw new Error("Error retrieving or creating cart") diff --git a/src/types.d.ts b/src/types.d.ts index ed38013f..5f16f7bd 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1 +1,10 @@ +import "@medusajs/types" +import { SellerProps } from "./types/seller" + declare module '*.lodash'; + +declare module "@medusajs/types" { + interface StoreProduct { + seller?: SellerProps + } +} \ No newline at end of file diff --git a/src/types/seller.ts b/src/types/seller.ts index cce3e2e1..929ae6f9 100644 --- a/src/types/seller.ts +++ b/src/types/seller.ts @@ -5,7 +5,7 @@ type SellerAddress = { postal_code?: string } -type SellerProps = SellerAddress & { +export type SellerProps = SellerAddress & { id: string name: string handle: string