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

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

xps15z
Visitor
2 0 1

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?

Replies 6 (6)

AndrewPienaar1
Visitor
1 0 0

Did you get a solution to this?

Emily96
Excursionist
12 0 5

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
SoPrettyDesign
Shopify Partner
2 0 0

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

Brently
Shopify Partner
3 0 1

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 <variants type="array"> tag, then beneath it you should see a single <variant> tag. Look for the <id type="integer"> tag nested beneath <variant>, 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 <id> and closing </id> tags.
  5. Use that number to build your permalink then test it out to see if it works.

 

I hope that helps!

"Two of the most famous products of Berkeley are LSD and Unix. I don’t think that is a coincidence."
wcc-crystal
Shopify Partner
3 0 0

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!

wcc-crystal
Shopify Partner
3 0 0

@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.