Should Not Add To Cart "Related Products" If "Main Products" Are Not Added First

Should Not Add To Cart "Related Products" If "Main Products" Are Not Added First

Puntriano
New Member
4 0 0

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.

Replies 2 (2)

PaulNewton
Shopify Partner
6756 610 1437

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


Puntriano
New Member
4 0 0

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:

  • Store URL: https://6a827c.myshopify.com/
  • Theme: Studio
  • The Related Products can only be accessed in Main product pages (Fucsia, Pink Roses, Red Roses and Pale Red Roses).

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,