Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
81 stars 29 forks source link

When creating subscriptions convert subscription date created to a timestamp so it is treated as UTC #526

Closed james-allan closed 8 months ago

james-allan commented 8 months ago

Description

When a WP site has a timezone set in the settings, subscriptions created via the checkout have a date created that is offset by the timezone incorrectly.

This is caused by a bug in the wcs_create_subscription() function where it passes a UTC mysql date string (2023-10-12 14:53:24) to $subscription->set_date_created() but if you look at the description of that function you'll notice that it says that:

If the DateTime string has no timezone or offset, WordPress site timezone will be assumed.

This means that when we pass a mysql date format, it assumes the date is in the site's local time.

This PR fixes it by converting the mysql date into a timestamp which the set_date_created() will assume is in UTC.

How to test this PR

  1. Go to the WordPress admin dashboard.
  2. Go to Settings → General and set the site's timezone (eg UTC+10)
Screenshot 2023-10-12 at 4 41 54 pm

4.Purchase a subscription through the checkout.

  1. Go to the admin |Orders list table.
  2. Edit the newly created order and scroll down to the related orders table.
    • On develop note the subscription has a created date -10 hours.
    • On this branch it should be correct.
Screenshot 2023-10-12 at 5 10 14 pm

You can also check the date created in the database and you'll notice that on develop the GMT date is double the expected offset (eg -20 hours).

Product impact