Focusing on managing products, variants, and collections through the API.
Hi guys!
I hope you're all doing well. I have a customization query and would greatly appreciate your insights.
Here's the scenario I'm dealing with:
Main products: Roses (Red, Fucsia, Pink, and Pale red)
Related products: Chocolates and drinks
I want to achieve the following:
If a Main product (e.g., Fucsia) has not been added to the cart, attempting to add a Related product (e.g., Chocolates) to cart should display a message box saying, "Cannot add this to the cart because a Main product is not in the cart."
I've tried implementing a solution with the following JavaScript code:
<script>
document.addEventListener('DOMContentLoaded', function() {
var AddBtn = document.getElementById('ProductSubmitButton-template--19975790657872__main');
var rosesInCart = JSON.parse(localStorage.getItem('cart'))?.includes('Fucsia');
agregarBtn.addEventListener('click', function(event) {
var cartItems = JSON.parse(localStorage.getItem('cart')) || [];
if (!rosesInCart) {
event.preventDefault();
alert("Primero agrega Rosas al carrito antes de agregar otros productos.");
} else {
cartItems.push(productHandle);
localStorage.setItem('cart', JSON.stringify(cartItems));
alert("Producto agregado al carrito.");
}
});
});
</script>
I'd love to hear your thoughts on this approach and if there's a more efficient way to handle this scenario. Your help is much appreciated!
Thank you in advance.
Show your work not just stand alone code.
https://community.shopify.com/c/blog/how-to-get-support-from-the-community/ba-p/2254458
Getting this behavior will vary wildly among themes and that code reads like psuedo code such as presumes the theme puts cart information into local storage which most themes don't bother to do , and there's no creation for agregarBtn variable that correlates to all other buttons.
But if it's working it's fine move on.
If it's not working you need this customization built then contact me by my email for services.
Contact info in forum signature.
ALWAYS please provide context, examples: store url, theme name, post url(s) , or any further detail in ALL correspondence.
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
Hi Paul,
Thank you for your prompt response. I appreciate your feedback on the code provided. The example code is not functional, and I understand your point about variations among themes.
To provide more context, here are the details:
I would appreciate any further guidance or customization you can offer to make the code work seamlessly with my specific theme. If you are available for such services, I'd like to discuss the details further. I'll reach you at the email provided in your signature.
Thank you for your assistance, and I look forward to resolving this issue.
Best regards,