WC_Blocks_Utils::has_block_in_page()public staticWC 1.0

Check if a given page contains a particular block.

Method of the class: WC_Blocks_Utils{}

No Hooks.

Return

true|false. Boolean value if the page contains the block or not. Null in case the page does not exist.

Usage

$result = WC_Blocks_Utils::has_block_in_page( $page, $block_name );
$page(int|WP_Post) (required)
Page post ID or post object.
$block_name(string) (required)
The name (id) of a block, e.g. woocommerce/cart.

WC_Blocks_Utils::has_block_in_page() code WC 8.7.0

public static function has_block_in_page( $page, $block_name ) {
	$page_to_check = get_post( $page );
	if ( null === $page_to_check ) {
		return false;
	}

	$blocks = parse_blocks( $page_to_check->post_content );
	foreach ( $blocks as $block ) {
		if ( $block_name === $block['blockName'] ) {
			return true;
		}
	}

	return false;
}