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

Commit

Permalink
Fix cart and checkout conditionals when using a block based theme and…
Browse files Browse the repository at this point in the history
… templates (#10098)

* Update conditionals to deal with templates instead of cart/checkout page objects

* Include notice style in main entrypoint
  • Loading branch information
mikejolley authored and opr committed Jul 5, 2023
1 parent 10a27d6 commit 603deab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '../css/editor.scss';
import '../css/style.scss';
import './filters/block-list-block';
import './filters/get-block-attributes';
import './base/components/notice-banner/style.scss';

setCategories( [
...getCategories().filter(
Expand Down
11 changes: 2 additions & 9 deletions src/Domain/Services/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks\Domain\Services;

use Automattic\WooCommerce\Blocks\Domain\Package;
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;

/**
* Service class for adding new-style Notices to WooCommerce core.
Expand Down Expand Up @@ -41,15 +42,7 @@ public function __construct( Package $package ) {
* is using the new block based cart/checkout.
*/
public function init() {
// Core page IDs.
$cart_page_id = wc_get_page_id( 'cart' );
$checkout_page_id = wc_get_page_id( 'checkout' );

// Checks a specific page (by ID) to see if it contains the named block.
$has_block_cart = $cart_page_id && has_block( 'woocommerce/cart', $cart_page_id );
$has_block_checkout = $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );

if ( $has_block_cart || $has_block_checkout ) {
if ( CartCheckoutUtils::is_cart_block_default() || CartCheckoutUtils::is_checkout_block_default() ) {
add_filter( 'woocommerce_kses_notice_allowed_tags', [ $this, 'add_kses_notice_allowed_tags' ] );
add_filter( 'wc_get_template', [ $this, 'get_notices_template' ], 10, 5 );
add_action(
Expand Down
30 changes: 30 additions & 0 deletions src/Utils/CartCheckoutUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ class CartCheckoutUtils {
* @return bool true if the WC cart page is using the Cart block.
*/
public static function is_cart_block_default() {
if ( wc_current_theme_is_fse_theme() ) {
// Ignore the pages and check the templates.
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'cart' ), 'wp_template' );

// If there is no template file, we're using default which does use the block.
if ( empty( $templates_from_db ) ) {
return true;
}

foreach ( $templates_from_db as $template ) {
if ( has_block( 'woocommerce/cart', $template->content ) ) {
return true;
}
}
}
$cart_page_id = wc_get_page_id( 'cart' );
return $cart_page_id && has_block( 'woocommerce/cart', $cart_page_id );
}
Expand All @@ -22,6 +37,21 @@ public static function is_cart_block_default() {
* @return bool true if the WC checkout page is using the Checkout block.
*/
public static function is_checkout_block_default() {
if ( wc_current_theme_is_fse_theme() ) {
// Ignore the pages and check the templates.
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'checkout' ), 'wp_template' );

// If there is no template file, we're using default which does use the block.
if ( empty( $templates_from_db ) ) {
return true;
}

foreach ( $templates_from_db as $template ) {
if ( has_block( 'woocommerce/checkout', $template->content ) ) {
return true;
}
}
}
$checkout_page_id = wc_get_page_id( 'checkout' );
return $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );
}
Expand Down

0 comments on commit 603deab

Please sign in to comment.