Shopify themes, liquid, logos, and UX
Dear community,
We've been having trouble with the add to cart functionality on Safari and Firefox.
In Chrome it works without a problem.
We have custom code on a collection grid / slider with a button to add to the cart right away. the element looks like this:
<a class="absolute-atc-enabled" href="#" type="button" name="add" data-add-to-cart onclick="addCart({{ product.variants.first.id }});" > ... </a>
and the function looks like this:
function addCart(id){ jQuery.post(window.Shopify.routes.root + 'cart/add.js', { items: [ { quantity: 1, id: id } ] }); let url = $(location).attr('href'); if (url.indexOf("?added") >= 0) { window.location.href = url; } else { window.location.href = url+"?added"; } }
Funnily enough, in my Shopify dev environment, adding to the cart works on Firefox too. It's just on our live site that we got the issue. I can click on the bag symbol, the cart opens, but the item doesn't get added. No errors in the console or warnings related, as far as I can see.
This is the link: https://eracoffee.com/
Thanks in advance for any hint!
Solved! Go to the solution
This is an accepted solution.
Hey @Christian10X ,
I double-checked on Firefox and it works. So, you need to pass a function callback that is executed if the request is successful.
function addCart(id){ jQuery.post('/cart/add.js', { items: [{ quantity: 1, id: id }] }, () => { console.log("add to cart success!") let url = $(location).attr('href'); if (url.indexOf("?added") >= 0) { window.location.href = url; } else { window.location.href = url+"?added"; } }); }
If our suggestions are useful, please let us know by giving it a like or marking it as a solution.
For B2B solutions and custom pricing, check out B2B Solution & Custom Pricing |
For adding product labels/badges, visit Product Labels by BSS
BSS Commerce - Shopify Apps & Store Development Service Provider
Hire a Shopify expert | Shopify Plus Store Development
I think you should using method ajax on jquery . I hope it will helpful for you
function addCart(id) { var product = { items: [ { quantity: 1, id: id } ] }; $.ajax({ type: 'POST', url: window.Shopify.routes.root + 'cart/add.js', data: JSON.stringify(product), contentType: 'application/json', success: function(response) { console.log('Product added to cart:', response); // Redirect let url = window.location.href; if (url.indexOf("?added") >= 0) { window.location.href = url; } else { window.location.href = url + "?added"; } }, error: function(error) { console.error('Error adding product to cart:', error); } }); }
If our suggestions are useful, please let us know by giving it a like or marking it as a solution.
For B2B solutions and custom pricing, check out B2B Solution & Custom Pricing |
For adding product labels/badges, visit Product Labels by BSS
BSS Commerce - Shopify Apps & Store Development Service Provider
Hire a Shopify expert | Shopify Plus Store Development
Thanks for you reply!
Unfortunately this didn't do the trick. Same result.
I noticed another thing tho: After a refresh, the item appears in the cart.
I've also tried JS native fetch API, which also didn't yield the needed result.
If anyone else has an idea, please let me know.
This is an accepted solution.
Hey @Christian10X ,
I double-checked on Firefox and it works. So, you need to pass a function callback that is executed if the request is successful.
function addCart(id){ jQuery.post('/cart/add.js', { items: [{ quantity: 1, id: id }] }, () => { console.log("add to cart success!") let url = $(location).attr('href'); if (url.indexOf("?added") >= 0) { window.location.href = url; } else { window.location.href = url+"?added"; } }); }
If our suggestions are useful, please let us know by giving it a like or marking it as a solution.
For B2B solutions and custom pricing, check out B2B Solution & Custom Pricing |
For adding product labels/badges, visit Product Labels by BSS
BSS Commerce - Shopify Apps & Store Development Service Provider
Hire a Shopify expert | Shopify Plus Store Development
Awesome, this one worked! Thanks so much
User | RANK |
---|---|
186 | |
170 | |
80 | |
55 | |
45 |
Transform this holiday season into a shopping spree. Plus, learn how to effortlessly open ...
By Jasonh Dec 8, 2023Make the shift from discounts to donations, and witness your business not only thrive fina...
By Holly Dec 4, 2023On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023