Automattic\WooCommerce\Blocks
DependencyDetection::page_has_tracked_blocks
Check if the current page contains any of the tracked blocks. Checks post content, widget areas, and template parts (header) for blocks.
Method of the class: DependencyDetection{}
No Hooks.
Returns
true|false. True if page has tracked blocks.
Usage
// private - for code of main (parent) class only $result = $this->page_has_tracked_blocks(): bool;
DependencyDetection::page_has_tracked_blocks() DependencyDetection::page has tracked blocks code WC 10.8.1
private function page_has_tracked_blocks(): bool {
// Check post content for blocks.
foreach ( self::TRACKED_BLOCKS as $block_name ) {
if ( \has_block( $block_name ) ) {
return true;
}
}
// Check widget areas for mini-cart (classic themes).
$mini_cart_in_widgets = BlocksUtil::get_blocks_from_widget_area( 'woocommerce/mini-cart' );
if ( ! empty( $mini_cart_in_widgets ) ) {
return true;
}
// Check header template part for mini-cart (block themes).
try {
$mini_cart_in_header = BlocksUtil::get_block_from_template_part( 'woocommerce/mini-cart', 'header' );
if ( ! empty( $mini_cart_in_header ) ) {
return true;
}
} catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// Template part may not exist in all themes, silently continue.
}
return false;
}