Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
87 stars 33 forks source link

Fix missing start date errors on new subscriptions for the schedule metabox #362

Closed james-allan closed 1 year ago

james-allan commented 1 year ago

Fixes #361

πŸ›‘ Blocked by https://github.com/Automattic/woocommerce-subscriptions-core/pull/336 πŸ›‘

This PR requires the changes in #336 to backfill the start date. The existing code checks if the start date is set, whereas it use to check if it is empty. #336 includes the changes which honours the old behavior of checking if the star date is empty not isset.

Description

When a new subscription is created via the admin screen, WC creates a new subscription in setup_action_new_order().

That function creates the subscription via new WC_Subscription(). Eventually that newly created subscription instance is passed around to all the metabox functions and used to populate the edit screen.

However when it gets to the subscription schedule metabox, this HPOS approach changed the existing behaviour because we almost always use to get a fresh instance of the subscription via the global post ID, now with an instance of the subscription, this was no longer necessary. There was a small reason to still get the subscription, however.

When a subscription is created, it doesn't have a start date by default. When you load (read) a subscription with no start date, we set it to the date the subscription was created.

And so, before these changes, if you went to the admin subscriptions list table and clicked Add new you'd get an edit subscription screen with no start date pre-populated.

This PR fixes that by making sure we do a read on the subscription if the start date is empty indicating that the subscription is brand new. This will trigger the start date to be backfilled with the created date and we're back to how this use to work.

Because the start date wasn't being pre-populated, youd get the following error when you'd save the subscription:

PHP Warning:  Undefined array key "start" in public/wp-content/plugins/woocommerce-subscriptions-core/includes/class-wc-subscription.php on line 2459

How to test this PR

  1. Enable HPOS tables.
  2. Go to WooCommerce > Subscriptions > Add new
  3. Note the subscription start date is empty.
  4. Add items, a customer etc and click Save.
  5. You should get the error message mentioned above.

Product impact