Automatically adding product to cart

migies12
Visitor
1 0 0

Hello, I have been working on automatically adding a product to my cart based on what is currently in it. My store focuses on villa renals and needs a cost per night, as well as a ficed base cost. As the cost per night is functioning as necessary, I was thinking of adding a second product to the cart automatically for the fixed cost, this way the product is added automatically when the other one is in the cart. I followed a couple tutorials, such as this.

 

For some reason even when following these tutorials I am unable to have these products add to the cart as they are supposed to.

 

If anyone has any way of helping either make this work or another alternative solution please let me know.

 

-Miguel

Reply 1 (1)

PaulNewton
Shopify Partner
6339 580 1350

The implementation details for this will vary wildly among themes so just following some tut will not always apply.

Undocumented - In some few themes with simple javascript the variant id input field in a product form can just be made to submit the same quantity of multiple items by appending brackets, [] , to the name attribute value,  name="id",  to indicate multiple ids. So then the addon products id can be added with an additional input. However most themes use javascript functions to submit the cart in ways that ultimately ignore the actual underlying html&form behavior.

Ie. 

<input type="number" name="quantity" value="1">

<input type="hidden" name="id" value="{{variant.id}}">

becomes 

<input type="number" name="quantity" value="1">

<input type="hidden" name="id[]" value="1234567">

<input type="hidden" name="id[]" value="765432">

Note with this workaround all quantities will be the same.

 

Otherwise for javascript ajax methods start with the fundamentals:

https://shopify.dev/api/ajax 

https://shopify.dev/api/ajax/reference/cart 

 

For cart based product adding also reference the gift-wrap customization:

https://help.shopify.com/en/manual/online-store/themes/themes-by-shopify/vintage-themes/customizing-...   With a little javascript know how that can be automatically added instead of using the checkbox display.

And this old github repo for vintage themes https://github.com/carolineschnapp/add-product-to-cart-automatically 

 

Good hunting.

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org