WC_Cart::set_quantity( string $cart_item_key, int $quantity = 1, bool $refresh_totals = true )

Set the quantity for an item in the cart using it’s key.

Description Description

Parameters Parameters


(Required) contains the id of the cart item.


(Optional) contains the quantity of the item.

Default value: 1


(Optional) whether or not to calculate totals after setting the new qty. Can be used to defer calculations if setting quantities in bulk.

Default value: true

Top ↑

Return Return


Top ↑

Source Source

File: includes/class-wc-cart.php

	public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
		if ( 0 === $quantity || $quantity < 0 ) {
			wc_do_deprecated_action( 'woocommerce_before_cart_item_quantity_zero', array( $cart_item_key, $this ), '3.7.0', 'woocommerce_remove_cart_item' );
			// If we're setting qty to 0 we're removing the item from the cart.
			return $this->remove_cart_item( $cart_item_key );

		// Update qty.
		$old_quantity                                      = $this->cart_contents[ $cart_item_key ]['quantity'];
		$this->cart_contents[ $cart_item_key ]['quantity'] = $quantity;

		do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity, $this );

		if ( $refresh_totals ) {

		 * Fired after qty has been changed.
		 * @since 3.6.0
		 * @param string  $cart_item_key contains the id of the cart item. This may be empty if the cart item does not exist any more.
		 * @param int     $quantity contains the quantity of the item.
		 * @param WC_Cart $this Cart class.
		do_action( 'woocommerce_cart_item_set_quantity', $cart_item_key, $quantity, $this );

		return true;

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.