Skip to content

Commit

Permalink
fix(esp-sync): transform outgoing dates to site timzeone (#3728)
Browse files Browse the repository at this point in the history
* fix(esp-sync): transform outgoing dates to site timzeone

* Ditch regex for simpler date create and check validation

Co-authored-by: Derrick Koo <[email protected]>

* fix: do some quick checks and reuse date

* fix: always use site tz in all metadata in sync

* fix: move code and don't null coalece

---------

Co-authored-by: Derrick Koo <[email protected]>
  • Loading branch information
naxoc and dkoo authored Feb 13, 2025
1 parent 882d55c commit 77dc361
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions includes/reader-activation/sync/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ private static function get_last_payment_amount( $subscription ) {
/**
* Get data about a customer's order to sync to the connected ESP.
*
* Note that all dates are in the site's timezone.
*
* @param \WC_Order|int $order WooCommerce order or order ID.
* @param bool|string $payment_page_url Payment page URL. If not provided, checkout URL will be used.
*
Expand All @@ -231,11 +233,6 @@ private static function get_order_metadata( $order, $payment_page_url = false )
return [];
}

$is_subscription = false;
if ( function_exists( 'wcs_is_subscription' ) ) {
$is_subscription = \wcs_is_subscription( $order );
}

$metadata = [];

if ( empty( $payment_page_url ) ) {
Expand Down Expand Up @@ -322,16 +319,20 @@ private static function get_order_metadata( $order, $payment_page_url = false )
$metadata['membership_status'] = $current_subscription->get_status();
}

$metadata['sub_start_date'] = $current_subscription->get_date( 'start' );
$metadata['sub_end_date'] = $current_subscription->get_date( 'end' ) ? $current_subscription->get_date( 'end' ) : '';
$sub_start_date = $current_subscription->get_date( 'start', 'site' );
$sub_end_date = $current_subscription->get_date( 'end', 'site' );
$last_payment_date = $current_subscription->get_date( 'last_order_date_paid', 'site' );

$metadata['sub_start_date'] = empty( $sub_start_date ) ? '' : $sub_start_date;
$metadata['sub_end_date'] = empty( $sub_end_date ) ? '' : $sub_end_date;
$metadata['billing_cycle'] = $current_subscription->get_billing_period();
$metadata['recurring_payment'] = $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 );
$metadata['last_payment_date'] = empty( $last_payment_date ) ? current_time( Metadata::DATE_FORMAT ) : $last_payment_date;

// 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
// in the event of cancellation.
$next_payment_date = $current_subscription->get_date( 'next_payment' );
$next_payment_date = $current_subscription->get_date( 'next_payment', 'site' );
if ( $next_payment_date ) {
$metadata['next_payment_date'] = $next_payment_date;
}
Expand Down Expand Up @@ -382,10 +383,9 @@ public static function get_contact_from_customer( $customer, $payment_page_url =
$metadata = [];

$customer_id = $customer->get_id();
$user = \get_user_by( 'id', $customer_id );
$created_date = $customer->get_date_created();
$metadata['account'] = $customer_id;
$metadata['registration_date'] = $created_date ? $created_date->date( Metadata::DATE_FORMAT ) : '';
$metadata['registration_date'] = $created_date ? get_date_from_gmt( $created_date->date( Metadata::DATE_FORMAT ) ) : '';
$metadata['total_paid'] = $customer->get_total_spent();

$order = self::get_current_product_order_for_sync( $customer );
Expand Down

0 comments on commit 77dc361

Please sign in to comment.