Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Can't add a bundle to a cart using the Storefront API

Solved

Can't add a bundle to a cart using the Storefront API

jpvalery
Tourist
6 0 2

Hi there,

I'm running into an issue where trying to add my bundle to a checkout returns a null:

```

mutation {
checkoutCreate(input: { lineItems: [{ quantity: 1, variantId: "the_base_64_encoded_string_of_the_product_variant" }] }) {
checkout {
id
webUrl
}
}
}
```

returns

```
{
"data": {
"checkoutCreate": {
"checkout": null
}
}
}
```

What am I missing to be able to add a bundle to a cart using the storefront API?

 

Accepted Solution (1)
christian_s_dev
Tourist
8 1 2

This is an accepted solution.

Interesting error, I have a feeling it's just not supported with Checkouts yet, for creating a cart with a bundle items this should work:

mutation {
  cartCreate(
    input: {
      lines: [
        {
          quantity: 1
          merchandiseId: "VARIANT_ID_FOR_BUNDLE"
        }
      ]
    }
  ) {
    cart {
      id
      lines(first: 10) {
        edges {
          node {
            id
            merchandise {
              ... on ProductVariant {
                id
                title
              }
            }
          }
        }
      }
    }
  }
}

 

Then you can get check out URL:

query checkoutURL {
  cart(
    id: "CARTR_ID_FROM_PREVIOUS_STEP"
  ) {
    checkoutUrl
  }
}

 
The Cart and Checkout API seem fairly similar, so migrating shouldn't be too painful, although it is an unexpected headache for sure. 

View solution in original post

Replies 10 (10)
jpvalery
Tourist
6 0 2

 

Yes, I've already confirmed these steps. The same request with a non-bundle product variant id does function as expected.

jpvalery_0-1698681918453.png

with the bundle:

jpvalery_2-1698682170214.png

 

with any other product:

jpvalery_1-1698682104729.png



The bundle is available on all markets, in stock, and available on the headless storefront.

christian_s_dev
Tourist
8 1 2

I'm having the same issue, normal product variants work, but not variants from Bundles, also tried using the latest storefront endpoint but no luck, did you find a solution?

jpvalery
Tourist
6 0 2

Glad to see I'm not the only one—unfortunately I haven't found a solution. Started a ticket yesterday with Shopify but I'm yet to have an answer (hence that community post).

I'm pretty sure it's something stupid that needs to be done for bundles, but the docs are extremely confusing and not very clear

christian_s_dev
Tourist
8 1 2

Some progress on my end: while it seems we can't add bundles to Checkout objects, we can add them to Cart objects, and then retrieve a Checkout URL... so it seems maybe we're supposed to migrate to using Cart objects for working with bundles?

jpvalery
Tourist
6 0 2

Could you walk me through the requests you're using?

FYI, I realized I could add checkoutUserErrors to the request to understand why it's not adding the bundle. Here's what I got:

```

"checkoutUserErrors": [
{
"code": "PRODUCT_NOT_AVAILABLE",
"field": [
"input",
"lineItems",
"0",
"variantId"
],
"message": "Variant the product is not published for this customer"
}
]

```


Update: indeed, creating a cart does work:

jpvalery_0-1698724426665.png

 

```
mutation {
cartCreate(input: {
lines: [
{
merchandiseId: "the_variant_id",
quantity: 1
}
]
}) {
cart {
id
checkoutUrl
}
userErrors {
field
message
}
}
}

```

christian_s_dev
Tourist
8 1 2

This is an accepted solution.

Interesting error, I have a feeling it's just not supported with Checkouts yet, for creating a cart with a bundle items this should work:

mutation {
  cartCreate(
    input: {
      lines: [
        {
          quantity: 1
          merchandiseId: "VARIANT_ID_FOR_BUNDLE"
        }
      ]
    }
  ) {
    cart {
      id
      lines(first: 10) {
        edges {
          node {
            id
            merchandise {
              ... on ProductVariant {
                id
                title
              }
            }
          }
        }
      }
    }
  }
}

 

Then you can get check out URL:

query checkoutURL {
  cart(
    id: "CARTR_ID_FROM_PREVIOUS_STEP"
  ) {
    checkoutUrl
  }
}

 
The Cart and Checkout API seem fairly similar, so migrating shouldn't be too painful, although it is an unexpected headache for sure. 

jpvalery
Tourist
6 0 2

Yeah it's definitely a head-scratcher.

By any chance, would you happen to know how to transcribe that graphQL query into the shopify-buy node package? That's my missing piece to get it to work on my nextjs headless storefront

christian_s_dev
Tourist
8 1 2

Unfortunately, I don't since I'm calling GraphQL directly. If you take that same route, it's also easier to get the checkout URL from what I had above. It's a field within the Cart object. So no need to call the separate `checkoutUrl` query

jpvalery
Tourist
6 0 2

Yeah sounds like that's what I do.

Shopify finally replied to my support ticket saying that they wouldn't help (it's their own app and their own library but ¯\_(ツ)_/¯)

zhangxiaolei1
Shopify Partner
5 0 0

hello,Can you send me the format of "merchandiseId"?