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

Commit eaa119c

Browse files
barryhughesMatt Allan
andauthored
Increase number of args supported by wcs_get_subscriptions() (#811)
* Accept and pass arbitrary query args across to WC_Order_Query. * Add changelog entry. --------- Co-authored-by: Matt Allan <[email protected]>
1 parent e12381a commit eaa119c

File tree

3 files changed

+137
-66
lines changed

3 files changed

+137
-66
lines changed

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*** WooCommerce Subscriptions Core Changelog ***
22

3+
= 8.2.0 - 2025-xx-xx =
4+
* Update - Increase the number of args accepted by wcs_get_subscriptions(), to bring about parity with wc_get_orders().
5+
36
= 8.1.0 - 2025-03-24 =
47
* Update - Improved subscription search performance for WP Post stores by removing unnecessary _order_key and _billing_email meta queries.
58
* Update - Make it possible to dispatch the Cancelled Subscription email more than once (when initially set to pending-cancellation, and again when it reaches final cancellation).

tests/unit/test-wcs-functions.php

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,16 @@ public function test_1_wcs_get_subscriptions() {
824824
}
825825

826826
/**
827-
* Deals with cases where we're filtering for status
827+
* Deals with cases where we're querying by status.
828+
*
829+
* Status can be specified in two different ways. Traditionally, via the
830+
* 'subscription_status' argument, but 'status' can also be used directly
831+
* since 7.3.0.
832+
*
833+
* @testWith ["subscription_status"]
834+
* ["status"]
828835
*/
829-
public function test_2_wcs_get_subscriptions() {
836+
public function test_2_wcs_get_subscriptions( string $status_key ) {
830837

831838
$subscription_1 = WCS_Helper_Subscription::create_subscription(
832839
array(
@@ -877,7 +884,7 @@ public function test_2_wcs_get_subscriptions() {
877884
);
878885

879886
// Check for on-hold
880-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'on-hold' ) );
887+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'on-hold' ) );
881888

882889
$this->assertIsArray( $subscriptions );
883890
$this->assertEquals( 1, count( $subscriptions ) );
@@ -892,7 +899,7 @@ public function test_2_wcs_get_subscriptions() {
892899
unset( $subscriptions );
893900

894901
// Pending
895-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'pending' ) );
902+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'pending' ) );
896903

897904
$this->assertIsArray( $subscriptions );
898905
$this->assertEquals( 2, count( $subscriptions ) );
@@ -907,7 +914,7 @@ public function test_2_wcs_get_subscriptions() {
907914
unset( $subscriptions );
908915

909916
// Switched
910-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'switched' ) );
917+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'switched' ) );
911918

912919
$this->assertIsArray( $subscriptions );
913920
$this->assertEquals( 1, count( $subscriptions ) );
@@ -922,7 +929,7 @@ public function test_2_wcs_get_subscriptions() {
922929
unset( $subscriptions );
923930

924931
// Any
925-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'any' ) );
932+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'any' ) );
926933

927934
$this->assertIsArray( $subscriptions );
928935
$this->assertEquals( 8, count( $subscriptions ) );
@@ -937,14 +944,14 @@ public function test_2_wcs_get_subscriptions() {
937944
unset( $subscriptions );
938945

939946
// Trash
940-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'trash' ) );
947+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'trash' ) );
941948

942949
$this->assertIsArray( $subscriptions );
943950
$this->assertEmpty( $subscriptions );
944951
unset( $subscriptions );
945952

946953
// Active
947-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'active' ) );
954+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'active' ) );
948955

949956
$this->assertIsArray( $subscriptions );
950957
$this->assertEquals( 1, count( $subscriptions ) );
@@ -959,7 +966,7 @@ public function test_2_wcs_get_subscriptions() {
959966
unset( $subscriptions );
960967

961968
// Cancelled
962-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'cancelled' ) );
969+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'cancelled' ) );
963970

964971
$this->assertIsArray( $subscriptions );
965972
$this->assertEquals( 1, count( $subscriptions ) );
@@ -974,7 +981,7 @@ public function test_2_wcs_get_subscriptions() {
974981
unset( $subscriptions );
975982

976983
// Expired
977-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'expired' ) );
984+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'expired' ) );
978985

979986
$this->assertIsArray( $subscriptions );
980987
$this->assertEquals( 1, count( $subscriptions ) );
@@ -989,7 +996,7 @@ public function test_2_wcs_get_subscriptions() {
989996
unset( $subscriptions );
990997

991998
// Pending Cancellation
992-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'pending-cancel' ) );
999+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'pending-cancel' ) );
9931000

9941001
$this->assertIsArray( $subscriptions );
9951002
$this->assertEquals( 1, count( $subscriptions ) );
@@ -1006,7 +1013,7 @@ public function test_2_wcs_get_subscriptions() {
10061013
$is_hpos_enabled = wcs_is_custom_order_tables_usage_enabled();
10071014

10081015
// An invalid status
1009-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => 'rubbish' ) );
1016+
$subscriptions = wcs_get_subscriptions( array( $status_key => 'rubbish' ) );
10101017

10111018
if ( $is_hpos_enabled ) {
10121019
// No subscriptions should match the invalid status.
@@ -1029,7 +1036,7 @@ public function test_2_wcs_get_subscriptions() {
10291036
unset( $subscriptions );
10301037

10311038
// An invalid status is ignored and does not apply as a clause to the query, while the valid status still applies.
1032-
$subscriptions = wcs_get_subscriptions( array( 'subscription_status' => [ 'rubbish', 'active' ] ) );
1039+
$subscriptions = wcs_get_subscriptions( array( $status_key => [ 'rubbish', 'active' ] ) );
10331040

10341041
$this->assertIsArray( $subscriptions );
10351042
$this->assertEquals( 1, count( $subscriptions ) );
@@ -1117,6 +1124,52 @@ public function test_5_wcs_get_subscriptions() {
11171124
$this->assertEquals( $subscriptions, $correct_order );
11181125
}
11191126

1127+
/**
1128+
* Since 7.3.0, the number of queries accepted by wcs_get_subscriptions() has broadened,
1129+
* and arbitrary arguments can be provided (chiefly to open up access to the features of
1130+
* WooCommerce's own order queries).
1131+
*
1132+
* @return void
1133+
*/
1134+
public function test_wcs_get_subscriptions_wc_order_query_compat() {
1135+
$old_subscription = WCS_Helper_Subscription::create_subscription(
1136+
array(
1137+
'status' => 'pending',
1138+
'start_date' => '2024-12-31 00:00:00',
1139+
'customer_id' => wp_create_user( 'withnail', 'x', '[email protected]' ),
1140+
)
1141+
);
1142+
1143+
$new_subscription = WCS_Helper_Subscription::create_subscription(
1144+
array(
1145+
'status' => 'pending',
1146+
'start_date' => '2025-12-31 00:00:00',
1147+
'customer_id' => wp_create_user( 'marwood', 'x', '[email protected]' ),
1148+
)
1149+
);
1150+
1151+
$old_subscription->set_date_created( '2024-12-31 00:00:00' );
1152+
$new_subscription->set_date_created( '2025-12-31 00:00:00' );
1153+
$old_subscription->save();
1154+
$new_subscription->save();
1155+
1156+
$old_subscriptions = wcs_get_subscriptions( array( 'date_created' => '<=2025-06-01' ) );
1157+
$this->assertCount( 1, $old_subscriptions, '`WC_Order_Query` args such as `date_created` can be used when querying for subscriptions.' );
1158+
$this->assertEquals( $old_subscription->get_id(), current( $old_subscriptions )->get_id(), 'The correct subscription is returned.' );
1159+
1160+
// Watch for and capture the query arguments passed to WC_Order_Query.
1161+
$query_args = array();
1162+
$query_arg_watcher = function ( $query, $args ) use ( &$query_args ) {
1163+
$query_args = $args;
1164+
return $query;
1165+
};
1166+
1167+
add_filter( 'woocommerce_order_query', $query_arg_watcher, 10, 2 );
1168+
wcs_get_subscriptions( array( 'foo_bar' => 'baz' ) );
1169+
$this->assertArrayHasKey( 'foo_bar', $query_args, 'When querying subscriptions, and additional or arbitrary arguments are also passed to `WC_Order_Query`.' );
1170+
remove_filter( 'woocommerce_order_query', $query_arg_watcher );
1171+
}
1172+
11201173
/**
11211174
* Test for non-existent product
11221175
*/

0 commit comments

Comments
 (0)