Skip to content
Advertisement

Sync two product quantities at cart woocommerce

I am trying to sync two product ids at the cart in woocommerce so that change in one bring change in the other quantity, I am trying this code but it updates all the products in the cart with master product ID, which I dont want.

JavaScript

Advertisement

Answer

I took an interest in this as the problem is quite tricky to fix. In order for you to accomplish what you are asking, you need to know which quantity changed. Which is a big issue as I don’t think there is any hook on the PHP side that can tell you this.

This leaves us with one option from what I can see, and that is to inject some JavaScript into the bottom of the cart page that directly listens for the change. Here is an attempt I wrote to see if I could get it working:

JavaScript

This absolutely can be cleaned up further and made more efficient, however I’ve tried to comment as much of it as possible so you can see why I did what I did. I also tested this on a site I’m hosting and it worked, so it should work for you.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement