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

Commit 687d500

Browse files
wjrosajames-allan
andauthored
Removing wc-checkout-draft status from the get related order IDs method (#626)
* Removing wc-checkout-draft status from the get related order IDs method * Including changelog entry * Removing deprecated function * Debug info for unit tests * Debug info for unit tests * Including a new param to define whether to consider draft status when retrieving related orders * Fix child class missing param * Fix tests * Reverting changes after review * Changing the main implementation based on the code review * Revert unnecessary changes in test files * Including specific unit test * Fix tests * Fix tests * Update changelog.txt Co-authored-by: James Allan <[email protected]> * Update includes/class-wc-subscription.php Co-authored-by: James Allan <[email protected]> --------- Co-authored-by: James Allan <[email protected]>
1 parent 578bcfb commit 687d500

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** WooCommerce Subscriptions Core Changelog ***
22

33
= 7.4.0 - 2024-xx-xx =
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

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 {

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' ) );

0 commit comments

Comments
 (0)