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.

Метод класса: AddToCartForm{}

Хуков нет.

Возвращает

Строку. The Add to Cart Form HTML with the hidden input.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product_html, $is_descendent_of_single_product_block );
$product_html(строка) (обязательный)
The Add to Cart Form HTML.
$is_descendent_of_single_product_block(строка) (обязательный)
Indicates if block is descendent of Single Product block.

Код AddToCartForm::add_is_descendent_of_single_product_block_hidden_input_to_product_form() WC 9.5.1

protected function add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product_html, $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_html, $input_matches );

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

	return $product_html;
}