WC_Form_Handler::add_to_cart_handler_simpleprivate staticWC 2.4.6

Handle adding simple products to the cart.

Method of the class: WC_Form_Handler{}

Hooks from the method

Returns

true|false. success or not

Usage

$result = WC_Form_Handler::add_to_cart_handler_simple( $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.

WC_Form_Handler::add_to_cart_handler_simple() code WC 10.6.2

private static function add_to_cart_handler_simple( $product_id ) {
	if ( ! WC()->cart ) {
		wc_doing_it_wrong( __FUNCTION__, 'Cart is not initialized.', '10.5.0' );
		return false;
	}

	$quantity          = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_REQUEST['quantity'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

	if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
		wc_add_to_cart_message( array( $product_id => $quantity ), true );
		return true;
	}
	return false;
}