Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Cart item quantities not re-rendered when new values are passed down via the API #5239

Closed
manospsyx opened this issue Nov 24, 2021 · 0 comments · Fixed by #5352
Closed

Cart item quantities not re-rendered when new values are passed down via the API #5239

manospsyx opened this issue Nov 24, 2021 · 0 comments · Fixed by #5352
Assignees
Labels
block: cart Issues related to the cart block. type: bug The issue/PR concerns a confirmed bug.

Comments

@manospsyx
Copy link

manospsyx commented Nov 24, 2021

Describe the bug

One issue I attempted to tackle here, is this:

When custom code is used to keep the quantities of two (or more) items in sync, the quantities of "dependent" items fail to render correctly.

To reproduce

Here's some code you can use to play around with this state-related problem:

add_action( 'woocommerce_after_cart_item_quantity_update', function( $cart_item_key, $quantity = 0 ) {

	if ( isset( WC()->cart->is_syncing ) && WC()->cart->is_syncing ) {
		return;
	}

	if ( ! empty( WC()->cart->cart_contents[ $cart_item_key ] ) ) {

		$cart_item = WC()->cart->cart_contents[ $cart_item_key ];

		if ( ! $quantity || $quantity < 0 ) {
			$quantity = 0;
		} else {
			$quantity = $cart_item[ 'quantity' ];
		}

		if ( in_array( $cart_item[ 'data' ]->get_name(), array( 'Hoodie', 'Belt' ) ) ) {

			foreach ( WC()->cart->cart_contents as $search_item_key => $search_item ) {

				if ( 'Belt' === $search_item[ 'data' ]->get_name() ) {

					if ( 'Hoodie' !== $cart_item[ 'data' ]->get_name() ) {
						continue;
					}

				} elseif ( 'Hoodie' === $search_item[ 'data' ]->get_name() ) {

					if ( 'Belt' !== $cart_item[ 'data' ]->get_name() ) {
						continue;
					}

				} else {
					continue;
				}

				WC()->cart->is_syncing = true;
				WC()->cart->set_quantity( $search_item_key, $quantity, false );
				WC()->cart->is_syncing = false;
			}

		}
	}
}, 10, 2 );

What this code does, in a nutshell: Whenever you change the quantity of an item called "Hoodie", it searches for an item called "Belt", and tries to keep the quantity of the latter in sync with the former. Here's how this works in the legacy cart:

Screen.Recording.2021-11-23.at.05.15.18.p.m.mp4

And here's what's happening in the block cart:

Screen.Recording.2021-11-23.at.05.17.51.p.m.mp4

Note that I need to refresh to get the qty of the "synced" item to update, although it's coming back just fine through the API.
This is a roadblock that we'll need to overcome to make the new Cart/Checkout Blocks compatible with Force Sells, Product Bundles, Composite Products, Mix and Match Products, and likely other extensions that rely on the woocommerce_after_cart_item_quantity_update filter to keep cart item quantities in sync.

Expected behavior

Cart item quantities should always render the values passed down via the API.

Screenshots

See above.

Environment

WordPress (please complete the following information):

@manospsyx manospsyx added the type: bug The issue/PR concerns a confirmed bug. label Nov 24, 2021
@gigitux gigitux added the block: cart Issues related to the cart block. label Nov 25, 2021
@senadir senadir self-assigned this Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: cart Issues related to the cart block. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants