Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements: rendering the Subscription Relationship column and checking if order is a renewal, switch etc. #732

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
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
Expand Up @@ -3,6 +3,7 @@
= 7.8.0 - xxxx-xx-xx =
* Fix - Use block theme-styled buttons for subscription and related-orders actions on My Account pages.
* Update - Changed the link on the order thank-you page to take customers directly to their "My Account > Subscriptions" page.
* Update - Improved performance when checking if an order is a subscription renewal, resubscribe or switch order.

= 7.7.1 - 2024-11-13 =
* Fix - Only show the individual subscription information in customer notification emails, not all subscriptions purchased in the initial order.
Expand Down
18 changes: 12 additions & 6 deletions includes/class-wc-subscriptions-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public static function add_contains_subscription_column_content( $column ) {
*/
public static function add_contains_subscription_column_content_orders_table( string $column_name, WC_Order $order ) {
if ( 'subscription_relationship' === $column_name ) {
self::render_contains_subscription_column_content( $order->get_id() );
self::render_contains_subscription_column_content( $order );
}
}

Expand Down Expand Up @@ -2441,14 +2441,20 @@ private static function render_restrict_manage_subscriptions_dropdown() {
*
* @since 6.3.0
*
* @param integer $order_id The ID of the order in the current row.
* @param WC_Order $order The order in the current row.
*/
private static function render_contains_subscription_column_content( int $order_id ) {
if ( wcs_order_contains_subscription( $order_id, 'renewal' ) ) {
private static function render_contains_subscription_column_content( $order ) {
$order = ! is_object( $order ) ? wc_get_order( $order ) : $order;

if ( ! $order ) {
return;
}

if ( wcs_order_contains_renewal( $order ) ) {
echo '<span class="subscription_renewal_order tips" data-tip="' . esc_attr__( 'Renewal Order', 'woocommerce-subscriptions' ) . '"></span>';
} elseif ( wcs_order_contains_subscription( $order_id, 'resubscribe' ) ) {
} elseif ( wcs_order_contains_resubscribe( $order ) ) {
echo '<span class="subscription_resubscribe_order tips" data-tip="' . esc_attr__( 'Resubscribe Order', 'woocommerce-subscriptions' ) . '"></span>';
} elseif ( wcs_order_contains_subscription( $order_id, 'parent' ) ) {
} elseif ( wcs_is_parent_order( $order ) ) {
echo '<span class="subscription_parent_order tips" data-tip="' . esc_attr__( 'Parent Order', 'woocommerce-subscriptions' ) . '"></span>';
} else {
echo '<span class="normal_order">&ndash;</span>';
Expand Down
28 changes: 27 additions & 1 deletion includes/wcs-order-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function wcs_order_contains_subscription( $order, $order_type = array( 'parent',
$contains_subscription = false;
$get_all = in_array( 'any', $order_type, true );

if ( ( in_array( 'parent', $order_type, true ) || $get_all ) && count( wcs_get_subscriptions_for_order( $order->get_id(), array( 'order_type' => 'parent' ) ) ) > 0 ) {
if ( ( in_array( 'parent', $order_type, true ) || $get_all ) && wcs_is_parent_order( $order ) ) {
$contains_subscription = true;

} elseif ( ( in_array( 'renewal', $order_type, true ) || $get_all ) && wcs_order_contains_renewal( $order ) ) {
Expand Down Expand Up @@ -1055,3 +1055,29 @@ function wcs_set_recurring_item_total( &$item ) {
]
);
}

/**
* Checks if an order is a Subscriptions parent/initial order.
*
* @param WC_Order|int $order The WC_Order object or ID of a WC_Order order.
*/
function wcs_is_parent_order( $order ) {
$order = ! is_object( $order ) ? wc_get_order( $order ) : $order;

if ( ! $order || ! wcs_is_order( $order ) ) {
return false;
}

// Check if the order ID is the parent of a subscription.
$is_parent_order = wc_get_orders(
[
'parent' => $order->get_id(),
'type' => 'shop_subscription',
'status' => 'any',
'limit' => 1,
'return' => 'ids',
]
);

return apply_filters( 'woocommerce_subscriptions_is_parent_order', ! empty( $is_parent_order ), $order );
}
5 changes: 3 additions & 2 deletions includes/wcs-renewal-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ function wcs_order_contains_renewal( $order ) {
$order = wc_get_order( $order );
}

$related_subscriptions = wcs_get_subscriptions_for_renewal_order( $order );
// Pluck the `_subscription_renewal` meta key from the order.
$related_subscription_ids = WCS_Related_Order_Store::instance()->get_related_subscription_ids( $order, 'renewal' );

if ( wcs_is_order( $order ) && ! empty( $related_subscriptions ) ) {
if ( wcs_is_order( $order ) && ! empty( $related_subscription_ids ) ) {
$is_renewal = true;
} else {
$is_renewal = false;
Expand Down
5 changes: 3 additions & 2 deletions includes/wcs-resubscribe-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ function wcs_order_contains_resubscribe( $order ) {
$order = wc_get_order( $order );
}

$related_subscriptions = wcs_get_subscriptions_for_resubscribe_order( $order );
// Pluck the `_subscription_resubscribe` meta key from the order
$related_subscription_ids = WCS_Related_Order_Store::instance()->get_related_subscription_ids( $order, 'resubscribe' );

if ( wcs_is_order( $order ) && ! empty( $related_subscriptions ) ) {
if ( wcs_is_order( $order ) && ! empty( $related_subscription_ids ) ) {
$is_resubscribe_order = true;
} else {
$is_resubscribe_order = false;
Expand Down
11 changes: 2 additions & 9 deletions includes/wcs-switch-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ function wcs_order_contains_switch( $order ) {
}

if ( ! wcs_is_order( $order ) || wcs_order_contains_renewal( $order ) ) {

$is_switch_order = false;

} else {

$switched_subscriptions = wcs_get_subscriptions_for_switch_order( $order );

if ( ! empty( $switched_subscriptions ) ) {
$is_switch_order = true;
} else {
$is_switch_order = false;
}
$switched_subscription_ids = WCS_Related_Order_Store::instance()->get_related_subscription_ids( $order, 'switch' );
$is_switch_order = ! empty( $switched_subscription_ids );
}

return apply_filters( 'woocommerce_subscriptions_is_switch_order', $is_switch_order, $order );
Expand Down
Loading