Skip to content

Commit 9a98889

Browse files
authored
feat(payment-notice): detect equivalent subscription (#4333)
1 parent c9c816f commit 9a98889

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

includes/plugins/woocommerce/class-woocommerce-update-payment-notice.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static function maybe_add_newspack_notices() {
110110
* @return string[] The notices.
111111
*/
112112
private static function get_notices() {
113-
if ( ! function_exists( 'wcs_get_subscriptions' ) ) {
113+
if ( ! function_exists( 'wcs_get_subscriptions' ) || ! function_exists( 'wc_get_product' ) ) {
114114
return [];
115115
}
116116

@@ -134,7 +134,25 @@ private static function get_notices() {
134134
continue;
135135
}
136136

137-
$product = array_values( $subscription->get_items() )[0]->get_product();
137+
$line_item = reset( $subscription->get_items() );
138+
$product = wc_get_product( $line_item->get_product_id() );
139+
// If the product has a parent, use the parent product.
140+
if ( $product->get_parent_id() ) {
141+
$product = wc_get_product( $product->get_parent_id() );
142+
}
143+
// If the product belongs to a grouped product, use the grouped product.
144+
if ( class_exists( 'WC_Subscriptions_Product' ) && method_exists( 'WC_Subscriptions_Product', 'get_visible_grouped_parent_product_ids' ) ) {
145+
$parent = \WC_Subscriptions_Product::get_visible_grouped_parent_product_ids( $product );
146+
if ( ! empty( $parent ) ) {
147+
$product = wc_get_product( reset( $parent ) );
148+
}
149+
}
150+
// Check if there's another active subscription of the same grouped or variable product.
151+
$active_subscriptions = WooCommerce_Subscriptions::get_user_subscription( $product );
152+
if ( $active_subscriptions ) {
153+
continue;
154+
}
155+
138156
$is_donation = Donations::is_donation_product( $product->get_id() );
139157

140158
$link_attrs = [];

0 commit comments

Comments
 (0)