WC_Cart::set_quantity()publicWC 1.0

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

Method of the class: WC_Cart{}

Return

true|false.

Usage

$WC_Cart = new WC_Cart();
$WC_Cart->set_quantity( $cart_item_key, $quantity, $refresh_totals );
$cart_item_key(string) (required)
contains the id of the cart item.
$quantity(int)
contains the quantity of the item.
Default: 1
$refresh_totals(true|false)
whether or not to calculate totals after setting the new qty. Can be used to defer calculations if setting quantities in bulk.
Default: true

WC_Cart::set_quantity() code WC 8.7.0

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 ) {
		$this->calculate_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;
}