From 603deabb84ceb37fed6dd09f053b6ad8bdf32dde Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 5 Jul 2023 12:02:27 +0100 Subject: [PATCH] Fix cart and checkout conditionals when using a block based theme and templates (#10098) * Update conditionals to deal with templates instead of cart/checkout page objects * Include notice style in main entrypoint --- assets/js/index.js | 1 + src/Domain/Services/Notices.php | 11 ++--------- src/Utils/CartCheckoutUtils.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/assets/js/index.js b/assets/js/index.js index 5fdfbdcc91c..3324889f387 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -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( diff --git a/src/Domain/Services/Notices.php b/src/Domain/Services/Notices.php index 990681489b0..12090af3cf1 100644 --- a/src/Domain/Services/Notices.php +++ b/src/Domain/Services/Notices.php @@ -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. @@ -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( diff --git a/src/Utils/CartCheckoutUtils.php b/src/Utils/CartCheckoutUtils.php index 24f59c46f2d..e1d74d2c65e 100644 --- a/src/Utils/CartCheckoutUtils.php +++ b/src/Utils/CartCheckoutUtils.php @@ -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 ); } @@ -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 ); }