Skip to content

Commit

Permalink
Removing wc-checkout-draft status from the get related order IDs meth…
Browse files Browse the repository at this point in the history
…od (#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]>
  • Loading branch information
wjrosa and james-allan authored Aug 16, 2024
1 parent 578bcfb commit 687d500
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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

0 comments on commit 687d500

Please sign in to comment.