wc_delete_shop_order_transients()
Clear all transients cache for order data.
Hooks from the function
Returns
null. Nothing (null).
Usage
wc_delete_shop_order_transients( $order );
- $order(int|WC_Order)
- Order instance or ID.
wc_delete_shop_order_transients() wc delete shop order transients code WC 10.7.0
function wc_delete_shop_order_transients( $order = 0 ) {
if ( $order && is_numeric( $order ) ) {
$order = wc_get_order( $order );
}
// Clear customer's order related caches.
$order_id = 0;
if ( $order && is_a( $order, 'WC_Order' ) ) {
$order_id = $order->get_id();
$customer_id = $order->get_customer_id();
if ( $customer_id ) {
// Optimization note: the function is fired multiple times during order persistence lifecycle, and by
// verifying that metas carry non-empty values we ensure no repetitive attempts dropping the metas.
$metas_to_purge = array_filter(
array(
is_numeric( Users::get_site_user_meta( $customer_id, 'wc_order_count' ) ) ? 'wc_order_count' : '',
is_numeric( Users::get_site_user_meta( $customer_id, 'wc_last_order' ) ) ? 'wc_last_order' : '',
is_numeric( Users::get_site_user_meta( $customer_id, 'wc_money_spent' ) ) ? 'wc_money_spent' : '',
)
);
if ( ! empty( $metas_to_purge ) ) {
foreach ( $metas_to_purge as $meta ) {
Users::delete_site_user_meta( $customer_id, $meta );
}
}
}
}
// Increments the transient version to invalidate cache.
WC_Cache_Helper::get_transient_version( 'orders', true );
// Do the same for regular cache.
WC_Cache_Helper::invalidate_cache_group( 'orders' );
do_action( 'woocommerce_delete_shop_order_transients', $order_id );
}