#How to add multiple line items to cart

11 messages · Page 1 of 1 (latest)

flint geyser
#

How to add multiple line items to cart as an array or other method

heady atlas
#

You can attach some unique property to the lineItem metadata

#

medusa will then treat it as unique

flint geyser
#

I don't understand what you mean

flint geyser
#

Can anyone help me with this issue?

dull tundra
#

Why do you need it? What is your use case?

flint geyser
#

While adding a product A in cart, I need to add two more products B and C as it's add-on.

dull tundra
#

There's nothing in medusa built-in for it. You would need to loop through your variants and add them.

const variantsToAdd = [{variant_id: 'variantA', quantity: 1},{variant_id: 'variantB', quantity: 1},{variant_id: 'variantC', quantity: 1}];

Sequentially:

  for (const variant of variantsToAdd) {
    await medusaClient.lineItems.create(cartId, { variant_id: variant.variant_id, quantity: variant.quantity});
   }

In parallel

  await Promise.all(variantsToAdd.map((variant) => 
     medusaClient.lineItems.create(cartId, { variant_id: variant.variant_id, quantity: variant.quantity});
  ));
#

Not sure about 409 conflicts when doing parallel, try.

flint geyser
#

ok let me try

eager temple
#

We created a new endpoint for this -> Much more efficient