woocommerce / wc-smooth-generator

Smooth product, customer and order generation for WooCommerce
309 stars 49 forks source link

Set paid and completed dates #143

Closed mikkamp closed 1 month ago

mikkamp commented 2 months ago

All Submissions:

Changes proposed in this Pull Request:

When we generate orders we are able to generate them over a range of dates. This works correctly when setting the order creation date. However other dates will default to the current date.

So if we generate a set of orders for the Analytics reports the date_paid will all be at the time of when the order was generated. Which isn't very useful for populating some test data for these reports.

This PR changes that behaviour and allows setting both the paid date as well as the order completed date. This is set based on the chosen status of the order. Paid date is set for both processing and completed. Completed date is set for only completed orders. The date is set by adding a random amount of 0 to 36 hours to the creation date.

How to test the changes in this Pull Request:

  1. Generate a set of orders in WP-CLI, for January wp wc generate orders 10 --date-start=2024-01-01 --date-end=2024-01-31
  2. Go to Analytics > Orders
  3. Set a custom date range to view all orders from January
  4. Confirm that we are seeing orders within the report (with a date of January and not the date we generate the orders) image

Other information:

Changelog entry

Set paid and completed dates based on order status.

FOR PR REVIEWER ONLY:

mikkamp commented 1 month ago

@coreymckrill Thanks for the quick review. I went ahead and added that offset to the payment date as well.

Looking in the DB table for wc_order_stats I can now see the last 10 orders have a random offset for the payment date: image

Would you mind taking one more quick look that all is well?

coreymckrill commented 1 month ago

@mikkamp looks good! Another thing just occurred to me, though. In some cases, adding up to 36 hours to the paid/completed date could end up making those dates be in the future. That doesn't seem great to me, but I could be convinced otherwise.

If we want to prevent that, we could potentially do something like this:

min( strtotime( $date ) + ( wp_rand( 0, 36 ) * HOUR_IN_SECONDS ), time() )
mikkamp commented 1 month ago

Another thing just occurred to me, though. In some cases, adding up to 36 hours to the paid/completed date could end up making those dates be in the future. That doesn't seem great to me, but I could be convinced otherwise.

That is true, but considering that we are also able to generate orders in the future, with something like: wp wc generate orders 10 --date-start=2024-10-01 --date-end=2024-10-30

I didn't think it would be such a big deal to have payment dates in the future. If they would really want to prevent that then they could always specify an older date range to prevent it from getting anything in the future.