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

Removing wc-checkout-draft status from the get related order IDs method #626

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** WooCommerce Subscriptions Core Changelog ***

= 7.4.0 - 2024-xx-xx =
* Dev - Introduce new parameter to WC_Subscription::get_last_order() to enable filtering out orders with specific statuses.
* Update - Schedule subscription-related events with a priority of 1 to allow for earlier execution within the Action Scheduler.
* Fix - Ensure admin notices are displayed after performing bulk actions on subscriptions when HPOS is enabled.

Expand Down
13 changes: 12 additions & 1 deletion includes/class-wc-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -2077,9 +2077,10 @@ protected function get_related_order_ids( $order_type = 'any' ) {
*
* @param string $return_fields The columns to return, either 'all' or 'ids'
* @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'.
* @param array $exclude_statuses An array of statuses to exclude from the search. Defaults to an empty array.
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.0
*/
public function get_last_order( $return_fields = 'ids', $order_types = array( 'parent', 'renewal' ) ) {
public function get_last_order( $return_fields = 'ids', $order_types = array( 'parent', 'renewal' ), $exclude_statuses = [] ) {

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

if ( ! empty( $exclude_statuses ) ) {
$related_orders = array_filter(
$related_orders,
function( $order_id ) use ( $exclude_statuses ) {
$order = wc_get_order( $order_id );
return $order && ! $order->has_status( $exclude_statuses );
}
);
}

if ( empty( $related_orders ) ) {
$last_order = false;
} else {
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test-class-wc-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,11 @@ public function test_get_last_order() {
$this->assertEquals( $order_id, $subscription->get_last_order() );
$this->assertEquals( wc_get_order( $order_id ), $subscription->get_last_order( 'all' ) );

// Test for the status filtering parameter
$order->update_status( 'failed' );
$order->save();
$this->assertFalse( $subscription->get_last_order( 'ids', array( 'parent', 'renewal' ), array( 'failed' ) ) );

$renewal = WCS_Helper_Subscription::create_renewal_order( $subscription );
$renewal_id = wcs_get_objects_property( $renewal, 'id' );
$this->assertEquals( $renewal_id, $subscription->get_last_order( 'ids' ) );
Expand Down
Loading