WooCommerce Code Reference

BlockHooksTrait

BlockHooksTrait

Shared functionality for using the Block Hooks API with WooCommerce Blocks.

Table of Contents

register_hooked_block()  : array<string|int, mixed>
Callback for `hooked_block_types` to auto-inject the mini-cart block into headers after navigation.
get_context_content()  : string
Given a provided context, returns the content of the context.
has_block_in_content()  : bool
Checks if the provided context contains a the block already.
is_target_area()  : bool
Given a provided context, returns whether the context refers to the target area and isn't marked as excluded.
is_template_part_or_pattern()  : bool
Given a provided context, returns whether the context refers to header content.
pattern_is_excluded()  : bool
Returns whether the pattern is excluded or not

Methods

register_hooked_block()

Callback for `hooked_block_types` to auto-inject the mini-cart block into headers after navigation.

public register_hooked_block(array<string|int, mixed> $hooked_blocks, string $position, string $anchor_block, array<string|int, mixed>|WP_Post|WP_Block_Template $context) : array<string|int, mixed>
Parameters
$hooked_blocks : array<string|int, mixed>

An array of block slugs hooked into a given context.

$position : string

Position of the block insertion point.

$anchor_block : string

The block acting as the anchor for the inserted block.

$context : array<string|int, mixed>|WP_Post|WP_Block_Template

Where the block is embedded.

Tags
since
8.5.0
Return values
array<string|int, mixed>An array of block slugs hooked into a given context.

get_context_content()

Given a provided context, returns the content of the context.

protected get_context_content(array<string|int, mixed>|WP_Post|WP_Block_Template $context) : string
Parameters
$context : array<string|int, mixed>|WP_Post|WP_Block_Template

Where the block is embedded.

Tags
since
8.5.0
Return values
string

has_block_in_content()

Checks if the provided context contains a the block already.

protected has_block_in_content(array<string|int, mixed>|WP_Block_Template $context) : bool
Parameters
$context : array<string|int, mixed>|WP_Block_Template

Where the block is embedded.

Return values
bool

is_target_area()

Given a provided context, returns whether the context refers to the target area and isn't marked as excluded.

protected is_target_area(array<string|int, mixed>|WP_Post|WP_Block_Template $context, string $area) : bool
Parameters
$context : array<string|int, mixed>|WP_Post|WP_Block_Template

the context to check.

$area : string

The area to check against before inserting.

Tags
since
8.5.0
Return values
bool

is_template_part_or_pattern()

Given a provided context, returns whether the context refers to header content.

protected is_template_part_or_pattern(array<string|int, mixed>|WP_Post|WP_Block_Template $context, string $area) : bool
Parameters
$context : array<string|int, mixed>|WP_Post|WP_Block_Template

Where the block is embedded.

$area : string

The area to check against before inserting.

Tags
since
8.5.0
Return values
bool

pattern_is_excluded()

Returns whether the pattern is excluded or not

protected pattern_is_excluded(array<string|int, mixed>|WP_Block_Template $context) : bool
Parameters
$context : array<string|int, mixed>|WP_Block_Template

Where the block is embedded.

Tags
since
8.5.0
Return values
bool