Skip to content
Open
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
108 changes: 108 additions & 0 deletions orders/renewal-label-for-renewal-orders-only.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Replace "Renewal" label logics showing "Renewal" for orders which are actually renewals.
*
* Orders status column in admin shows a not clear "Renewal" label for returning customers
* (orders from a customer who already had an order before - might be a renewal or not).
*
* Some people just want to see "Renewal" for orders which are actually renewals.
Comment on lines +3 to +8

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblock wording is grammatically incorrect/unclear (e.g., “label logics”, “a not clear”). Please rephrase for clarity since these snippet headers are user-facing in the recipe library.

Suggested change
* Replace "Renewal" label logics showing "Renewal" for orders which are actually renewals.
*
* Orders status column in admin shows a not clear "Renewal" label for returning customers
* (orders from a customer who already had an order before - might be a renewal or not).
*
* Some people just want to see "Renewal" for orders which are actually renewals.
* Adjust the logic for the "Renewal" label so it is only shown for orders that are actual renewals.
*
* By default, the Orders status column in the admin shows a "Renewal" label for any returning customer
* (orders from a customer who has placed a previous order, whether or not it is an actual renewal).
*
* Use this recipe if you want the "Renewal" label to appear only for true subscription renewal orders.

Copilot uses AI. Check for mistakes.
*
* title: Show "Renewal" for orders which are actually renewals.
* layout: snippet
* collection: orders
* category: status, renewal
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/

add_action( 'pmpro_orders_extra_cols_header', function ( $order_id ) {
echo '<th class="column-status-v2">' . esc_html__( 'Status', 'paid-memberships-pro' ) . '</th>';
} );

add_action( 'pmpro_orders_extra_cols_body', function ( $order ) {
?>
<td class="column-status-v2" data-colname="<?php esc_attr_e( 'Status', 'paid-memberships-pro' ); ?>">
<span class="pmpro_order-status pmpro_order-status-<?php esc_attr_e( $order->status ); ?>">
<?php esc_html_e( ucwords( $order->status ) ); ?>
Comment on lines +28 to +29

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esc_attr_e()/esc_html_e() are translation-and-echo helpers; using them with dynamic values like $order->status can invoke the wrong text domain (default) and is not intended for non-translatable data. Use proper escaping (esc_attr/esc_html) and echo the result instead.

Suggested change
<span class="pmpro_order-status pmpro_order-status-<?php esc_attr_e( $order->status ); ?>">
<?php esc_html_e( ucwords( $order->status ) ); ?>
<span class="pmpro_order-status pmpro_order-status-<?php echo esc_attr( $order->status ); ?>">
<?php echo esc_html( ucwords( $order->status ) ); ?>

Copilot uses AI. Check for mistakes.
</span>

<?php if ( pmpro_order_is_renewal_in_subscription( $order ) ) { ?>
<a href="<?php echo esc_url( add_query_arg( array(
'page' => 'pmpro-orders',
's' => $order->subscription_transaction_id
), admin_url( 'admin.php' ) ) ); ?>"
title="<?php esc_attr_e( 'View all orders for this subscription', 'paid-memberships-pro' ); ?>"
class="pmpro_order-renewal"><?php esc_html_e( 'Renewal', 'paid-memberships-pro' ); ?></a>
<?php } ?>
Comment on lines +32 to +39

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The “Renewal” link always searches by $order->subscription_transaction_id. For non-recurring renewals (where this field is empty) this renders a link with an empty s parameter, which won’t show the intended related orders. Consider only linking when a subscription transaction ID exists, or using a different query target for non-recurring renewals.

Suggested change
<?php if ( pmpro_order_is_renewal_in_subscription( $order ) ) { ?>
<a href="<?php echo esc_url( add_query_arg( array(
'page' => 'pmpro-orders',
's' => $order->subscription_transaction_id
), admin_url( 'admin.php' ) ) ); ?>"
title="<?php esc_attr_e( 'View all orders for this subscription', 'paid-memberships-pro' ); ?>"
class="pmpro_order-renewal"><?php esc_html_e( 'Renewal', 'paid-memberships-pro' ); ?></a>
<?php } ?>
<?php if ( pmpro_order_is_renewal_in_subscription( $order ) ) : ?>
<?php if ( ! empty( $order->subscription_transaction_id ) ) : ?>
<a href="<?php echo esc_url( add_query_arg( array(
'page' => 'pmpro-orders',
's' => $order->subscription_transaction_id,
), admin_url( 'admin.php' ) ) ); ?>"
title="<?php esc_attr_e( 'View all orders for this subscription', 'paid-memberships-pro' ); ?>"
class="pmpro_order-renewal"><?php esc_html_e( 'Renewal', 'paid-memberships-pro' ); ?></a>
<?php else : ?>
<span class="pmpro_order-renewal"><?php esc_html_e( 'Renewal', 'paid-memberships-pro' ); ?></span>
<?php endif; ?>
<?php endif; ?>

Copilot uses AI. Check for mistakes.

</td>
<?php
} );


/**
* New definition of Renewal is "any order which is not the first order for a subscription".
*
* @param MemberOrder $order
*
* @return bool|mixed
*/
function pmpro_order_is_renewal_in_subscription( $order ) {
global $wpdb;

// If our property is already set, use that.
if ( isset( $order->is_renewal ) ) {
return $order->is_renewal;
}

// Can't tell if this is a renewal without a user.
if ( empty( $order->user_id ) ) {
$order->is_renewal = false;

return $order->is_renewal;
}

if ( ! empty( $order->subscription_transaction_id ) ) {
// Logic for recurring orders.
$original_subscription_order = $order->get_original_subscription_order();
if ( $order->id !== $original_subscription_order->id ) {
$order->is_renewal = true;
} else {
$order->is_renewal = false;
}
} else {
// Logic for non-recurring orders.
$sqlQuery = "SELECT `id`
FROM $wpdb->pmpro_membership_orders
WHERE `user_id` = '" . esc_sql( $order->user_id ) . "'
AND `id` <> '" . esc_sql( $order->id ) . "'
AND `gateway_environment` = '" . esc_sql( $order->gateway_environment ) . "'
AND `total` > 0
AND `total` IS NOT NULL
AND status NOT IN('refunded', 'review', 'token', 'error')
AND timestamp < '" . esc_sql( date( 'Y-m-d H:i:s', $order->timestamp ) ) . "'
LIMIT 1";
Comment on lines +78 to +87

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The non-recurring renewal check builds SQL via string concatenation. Even with esc_sql(), this is error-prone and bypasses $wpdb->prepare() protections/type handling. Please switch to $wpdb->prepare() with placeholders and cast numeric fields (e.g., user_id/id) to integers.

Suggested change
$sqlQuery = "SELECT `id`
FROM $wpdb->pmpro_membership_orders
WHERE `user_id` = '" . esc_sql( $order->user_id ) . "'
AND `id` <> '" . esc_sql( $order->id ) . "'
AND `gateway_environment` = '" . esc_sql( $order->gateway_environment ) . "'
AND `total` > 0
AND `total` IS NOT NULL
AND status NOT IN('refunded', 'review', 'token', 'error')
AND timestamp < '" . esc_sql( date( 'Y-m-d H:i:s', $order->timestamp ) ) . "'
LIMIT 1";
$sqlQuery = $wpdb->prepare(
"SELECT `id`
FROM $wpdb->pmpro_membership_orders
WHERE `user_id` = %d
AND `id` <> %d
AND `gateway_environment` = %s
AND `total` > 0
AND `total` IS NOT NULL
AND status NOT IN('refunded', 'review', 'token', 'error')
AND timestamp < %s
LIMIT 1",
(int) $order->user_id,
(int) $order->id,
$order->gateway_environment,
date( 'Y-m-d H:i:s', (int) $order->timestamp )
);

Copilot uses AI. Check for mistakes.
$older_order_id = $wpdb->get_var( $sqlQuery );

if ( ! empty( $older_order_id ) ) {
$order->is_renewal = true;
} else {
$order->is_renewal = false;
}
}

return $order->is_renewal;
}

add_action( 'admin_footer', function () {
?>
<style>
body.memberships_page_pmpro-orders table.wp-list-table .column-status {
display: none;
}
</style>
<?php
} );