Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const ProductDetailsHeader = ({
variantId: variantId,
quantity: 1,
countryCode: locale,
sellerHandle: product.seller?.handle ?? ''
})

setIsAdding(false)
Expand Down
21 changes: 18 additions & 3 deletions src/lib/data/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
setCartId,
} from "./cookies"
import { getRegion } from "./regions"
import { getSellerByHandle } from "./seller"

export async function quickOrder({
region_id,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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
)
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
import "@medusajs/types"
import { SellerProps } from "./types/seller"

declare module '*.lodash';

declare module "@medusajs/types" {
interface StoreProduct {
seller?: SellerProps
}
}
2 changes: 1 addition & 1 deletion src/types/seller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type SellerAddress = {
postal_code?: string
}

type SellerProps = SellerAddress & {
export type SellerProps = SellerAddress & {
id: string
name: string
handle: string
Expand Down