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

Commit

Permalink
Create Mini-Cart template part area (#10203)
Browse files Browse the repository at this point in the history
* Create Mini-Cart template part area

* Update Mini-Cart template part name to exclude it

* Update src/BlockTemplatesController.php

Co-authored-by: Daniel Dudzic <[email protected]>

* Update src/BlockTemplatesController.php

Co-authored-by: Daniel Dudzic <[email protected]>

* Keep both variations of the template part name when excluding them

* Revert "Keep both variations of the template part name when excluding them"

This reverts commit c3f2935.

---------

Co-authored-by: Daniel Dudzic <[email protected]>
  • Loading branch information
Aljullu and danieldudzic authored Jul 18, 2023
1 parent 32b75c8 commit c4f796e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion assets/js/blocks/mini-cart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ addFilter(
...blockSettings,
variations: blockSettings.variations.map(
( variation: { name: string } ) => {
if ( variation.name === 'instance_mini-cart' ) {
if ( variation.name === 'mini-cart' ) {
return {
...variation,
scope: [],
Expand Down
18 changes: 18 additions & 0 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __construct( Package $package ) {
* Initialization method.
*/
protected function init() {
add_filter( 'default_wp_template_part_areas', array( $this, 'register_mini_cart_template_part_area' ), 10, 1 );
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
add_filter( 'pre_get_block_template', array( $this, 'get_block_template_fallback' ), 10, 3 );
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
Expand Down Expand Up @@ -103,6 +104,23 @@ function( $settings, $metadata ) {
}
}

/**
* Add Mini-Cart to the default template part areas.
*
* @param array $default_area_definitions An array of supported area objects.
* @return array The supported template part areas including the Mini-Cart one.
*/
public function register_mini_cart_template_part_area( $default_area_definitions ) {
$mini_cart_template_part_area = [
'area' => 'mini-cart',
'label' => __( 'Mini-Cart', 'woo-gutenberg-products-block' ),
'description' => __( 'The Mini-Cart template allows shoppers to see their cart items and provides access to the Cart and Checkout pages.', 'woo-gutenberg-products-block' ),
'icon' => 'mini-cart',
'area_tag' => 'mini-cart',
];
return array_merge( $default_area_definitions, [ $mini_cart_template_part_area ] );
}

/**
* Renders the `core/template-part` block on the server.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public static function build_template_result_from_file( $template_file, $templat
$template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
$template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
$template->area = 'uncategorized';

// Force the Mini-Cart template part to be in the Mini-Cart template part area.
if ( 'wp_template_part' === $template_type && 'mini-cart' === $template_file->slug ) {
$template->area = 'mini-cart';
}
return $template;
}

Expand Down

0 comments on commit c4f796e

Please sign in to comment.