woocommerce-subscriptions-importer-exporter icon indicating copy to clipboard operation
woocommerce-subscriptions-importer-exporter copied to clipboard

fix conflict between cancelled vs. next_payment dates for pending-cancel

Open dkoo opened this issue 11 months ago • 1 comments

#278 introduced cancelled_date as an importable column, and also fixed some behavior which was preventing end_date from being properly applied to imported subscriptions. However, it also introduced a potential conflict between cancelled_date and next_payment dates for imported subscriptions with a pending-cancel status.

Some preexisting logic in master forces a next_payment date for pending-cancel subscriptions, which becomes the end date for those subscriptions after being imported. However, if a cancelled_date is also set for these subscriptions, importing will fail with an error as pending-cancel subscriptions can't have a cancelled_date that occurs before next_payment.

This PR proposes a fix to not force next_payment for these subscriptions. However, pending-cancel subscriptions do need to ultimately have an end_date, otherwise they'll essentially continue to be active indefinitely. So the proposed change checks pending-cancel subs for a future next_payment date, and if it exists, moves it to the end_date instead and clears next_payment to avoid the import error. In local testing this resulted in imported pending-cancel subscriptions with the same date behavior/data schema as subscriptions that were manually moved to pending-cancel status via admin or subscriber action.

If the subscription to be imported lacks both an end_date and a next_payment date, or both are in the past, the importer will continue to throw an error as it did before in this scenario.

To test:

  1. On master, import a subscription that has both a cancelled_date and an end_date. Here's some sample data you could use to import (replace <product ID> with a valid product ID on your test site):
customer_id,customer_email,customer_username,customer_password,billing_first_name,billing_last_name,billing_address_1,billing_address_2,billing_city,billing_state,billing_postcode,billing_country,billing_email,billing_phone,billing_company,shipping_first_name,shipping_last_name,shipping_address_1,shipping_address_2,shipping_city,shipping_state,shipping_postcode,shipping_country,subscription_status,start_date,trial_end_date,next_payment_date,cancelled_date,end_date,billing_period,billing_interval,order_items,coupon_items,fee_items,tax_items,cart_discount,cart_discount_tax,order_shipping,order_shipping_tax,order_total,order_tax,order_currency,shipping_method,download_permissions,order_notes,payment_method,payment_method_title,payment_method_post_meta,payment_method_user_meta,customer_note
,[email protected],[email protected],,,,,,,,,,[email protected],,,,,,,,,,,wc-pending-cancel,2024-03-15 08:40:00,,,2024-03-17 13:38:00,2025-03-15 08:40:00,year,1,product_id:<product ID>|name:|quantity:1|subtotal_tax:0|subtotal:60|tax:0|total:60,,,,,,,,60,,,,1,,,Manual Renewal,,,
  1. Observe an error upon import:
Row #1 from CSV failed to import with error/s:
1. Subscription #1111: The cancelled date must occur after the next payment date.
  1. Apply the patch in this branch and repeat.
  2. Confirm that the subscription is imported with the correct "Pending cancellation" status, and the correct cancellation and end dates:
Screenshot 2024-03-28 at 1 41 24 PM
  1. Edit the CSV data and move the end_date value to the next_payment_date column instead. Import and confirm that it successfully imports with the same results as in step 4.
  2. Edit the CSV once more and include both a future end_date and a different next_payment_date value. Import and confirm that it successfully imports with the end date intact, but discards the next payment date.

dkoo avatar Mar 28 '24 19:03 dkoo

@james-allan as the person who helped me with the last couple of PRs, could I ask for a review of this one too?

dkoo avatar May 08 '24 17:05 dkoo