Remove caching on order save, instead do it when order is fetched.

This allows all the hooks when getting an order to be executed and we cache the correct object.
This commit is contained in:
Vedanshu Jain 2023-04-05 13:26:11 +05:30
parent 1d6c98985e
commit 817458a433
2 changed files with 21 additions and 4 deletions

View File

@ -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() );
}
/**

View File

@ -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 );
}
}