From 13df5635e858a8a91c6e3522b7c067f0c036c88e Mon Sep 17 00:00:00 2001 From: Nick Gavrilov Date: Wed, 1 Sep 2021 16:29:13 +0300 Subject: [PATCH] Fix fatal error when no product id passed to wc_get_product --- includes/class-wc-gateway-ppec-cart-handler.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-gateway-ppec-cart-handler.php b/includes/class-wc-gateway-ppec-cart-handler.php index 66d9d44f..7d487f67 100644 --- a/includes/class-wc-gateway-ppec-cart-handler.php +++ b/includes/class-wc-gateway-ppec-cart-handler.php @@ -485,6 +485,8 @@ protected function get_button_settings( $settings, $context = '' ) { * @return array */ public function inject_credit_messaging_configuration( $data, $page = '' ) { + global $post; + $context = ( 'product' === $page ) ? 'single_product_' : ( 'checkout' === $page ? 'mark_' : '' ); $context = ( $context && 'yes' === wc_gateway_ppec()->settings->{ $context . 'settings_toggle' } ) ? $context : ''; @@ -515,6 +517,18 @@ public function inject_credit_messaging_configuration( $data, $page = '' ) { ) ); + $product_id = null; + + if ( get_post_type( $post->ID ) !== 'product' && wc_post_content_has_shortcode( 'product_page' ) ) { + preg_match_all( '/' . get_shortcode_regex() . '/', $post->post_content, $matches, PREG_SET_ORDER ); + + foreach ( $matches as $shortcode ) { + if ( $shortcode[2] === 'product_page' ) { + $product_id = preg_replace( '/.*\bid="(\d+)".*/', '$1', $shortcode[3] ); + } + } + } + $data['credit_messaging'] = array( 'style' => array( 'layout' => $style['layout'], @@ -530,7 +544,7 @@ public function inject_credit_messaging_configuration( $data, $page = '' ) { ), 'placement' => ( 'checkout' === $page ) ? 'payment' : $page, // If Subscriptions is installed, we should not pass the 'amount' value. - 'amount' => class_exists( 'WC_Subscriptions' ) ? '' : ( ( 'product' === $page ) ? wc_get_price_including_tax( wc_get_product() ) : WC()->cart->get_total( 'raw' ) ), + 'amount' => class_exists( 'WC_Subscriptions' ) ? '' : ( ( 'product' === $page ) ? wc_get_price_including_tax( wc_get_product( $product_id ) ) : WC()->cart->get_total( 'raw' ) ), ); }