How can I create a direct add to cart link without a product variant?

Topic summary

Issue: Direct add-to-cart links require a variant ID; using a product ID returns “invalid variant ID.” Shopify assigns every product at least one variant ID, even if no variants are configured.

How to find the variant ID:

  • Storefront XML method: Open the product on the storefront and append .xml to the product URL, then read . Note: Appending .xml to the admin product URL may not work; use the public product page URL.
  • Theme method: Add a Custom Liquid block to the product page with {{ product.variants.first.id }} to display the variant ID.

Usage: Build the add-to-cart (cart permalink) with https://domain.com/cart/add?id=VARIANT-ID-HERE, which can preload the cart and optionally redirect to checkout.

Key terms:

  • Variant ID: The unique ID for a purchasable variant (exists even for single-variant products).
  • Cart permalink: A URL that preloads items in the cart (can lead to checkout).
  • Liquid: Shopify’s templating language used in themes.

Outcome: Both methods were tested and confirmed to work. The thread is resolved.

Summarized with AI on December 30. AI used: gpt-5.

I know to create an add to cart link you would use https://domain.com/cart/add?id=VARIANT-ID-HERE that has been shared on this forum.

However, my products do not have variants. Is there a way to just add the product ID instead? When I use the product id it obviously tells me it’s an invalid variant ID.

Or is the only way to do this is creating a variant for your product?

1 Like

Did you get a solution to this?

Hi @xps15z and @AndrewPienaar1 ,

With Shopify every product is assigned a variant ID, even if you don’t create any variants for your product. Confusing right?

Below is the easiest way to retrieve that elusive variant ID, so you can auto-add your product to the cart and simultaneously redirect users to the checkout page (Shopify calls this a cart permalink).

  1. From the Shopify admin, find the product that you want to automatically add to cart and click into it to open the product manager.
  2. Go to the browser bar and in the URL you should see /products/ followed by the product ID number. Add .xml to the end of the URL, then refresh the browser to reveal the document tree with all the data attributes for that product.
  3. Find the tag, then beneath it you should see a single tag. Look for the tag nested beneath , and you should see a number that is different than your product ID that is in the URL at the top.
  4. Copy that number between the opening and closing tags.
  5. Use that number to build your permalink then test it out to see if it works.

I hope that helps!

1 Like

Hi @Brently , Thank you for this answer. I just tried it and I keep getting that the page cannot be found. Do you know if this is still working as of 07/13/23? I added .xml after the product number from within the Product Manager screen (in admin), but it does not load. Thanks for your help!

@Brently As it usually goes - I posted my question and then I figured it out. I decided to look at the product from the front of the site and I added .xml after the product name to reveal the details you mentioned above. I think I found my variant! Thanks again for your help. It was extremely useful.

simply add custom liquid block to the product page you wish to inquire and add the following code to show the variant id of the product shown

{{ product.variants.first.id }}

enjoy

2 Likes

I did this and it worked perfectly. I have been trying to find a solution to this all day. Thank you!