Automattic\WooCommerce\Blocks\BlockTypes

ProductButton::get_cart_item_quantities_by_product_id()privateWC 1.0

Get the number of items in the cart for a given product id.

Method of the class: ProductButton{}

No Hooks.

Return

number. The number of items in the cart.

Usage

// private - for code of main (parent) class only
$result = $this->get_cart_item_quantities_by_product_id( $product_id );
$product_id(number) (required)
The product id.

ProductButton::get_cart_item_quantities_by_product_id() code WC 9.6.0

private function get_cart_item_quantities_by_product_id( $product_id ) {
	if ( ! isset( WC()->cart ) ) {
		return 0;
	}

	$cart = WC()->cart->get_cart_item_quantities();
	return isset( $cart[ $product_id ] ) ? $cart[ $product_id ] : 0;
}