Automattic / woocommerce-subscriptions-core

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

Delete the needs_processing transient after copying line items to subscription orders #518

Closed james-allan closed 9 months ago

james-allan commented 9 months ago

Fixes #517

Description

There exists a small chance that appears to have affected a handful of stores where renewal orders go straight to completed status rather than processing. We haven't been able to find the root cause of this but based on the theory outlined by @kaushikasomaiya in the issue description, it may be caused by code calling the needs_processing() function after we've created the order but before we've added the line items.

This causes WC to cache the needs_processing value in a transient, and therefore incorrectly determine that the order doesn't need processing even though the line items have changed since that original determination.

How to test this PR

  1. Purchase a subscription with a product that does need processing (not downloadable).
  2. Run the following code snippet.
// Hook into the order being created and call $order->needs_processing(); so it caches a false value (before items are added)
add_action( 'woocommerce_before_order_object_save', function( $order ) {
    $order->needs_processing();
} );

// Create a renewal order. Replace the ID with your local Subscription ID.
$renewal_order = wcs_create_order_from_subscription( wcs_get_subscription( 4436 ), 'renewal_order' );

// Confirm the order needs processing. 
var_export( $renewal_order->needs_processing() ); // False on `trunk` true on this branch.
  1. On trunk you'll get the order doesn't need processing (false) and a completed order.
  2. On this branch it will be true and the order will be processing as expected.
Screenshot 2023-09-29 at 6 27 48 pm

Product impact