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

fix(esp-sync): get last payment amount from actual completed order #3726

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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
38 changes: 37 additions & 1 deletion includes/reader-activation/sync/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ private static function get_one_time_donation_order_for_user( $user_id ) {
}
}

/**
* Get the amount of the last payment associated with the given subscription.
*
* @param \WC_Subscription $subscription Subscription object.
*
* @return float The amount of the last payment.
*/
private static function get_last_payment_amount( $subscription ) {
$last_order = $subscription->get_last_order(
// The whole WC_Order object, not just the ID.
'all',
// Only parent and renewal orders.
[
'parent',
'renewal',
],
// Only completed or processing orders, so exclude all other statuses.
[
'pending',
'failed',
'on-hold',
'cancelled',
'trash',
'draft',
'auto-draft',
'new',
]
);

if ( ! $last_order ) {
return 0;
}

return $last_order->get_total();
}

/**
* Get data about a customer's order to sync to the connected ESP.
*
Expand Down Expand Up @@ -290,7 +326,7 @@ private static function get_order_metadata( $order, $payment_page_url = false )
$metadata['sub_end_date'] = $current_subscription->get_date( 'end' ) ? $current_subscription->get_date( 'end' ) : '';
$metadata['billing_cycle'] = $current_subscription->get_billing_period();
$metadata['recurring_payment'] = $current_subscription->get_total();
$metadata['last_payment_amount'] = $current_subscription->get_total();
$metadata['last_payment_amount'] = self::get_last_payment_amount( $current_subscription );
$metadata['last_payment_date'] = $current_subscription->get_date( 'last_order_date_paid' ) ? $current_subscription->get_date( 'last_order_date_paid' ) : gmdate( Metadata::DATE_FORMAT );

// When a WC Subscription is terminated, the next payment date is set to 0. We don't want to sync that – the next payment date should remain as it was
Expand Down