A space to discuss online store customization, theme development, and Liquid templating.
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
Hi, we're trying to sell an engraving service as an addon to the keychains we sell. We charge an extra amount for this so we want to we're trying to hook into the add to cart function to add it as a `Product` but so far it's not working. We've tried the following
1) Adding functionality to the Add To Cart button
document.getElementById("AddToCart").onclick = function(event) { fetch(window.Shopify.routes.root + 'cart/add.js', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ 'id': {{ variant_id }}, 'quantity': 1, 'properties': { 'engraving': {{ engraving }} } }) }).then(response => { return response.json(); }).catch((error) => { console.error('Error', error); }) }
2) Hooking into the theme's Add To Cart function in theme.js.liquid
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback) { var formData = new FormData(form); // Add to Cart call for the engraving addEngravingToCart(); var params = { type: 'POST', url: '/cart/add.js', data: formData, processData: false, contentType: false, dataType: 'json', success: function(line_item) { if (typeof callback === 'function') { callback(line_item, form); } else { ShopifyAPI.onItemAdded(line_item, form); } }, error: function(XMLHttpRequest, textStatus) { if (typeof errorCallback === 'function') { errorCallback(XMLHttpRequest, textStatus); } else { ShopifyAPI.onError(XMLHttpRequest, textStatus); } } }; jQuery.ajax(params); };
In both cases, we get inconsistent results and only 1 of the 3 following scenarios happen everytime we click the Add To Cart button
A) Only the Engraving Product gets added to cart
B) Only the Keychain Product get added to the cart
C) Both the Engraving Product and the Keychain Product gets added to the cart
Is there a way that we can get C to work consistently? If it helps, our theme's Add to Cart function is below
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback) { var formData = new FormData(form); var params = { type: 'POST', url: '/cart/add.js', data: formData, processData: false, contentType: false, dataType: 'json', success: function(line_item) { if (typeof callback === 'function') { callback(line_item, form); } else { ShopifyAPI.onItemAdded(line_item, form); } }, error: function(XMLHttpRequest, textStatus) { if (typeof errorCallback === 'function') { errorCallback(XMLHttpRequest, textStatus); } else { ShopifyAPI.onError(XMLHttpRequest, textStatus); } } }; jQuery.ajax(params); };
Hi There,
I think the issue is that the standard code is generally only designed to add a single item to the cart at a time.
What you could do is instead of adding your call to the themes add to cart function and hitting the cart api twice is modify the function.
They are passing the product form, and generally they pass the variant id in a hidden "id" field. You will need to create an array of items from the form and when the customer has selected the engraving option for the particular product, you will add that product variant to the items array, and then submit the array of products.
items: [
{
id: 36110175633573,
quantity: 1
},
{
id: 36110198766789, //engraving product
quantity: 1
}
]
Cheers,
Gary
Hi @LorenzoA,
I understand that you're seeking a hassle-free solution to implement an engraving service with an additional fee for customers purchasing your Keychain product. I'm here to provide a straightforward approach using the Easify Product Options app, avoiding the need for coding and potential complications. Here's a step-by-step guide to help you set up engraving options with extra fees using the app 🤗:
1. Create a new option set and add Engraving Option:
2. Add Engraving Text Entry:
3. Integrate Font Selection:
4. Associate Options with Products:
By following these steps, you can effortlessly provide a tailored engraving service with associated fees, enhancing customer satisfaction and boosting your product offerings 🙌.