From e3801ba950fc2693e6fb0edcc62d9d4e69b01da7 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 13 Nov 2023 12:48:01 +0000 Subject: [PATCH] Fix styling of Classic Cart and Checkout Blocks on regular pages (#11694) * woocommerce_is_checkout/woocommerce_is_cart for classic shortcode block * Add return to has_block_variation --- src/BlockTypesController.php | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/BlockTypesController.php b/src/BlockTypesController.php index 2c9e175fbe5..d97c1773093 100644 --- a/src/BlockTypesController.php +++ b/src/BlockTypesController.php @@ -52,6 +52,46 @@ protected function init() { add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) ); add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_legacy_widgets_with_block_equivalent' ) ); add_action( 'woocommerce_delete_product_transients', array( $this, 'delete_product_transients' ) ); + add_filter( + 'woocommerce_is_checkout', + function( $return ) { + return $return || $this->has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', 'checkout' ); + } + ); + add_filter( + 'woocommerce_is_cart', + function( $return ) { + return $return || $this->has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', 'cart' ); + } + ); + } + + /** + * Check if the current post has a block with a specific attribute value. + * + * @param string $block_id The block ID to check for. + * @param string $attribute The attribute to check. + * @param string $value The value to check for. + * @return boolean + */ + private function has_block_variation( $block_id, $attribute, $value ) { + $post = get_post(); + + if ( ! $post ) { + return false; + } + + if ( has_block( $block_id, $post->ID ) ) { + $blocks = (array) parse_blocks( $post->post_content ); + + foreach ( $blocks as $block ) { + if ( isset( $block['attrs'][ $attribute ] ) && $value === $block['attrs'][ $attribute ] ) { + return true; + } + } + } + + return false; } /**