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

Commit 3415437

Browse files
committed
Including a new param to define whether to consider draft status when retrieving related orders
1 parent bb49bf0 commit 3415437

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

includes/abstracts/abstract-wcs-related-order-store.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ protected function init() {}
8686
*
8787
* @param WC_Order $subscription The order or subscription for which calling code wants to find related orders.
8888
* @param string $relation_type The relationship between the subscription and the orders. Must be 'renewal', 'switch' or 'resubscribe' unless custom relationships are implemented.
89+
* @param bool $include_draft Whether to include draft orders in the results (`wc-checkout-draft` status).
8990
*
9091
* @return array
9192
*/
92-
abstract public function get_related_order_ids( WC_Order $subscription, $relation_type );
93+
abstract public function get_related_order_ids( WC_Order $subscription, $relation_type, $include_draft = true );
9394

9495
/**
9596
* Find subscriptions related to a given order in a given way, if any.

includes/class-wc-subscription.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,10 +2049,11 @@ public function get_related_orders( $return_fields = 'ids', $order_types = array
20492049
* Get the related order IDs for a subscription based on an order type.
20502050
*
20512051
* @param string $order_type Can include 'any', 'parent', 'renewal', 'resubscribe' and/or 'switch'. Defaults to 'any'.
2052+
* @param bool $include_draft Whether to include draft orders in the search. Defaults to true.
20522053
* @return array List of related order IDs.
20532054
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.3.0
20542055
*/
2055-
protected function get_related_order_ids( $order_type = 'any' ) {
2056+
protected function get_related_order_ids( $order_type = 'any', $include_draft = true ) {
20562057

20572058
$related_order_ids = array();
20582059

@@ -2065,7 +2066,7 @@ protected function get_related_order_ids( $order_type = 'any' ) {
20652066
$relation_types = ( 'any' === $order_type ) ? array( 'renewal', 'resubscribe', 'switch' ) : array( $order_type );
20662067

20672068
foreach ( $relation_types as $relation_type ) {
2068-
$related_order_ids = array_merge( $related_order_ids, WCS_Related_Order_Store::instance()->get_related_order_ids( $this, $relation_type ) );
2069+
$related_order_ids = array_merge( $related_order_ids, WCS_Related_Order_Store::instance()->get_related_order_ids( $this, $relation_type, $include_draft ) );
20692070
}
20702071
}
20712072

@@ -2077,9 +2078,10 @@ protected function get_related_order_ids( $order_type = 'any' ) {
20772078
*
20782079
* @param string $return_fields The columns to return, either 'all' or 'ids'
20792080
* @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'.
2081+
* @param bool $include_draft Whether to include draft orders in the search. Defaults to true.
20802082
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.0
20812083
*/
2082-
public function get_last_order( $return_fields = 'ids', $order_types = array( 'parent', 'renewal' ) ) {
2084+
public function get_last_order( $return_fields = 'ids', $order_types = array( 'parent', 'renewal' ), $include_draft = true ) {
20832085

20842086
$return_fields = ( 'ids' == $return_fields ) ? $return_fields : 'all';
20852087
$order_types = ( 'any' == $order_types ) ? array( 'parent', 'renewal', 'switch' ) : (array) $order_types;
@@ -2093,7 +2095,7 @@ public function get_last_order( $return_fields = 'ids', $order_types = array( 'p
20932095
}
20942096
break;
20952097
default:
2096-
$related_orders = array_merge( $related_orders, $this->get_related_order_ids( $order_type ) );
2098+
$related_orders = array_merge( $related_orders, $this->get_related_order_ids( $order_type, $include_draft ) );
20972099
break;
20982100
}
20992101
}

includes/data-stores/class-wcs-related-order-store-cpt.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ public function __construct() {
3636
*
3737
* @param WC_Order $subscription The ID of the subscription for which calling code wants the related orders.
3838
* @param string $relation_type The relationship between the subscription and the orders. Must be 'renewal', 'switch' or 'resubscribe.
39+
* @param bool $include_draft Whether to include draft orders in the results (`wc-checkout-draft` status).
3940
*
4041
* @return array
4142
*/
42-
public function get_related_order_ids( WC_Order $subscription, $relation_type ) {
43-
// Filter out the new 'wc-checkout-draft' status, as it is not a valid order status for our purposes.
44-
$statuses = array_keys( wc_get_order_statuses() );
45-
unset( $statuses[ array_search( 'wc-checkout-draft', $statuses, true ) ] );
43+
public function get_related_order_ids( WC_Order $subscription, $relation_type, $include_draft = true ) {
44+
if ( ! $include_draft ) {
45+
$statuses = array_keys( wc_get_order_statuses() );
46+
unset( $statuses[ array_search( 'wc-checkout-draft', $statuses, true ) ] );
47+
}
4648
return wcs_get_orders_with_meta_query(
4749
[
4850
'limit' => -1,

0 commit comments

Comments
 (0)