Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Replace mini-cart widget with mini-cart block and add support for template parts. #2100

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">

<?php
// Need to call do_blocks() here to ensure any script/styles are enqueued in wp_head.
global $storefront_block_mini_cart;
// todo: could potentially implement better default attributes for the block.
$storefront_block_mini_cart = function_exists( 'do_blocks' ) ? do_blocks( '<!-- wp:woocommerce/mini-cart /-->' ) : null;
?>

<?php wp_head(); ?>
</head>

Expand Down
7 changes: 7 additions & 0 deletions inc/class-storefront.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ public function setup() {
),
)
);

/**
* Add support for block emplate parts.
*
* @return void
*/
add_theme_support( 'block-template-parts' );
}

/**
Expand Down
34 changes: 26 additions & 8 deletions inc/woocommerce/storefront-woocommerce-template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ function storefront_product_search() {
}
}

/**
* Returns the output for the mini-cart block.
*
* @return string
*/
function storefront_get_block_mini_cart() {
global $storefront_block_mini_cart;
return $storefront_block_mini_cart ?? '';
}

if ( ! function_exists( 'storefront_header_cart' ) ) {
/**
* Display Header Cart
Expand All @@ -124,21 +134,29 @@ function storefront_product_search() {
* @return void
*/
function storefront_header_cart() {
$block_mini_cart = storefront_get_block_mini_cart();
if ( storefront_is_woocommerce_activated() ) {
if ( is_cart() ) {
$class = 'current-menu-item';
} else {
$class = '';
}
?>
<ul id="site-header-cart" class="site-header-cart menu">
<li class="<?php echo esc_attr( $class ); ?>">
<?php storefront_cart_link(); ?>
</li>
<li>
<?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
</li>
</ul>
<ul id="site-header-cart" class="site-header-cart menu">
<!-- <li class="<?php echo esc_attr( $class ); ?>">
<?php storefront_cart_link(); ?>
</li> -->
<li>
<?php
// todo - consider adding a mechansim for either opt-in/opt-out of using the mini-cart block.
// old cart
// the_widget( 'WC_Widget_Cart', 'title=' );
// new cart
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped by via do_blocks() in header.php.
echo $block_mini_cart;
?>
</li>
</ul>
<?php
}
}
Expand Down