WooCommerce: Sync Product Quantities @ Cart

This snippet will help you synchronize all your cart items’ quantities with a given product ID quantity. When you add a second product to cart, therefore, it will get the same quantity of your product ID. Also, if you update the quantity of product ID, the other cart item quantities will automatically update accordingly.

Applications are quite niche, but it’s great to learn how to programmatically set the quantity of a cart item. As usual, each snippet of this website has got something that sooner or later you may need to use. Enjoy!

Based on the snippet example below, whenever I change the quantity of product ID = 20 (called 2 Simple), all the other products’ quantity will be synced.

PHP Snippet: Synchronize Product ID Quantity And Other Item Quantities @ WooCommerce Cart

Note: you have to specify your “master_product_id” inside the snippet. This is the reference product. All the other products in the cart will sync with its quantity.

/**
 * @snippet       Sync Cart Item Quantities
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 4.0
 * @community     https://businessbloomer.com/club/
 */
   
add_action( 'template_redirect', 'bbloomer_sync_cart_quantities' );
   
function bbloomer_sync_cart_quantities() {
    if ( WC()->cart->is_empty() ) return;
	$master_product_id = 20;
	$in_cart = false;
	foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		if ( $master_product_id === $cart_item['product_id'] ) {
			$qty = $cart_item['quantity'];
			$in_cart = true;
			break;
		}
	}
	if ( ! $in_cart ) return;
	foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		if ( $master_product_id !== $cart_item['product_id'] ) {
			WC()->cart->set_quantity( $cart_item_key, $qty );
		}
	}     
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

6 thoughts on “WooCommerce: Sync Product Quantities @ Cart

  1. Hi Rodolfo,
    Will this help with my problem which is:
    Let’s say the customer puts 2 blankets in the cart. Then wonders through the website and later adds another blanket. When I extract the order, it is written down as 2+1 instead of 3. So it doesn’t register that there are 3 same items in the cart.
    Best Regards, Luca

    1. Hey Luca, no, that seems like a bug. Sorry

  2. How can I make this work reverse?
    I have a “package” product and I want it quantity to be the same as total quiantities of all other products in cart.
    Thanks.

    1. Igor, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Very nice, Rodolfo! I cant’ see any usage for me right now, but I loved it and will test just to see it working. Thanks!

    1. Cool!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *