Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 8397d52

Browse files
authored
Merge branch 'trunk' into fix/synced-prepaid-sub
2 parents d9b4f21 + de943f2 commit 8397d52

8 files changed

+62
-19
lines changed

changelog.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** WooCommerce Subscriptions Core Changelog ***
22

3-
= 7.4.0 - 2024-xx-xx =
3+
= 7.4.0 - 2024-08-16 =
4+
* Dev - Introduce new parameter to WC_Subscription::get_last_order() to enable filtering out orders with specific statuses.
45
* Update - Schedule subscription-related events with a priority of 1 to allow for earlier execution within the Action Scheduler.
56
* Fix - Ensure admin notices are displayed after performing bulk actions on subscriptions when HPOS is enabled.
67
* Fix - Add a year to the next renewal date billing interval is 12 months or more for a synced subscription.
@@ -9,6 +10,7 @@
910
* Fix - Label improvements on subscription and order page templates.
1011
* Fix - Fixed an issue with subscriptions containing multiple renewal orders to mark a random item as processing, instead of the last order.
1112
* Fix - Prevent errors from invalid subscription objects during customer payment method updates.
13+
* Fix - Resolved an error when purchasing subscription products on the block checkout with a limited recurring coupon.
1214

1315
= 7.2.0 - 2024-06-13 =
1416
* Fix - label improvement on my subscription page template.

includes/class-wc-subscription.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2077,9 +2077,10 @@ protected function get_related_order_ids( $order_type = 'any' ) {
20772077
*
20782078
* @param string $return_fields The columns to return, either 'all' or 'ids'
20792079
* @param array $order_types Can include any combination of 'parent', 'renewal', 'switch' or 'any' which will return the latest renewal order of any type. Defaults to 'parent' and 'renewal'.
2080+
* @param array $exclude_statuses An array of statuses to exclude from the search. Defaults to an empty array.
20802081
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.0
20812082
*/
2082-
public function get_last_order( $return_fields = 'ids', $order_types = array( 'parent', 'renewal' ) ) {
2083+
public function get_last_order( $return_fields = 'ids', $order_types = array( 'parent', 'renewal' ), $exclude_statuses = [] ) {
20832084

20842085
$return_fields = ( 'ids' == $return_fields ) ? $return_fields : 'all';
20852086
$order_types = ( 'any' == $order_types ) ? array( 'parent', 'renewal', 'switch' ) : (array) $order_types;
@@ -2098,6 +2099,16 @@ public function get_last_order( $return_fields = 'ids', $order_types = array( 'p
20982099
}
20992100
}
21002101

2102+
if ( ! empty( $exclude_statuses ) ) {
2103+
$related_orders = array_filter(
2104+
$related_orders,
2105+
function( $order_id ) use ( $exclude_statuses ) {
2106+
$order = wc_get_order( $order_id );
2107+
return $order && ! $order->has_status( $exclude_statuses );
2108+
}
2109+
);
2110+
}
2111+
21012112
if ( empty( $related_orders ) ) {
21022113
$last_order = false;
21032114
} else {

includes/class-wc-subscriptions-core-plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WC_Subscriptions_Core_Plugin {
1616
* The version of subscriptions-core library.
1717
* @var string
1818
*/
19-
protected $library_version = '7.3.0'; // WRCS: DEFINED_VERSION.
19+
protected $library_version = '7.4.0'; // WRCS: DEFINED_VERSION.
2020

2121
/**
2222
* The subscription scheduler instance.

includes/class-wc-subscriptions-order.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -683,29 +683,54 @@ public static function order_needs_payment( $needs_payment, $order, $valid_order
683683
}
684684

685685
// Skip checks if order total is greater than zero, or
686-
// recurring total is zero, or
687686
// order status isn't valid for payment.
688-
if ( $order->get_total() > 0 || self::get_recurring_total( $order ) <= 0 || ! $order->has_status( $valid_order_statuses ) ) {
687+
if ( $order->get_total() > 0 || ! $order->has_status( $valid_order_statuses ) ) {
689688
return $needs_payment;
690689
}
691690

692-
// Check that there is at least 1 subscription with a next payment that would require a payment method.
693-
$has_next_payment = false;
691+
// Skip checks if manual renewal is required.
692+
if ( wcs_is_manual_renewal_required() ) {
693+
return $needs_payment;
694+
}
694695

696+
// Check if there's a subscription attached to this order that will require a payment method.
695697
foreach ( wcs_get_subscriptions_for_order( $order ) as $subscription ) {
698+
$has_next_payment = false;
699+
$contains_expiring_limited_coupon = false;
700+
$contains_free_trial = false;
701+
$contains_synced = false;
702+
703+
// Check if there's a subscription with a recurring total that would require a payment method.
704+
$recurring_total = (float) $subscription->get_total();
705+
706+
// Check that there is at least 1 subscription with a next payment that would require a payment method.
696707
if ( $subscription->get_time( 'next_payment' ) ) {
697708
$has_next_payment = true;
698-
break;
699709
}
700-
}
701710

702-
if ( ! $has_next_payment ) {
703-
return $needs_payment;
704-
}
711+
// Check if there's a subscription with a limited recurring coupon that is expiring that would require a payment method after the coupon expires.
712+
if ( class_exists( 'WCS_Limited_Recurring_Coupon_Manager' ) && WCS_Limited_Recurring_Coupon_Manager::order_has_limited_recurring_coupon( $subscription ) ) {
713+
$contains_expiring_limited_coupon = true;
714+
}
715+
716+
// Check if there's a subscription with a free trial that would require a payment method after the trial ends.
717+
if ( $subscription->get_time( 'trial_end' ) ) {
718+
$contains_free_trial = true;
719+
}
705720

706-
// If manual renewals are not required.
707-
if ( ! wcs_is_manual_renewal_required() ) {
708-
$needs_payment = true;
721+
// Check if there's a subscription with a synced product that would require a payment method.
722+
if ( WC_Subscriptions_Synchroniser::subscription_contains_synced_product( $subscription ) ) {
723+
$contains_synced = true;
724+
}
725+
726+
/**
727+
* We need to collect a payment method if there's a subscription with a recurring total or a limited recurring coupon that is expiring and
728+
* there's a next payment date or a free trial or a synced product.
729+
*/
730+
if ( ( $contains_expiring_limited_coupon || $recurring_total > 0 ) && ( $has_next_payment || $contains_free_trial || $contains_synced ) ) {
731+
$needs_payment = true;
732+
break; // We've found a subscription that requires a payment method.
733+
}
709734
}
710735

711736
return $needs_payment;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"title": "WooCommerce Subscriptions Core",
44
"author": "Automattic",
55
"license": "GPL-3.0-or-later",
6-
"version": "7.3.0",
6+
"version": "7.4.0",
77
"description": "",
88
"homepage": "https://github.com/Automattic/woocommerce-subscriptions-core",
99
"main": "Gruntfile.js",

tests/unit/test-class-wc-subscriptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,11 @@ public function test_get_last_order() {
20602060
$this->assertEquals( $order_id, $subscription->get_last_order() );
20612061
$this->assertEquals( wc_get_order( $order_id ), $subscription->get_last_order( 'all' ) );
20622062

2063+
// Test for the status filtering parameter
2064+
$order->update_status( 'failed' );
2065+
$order->save();
2066+
$this->assertFalse( $subscription->get_last_order( 'ids', array( 'parent', 'renewal' ), array( 'failed' ) ) );
2067+
20632068
$renewal = WCS_Helper_Subscription::create_renewal_order( $subscription );
20642069
$renewal_id = wcs_get_objects_property( $renewal, 'id' );
20652070
$this->assertEquals( $renewal_id, $subscription->get_last_order( 'ids' ) );

woocommerce-subscriptions-core.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
* Author: Automattic
77
* Author URI: https://woocommerce.com/
88
* Requires WP: 5.6
9-
* Version: 7.3.0
9+
* Version: 7.4.0
1010
*/

0 commit comments

Comments
 (0)