From 45c4d9aad7daac5a73eaa1a75d0adcfaf89f335b Mon Sep 17 00:00:00 2001 From: Andrew Lima Date: Thu, 11 Apr 2024 14:22:17 +0200 Subject: [PATCH 1/2] Fix false warnings for the is_purchase on cart page --- pmpro-woocommerce.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pmpro-woocommerce.php b/pmpro-woocommerce.php index 835da36..d1d2ef1 100644 --- a/pmpro-woocommerce.php +++ b/pmpro-woocommerce.php @@ -91,8 +91,14 @@ function pmprowoo_is_purchasable( $is_purchasable, $product ) { if ( ! function_exists( 'pmpro_get_group_id_for_level' ) ) { // If the cart already has a membership product, let's disable the purchase. if ( pmprowoo_cart_has_membership() ) { - add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); - return false; + // If we're on the cart page let's just return the $is_purchasable status and otherwise lets show a warning that there's already a membership in the cart. + if ( is_cart() || is_checkout() ) { + return $is_purchasable; + } else { + add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); + return false; + } + } return $is_purchasable; } @@ -126,8 +132,12 @@ function pmprowoo_is_purchasable( $is_purchasable, $product ) { // If the group ID in the cart matches the group ID of the product we are viewing, let's disable the purchase. if ( (int)$group_id_in_cart === (int)$group_id ) { - add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); - return false; + if ( is_cart() || is_checkout() ) { + return $is_purchasable; + } else { + add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); + return false; + } } } From 89fc9f95cf46617522e0f2728853a7353e35b089 Mon Sep 17 00:00:00 2001 From: Andrew Lima Date: Fri, 24 May 2024 11:41:38 +0200 Subject: [PATCH 2/2] Don't run is_purchasable on checkout. --- pmpro-woocommerce.php | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/pmpro-woocommerce.php b/pmpro-woocommerce.php index d1d2ef1..2aa148a 100644 --- a/pmpro-woocommerce.php +++ b/pmpro-woocommerce.php @@ -91,14 +91,8 @@ function pmprowoo_is_purchasable( $is_purchasable, $product ) { if ( ! function_exists( 'pmpro_get_group_id_for_level' ) ) { // If the cart already has a membership product, let's disable the purchase. if ( pmprowoo_cart_has_membership() ) { - // If we're on the cart page let's just return the $is_purchasable status and otherwise lets show a warning that there's already a membership in the cart. - if ( is_cart() || is_checkout() ) { - return $is_purchasable; - } else { - add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); - return false; - } - + add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); + return false; } return $is_purchasable; } @@ -132,18 +126,27 @@ function pmprowoo_is_purchasable( $is_purchasable, $product ) { // If the group ID in the cart matches the group ID of the product we are viewing, let's disable the purchase. if ( (int)$group_id_in_cart === (int)$group_id ) { - if ( is_cart() || is_checkout() ) { - return $is_purchasable; - } else { - add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); - return false; - } + add_action( 'woocommerce_single_product_summary', 'pmprowoo_purchase_disabled' ); + return false; } } return $is_purchasable; } -add_filter( 'woocommerce_is_purchasable', 'pmprowoo_is_purchasable', 10, 2 ); + +/** + * Only run the is_purchasable code once WordPress is loaded to prevent this from running multiple times as things initialize. + * This prevents checking in the cart or checkout page multiple times which can cause issues with newer WooCommerce versions. + * @since TBD + */ +function pmprowoo_run_is_purchasable() { + // Don't run this hook on the checkout or cart pages because we can assume that everything is okay once the customer reaches here. + if ( is_checkout() || is_cart() ) { + return; + } + add_filter( 'woocommerce_is_purchasable', 'pmprowoo_is_purchasable', 10, 2 ); +} +add_action( 'wp', 'pmprowoo_run_is_purchasable' ); /** * Info message when attempting to add a 2nd membership level to the cart