WordPress at Your Fingertips
function is not described

WC_Form_Handler::add_to_cart_handler_grouped() private WC 2.4.6

Handle adding grouped products to the cart.

{} It's a method of the class: WC_Form_Handler{}

Hooks from the method
Return

true|false. success or not

Usage

$result = WC_Form_Handler::add_to_cart_handler_grouped( $product_id );
$product_id(int) (required)
Product ID to add to the cart.

Changelog

Since 2.4.6 Introduced.
Since 2.4.6 Split from add_to_cart_action.

Code of WC_Form_Handler::add_to_cart_handler_grouped() WC 5.7.0

private static function add_to_cart_handler_grouped( $product_id ) {
	$was_added_to_cart = false;
	$added_to_cart     = array();
	$items             = isset( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ? wp_unslash( $_REQUEST['quantity'] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	if ( ! empty( $items ) ) {
		$quantity_set = false;

		foreach ( $items as $item => $quantity ) {
			$quantity = wc_stock_amount( $quantity );
			if ( $quantity <= 0 ) {
				continue;
			}
			$quantity_set = true;

			// Add to cart validation.
			$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );

			// Suppress total recalculation until finished.
			remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );

			if ( $passed_validation && false !== WC()->cart->add_to_cart( $item, $quantity ) ) {
				$was_added_to_cart      = true;
				$added_to_cart[ $item ] = $quantity;
			}

			add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
		}

		if ( ! $was_added_to_cart && ! $quantity_set ) {
			wc_add_notice( __( 'Please choose the quantity of items you wish to add to your cart&hellip;', 'woocommerce' ), 'error' );
		} elseif ( $was_added_to_cart ) {
			wc_add_to_cart_message( $added_to_cart );
			WC()->cart->calculate_totals();
			return true;
		}
	} elseif ( $product_id ) {
		/* Link on product archives */
		wc_add_notice( __( 'Please choose a product to add to your cart&hellip;', 'woocommerce' ), 'error' );
	}
	return false;
}