diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-order.php b/plugins/woocommerce/includes/abstracts/abstract-wc-order.php index 47b88c331be..db134382477 100644 --- a/plugins/woocommerce/includes/abstracts/abstract-wc-order.php +++ b/plugins/woocommerce/includes/abstracts/abstract-wc-order.php @@ -199,8 +199,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { return $this->get_id(); } - $updating = $this->get_id() > 0; - try { /** * Trigger action before saving to the DB. Allows you to adjust object props before save. @@ -218,9 +216,9 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { $this->save_items(); - if ( $updating && OrderUtil::orders_cache_usage_is_enabled() ) { + if ( OrderUtil::orders_cache_usage_is_enabled() ) { $order_cache = wc_get_container()->get( OrderCache::class ); - $order_cache->update_if_cached( $this ); + $order_cache->remove( $this->get_id() ); } /** diff --git a/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php b/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php index 5a8cdf5e67b..313869c6dbd 100644 --- a/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php +++ b/plugins/woocommerce/tests/php/includes/abstracts/class-wc-abstract-order-test.php @@ -269,4 +269,23 @@ class WC_Abstract_Order_Test extends WC_Unit_Test_Case { $this->assertEquals( $coupon->get_id(), $coupon_data['id'] ); $this->assertEquals( $coupon_code, $coupon_data['code'] ); } + + /** + * @testDox Cache does not interfere if wc_get_order returns a different class than WC_Order. + */ + public function test_cache_does_not_interferes_with_order_object() { + add_action( + 'woocommerce_new_order', + function( $order_id ) { + // this makes the cache store a specific order class instance, but it's quickly replaced by a generic one + // as we're in the middle of a save and this gets executed before the logic in WC_Abstract_Order. + $order = wc_get_order( $order_id ); + } + ); + $order = new WC_Order(); + $order->save(); + + $order = wc_get_order( $order->get_id() ); + $this->assertInstanceOf( Automattic\WooCommerce\Admin\Overrides\Order::class, $order ); + } }