Automattic\WooCommerce\Blocks\BlockTypes

AddToCartForm::add_is_descendent_of_single_product_block_hidden_input_to_product_form()protectedWC 1.0

Add a hidden input to the Add to Cart form to indicate that it is a descendent of a Single Product block.

Method of the class: AddToCartForm{}

No Hooks.

Return

String. The Add to Cart Form HTML with the hidden input.

Usage

// protected - for code of main (parent) or child class
$result = $this->add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product, $is_descendent_of_single_product_block );
$product(string) (required)
The Add to Cart Form HTML.
$is_descendent_of_single_product_block(string) (required)
Indicates if block is descendent of Single Product block.

AddToCartForm::add_is_descendent_of_single_product_block_hidden_input_to_product_form() code WC 9.4.2

protected function add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product, $is_descendent_of_single_product_block ) {

	$hidden_is_descendent_of_single_product_block_input = sprintf(
		'<input type="hidden" name="is-descendent-of-single-product-block" value="%1$s">',
		$is_descendent_of_single_product_block ? 'true' : 'false'
	);
	$regex_pattern                                      = '/<button\s+type="submit"[^>]*>.*?<\/button>/i';

	preg_match( $regex_pattern, $product, $input_matches );

	if ( ! empty( $input_matches ) ) {
		$product = preg_replace( $regex_pattern, $hidden_is_descendent_of_single_product_block_input . $input_matches[0], $product );
	}

	return $product;
}