From 7f5454dcdbf00f6083a752213f569cbd971c3770 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 5 Dec 2023 17:57:07 +0000 Subject: [PATCH] Fix buttons on classic checkout and cart blocks. --- .../ppcp-button/src/Assets/SmartButton.php | 2 ++ .../ppcp-button/src/Helper/ContextTrait.php | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index f6a646388..ec2b5a395 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -272,6 +272,8 @@ public function __construct( * @return bool */ public function render_wrapper(): bool { + $this->init_context(); + if ( $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ) ) { $this->render_button_wrapper_registrar(); $this->render_message_wrapper_registrar(); diff --git a/modules/ppcp-button/src/Helper/ContextTrait.php b/modules/ppcp-button/src/Helper/ContextTrait.php index e0fcb6d73..2f71a207d 100644 --- a/modules/ppcp-button/src/Helper/ContextTrait.php +++ b/modules/ppcp-button/src/Helper/ContextTrait.php @@ -12,6 +12,36 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; trait ContextTrait { + /** + * Initializes context preconditions like is_cart() and is_checkout(). + * + * @return void + */ + protected function init_context(): void { + + /** + * Activate is_checkout() on woocommerce/classic-shortcode checkout blocks. + * + * @psalm-suppress MissingClosureParamType + */ + add_filter( + 'woocommerce_is_checkout', + function ( $is_checkout ) { + if ( $is_checkout ) { + return $is_checkout; + } + return has_block( 'woocommerce/classic-shortcode {"shortcode":"checkout"}' ); + } + ); + + // Activate is_cart() on woocommerce/classic-shortcode cart blocks. + if ( ! is_cart() && is_callable( 'wc_maybe_define_constant' ) ) { + if ( has_block( 'woocommerce/classic-shortcode' ) && ! has_block( 'woocommerce/classic-shortcode {"shortcode":"checkout"}' ) ) { + wc_maybe_define_constant( 'WOOCOMMERCE_CART', true ); + } + } + } + /** * Checks WC is_checkout() + WC checkout ajax requests. */