Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Block Hooks: Auto insert the Mini Cart block into the themes header template part #11720

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected function initialize() {
parent::initialize();
add_action( 'wp_loaded', array( $this, 'register_empty_cart_message_block_pattern' ) );
add_action( 'wp_print_footer_scripts', array( $this, 'print_lazy_load_scripts' ), 2 );
add_filter( 'hooked_block_types', array( $this, 'auto_insert_into_header_template_part' ), 10, 4 );
}

/**
Expand Down Expand Up @@ -587,4 +588,28 @@ public function register_empty_cart_message_block_pattern() {
public function should_not_render_mini_cart( array $attributes ) {
return isset( $attributes['cartAndCheckoutRenderStyle'] ) && 'hidden' !== $attributes['cartAndCheckoutRenderStyle'];
}

/**
* Uses Block Hooks to automatically insert the Mini Cart block after the Navigation block in the header template part.
*
* @param array $hooked_blocks Blocks to be hooked.
* @param string $position Where to hook the block in relation to its anchor.
* @param string $anchor_block The anchor block where we are hooking the Mini Cart block.
* @param \WP_Block_Template $context The context of the block.
*
* @return array
*/
public function auto_insert_into_header_template_part( $hooked_blocks, $position, $anchor_block, $context ) {
if ( $context instanceof \WP_Block_Template ) {
if (
'core/navigation' === $anchor_block &&
'after' === $position &&
'header' === $context->area
) {
$hooked_blocks[] = $this->namespace . '/' . $this->block_name;
}
}

return $hooked_blocks;
}
}
Loading