Want to create two different carts



Hi everyone, salü hardworking moltin team,

I want to generate two different carts , one for shopping basket one for exemplary.
So I am using “@moltin/sdk”: “3.11.0”, and I try to match the youtube tutorial “Build a NextJS online store” https://www.youtube.com/watch?v=rmevNGXVK0c&list=PLci1bAwknnOSbWbkIIZ5D9tbNS6Og2lug

I know the Documentation
but didn’t get more knowledge :confused:

So my moltin.js file looks like this

Every time I’m typing in my reference in my Moltin.Cart(…) I got an error

Am I right to define two different addToCart 1 / 2 so one for adding to shopping one for exemplary basket ?

Do I have to change the parameters productId and quantity or are the general?

Here is my AddToCart.js file looks same like my wanted AddToMusterkorb (german word for exemplary basket)

Many many thanks in advance for help :slight_smile:


Hi @christoph

Are you able to expand the Promise rejection error you’re receiving in the console?

You could simply the addToCart function to accept a cartId param that is then passed onto Moltin.Cart()

export const addToCart = (productId, quantity, cartId = null) => Moltin.Cart(cartId).AddProduct(productId, quantity)`

Thanks :slight_smile:


Sry but unfortunately I am a newbie in programming Javascript :confused:

More detailed explanation would be pleasure!

Is the cartId the param who defines which cart is selected ?
Why null?

Thank you :slight_smile:


small solution … I hope .

So as @notrab explained , you have to expand function addToCart with param cartId.

In your new cart file for example AddToMusterkorb.js you have to change

const reference2 = 'Musterkorb'
const cart = await addToCart(productId, quantity, reference2 )

now when I klick button AddToMusterkorb there is no adding product to Cart , but where can I define to see this added product.
Are there two different mcart or what I have to do to see / grap these products ?

Thank you :slight_smile:


Hey Chris,

Jumping in for Jamie. Just to kick off talking through this problem. It seems you have two cart ids (reference), which is not a problem. And you should be able to use the two different references (cartId’s) to see the items. (https://docs.moltin.com/carts-and-checkout/carts/cart-items/get-cart-items). The above looks like you should be fetching the items correctly. Are you not seeing data in the items. Are you able to print out items or set a breakpoint?



Hey @GeorgeF

many thanks for confirming me I’m right with my defined references! Got my solution :slight_smile:

I was confused of the

async componentDidMount() {
const cartId = await localStorage.getItem(‘mcart’)
const { data, meta } = await getCartItems(cartId)

and I thought cartId will geht the lokalStorage and will then be used for the getCartItems function.
And its like I said but you just have to grab only the Items of your Cart with reference you want.


import {reference2} from ‘…/components/AddToMusterkorb’

async componentDidMount() {
const cartId = await localStorage.getItem(‘mcart’)
const { data, meta } = await getCartItems(reference2)

Thanks again @notrab for great tutorial and @GeorgeF for helping me out of my confusion !!


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.