$order ): void { $order_id = (int) $order_id; $this->invalidate( $order_id ); if ( $this->did_customer_change( $order_id, $order ) ) { $this->invalidate_orders_list(); } unset( $this->pre_save_customer_ids[ $order_id ] ); } /** * Check if the customer ID changed during the update. * * @param int $order_id The order ID. * @param \WC_Order $order The order object (after save). * * @return bool True if customer changed. */ private function did_customer_change( int $order_id, $order ): bool { if ( ! isset( $this->pre_save_customer_ids[ $order_id ] ) ) { return false; } $old_customer_id = $this->pre_save_customer_ids[ $order_id ]; $new_customer_id = $order instanceof \WC_Order ? (int) $order->get_customer_id() : 0; return $old_customer_id !== $new_customer_id; } /** * Handle the woocommerce_before_delete_order hook. * * @param int $order_id The order ID. * @param \WC_Order $order The order object. * * @return void * * @since 10.6.0 * * @internal */ public function handle_woocommerce_before_delete_order( $order_id, $order ): void { $this->invalidate( (int) $order_id ); $this->invalidate_orders_list(); } /** * Handle the woocommerce_trash_order hook. * * @param int $order_id The order ID. * * @return void * * @since 10.6.0 * * @internal */ public function handle_woocommerce_trash_order( $order_id ): void { $this->invalidate( (int) $order_id ); $this->invalidate_orders_list(); } /** * Handle the woocommerce_untrash_order hook. * * @param int $order_id The order ID. * @param string $previous_status The previous order status before trashing. * * @return void * * @since 10.6.0 * * @internal */ public function handle_woocommerce_untrash_order( $order_id, $previous_status ): void { $this->invalidate( (int) $order_id ); $this->invalidate_orders_list(); } /** * Handle the woocommerce_order_status_changed hook. * * Status changes affect which orders appear in status-filtered collection endpoints, * so we always invalidate the orders list. * * @param int $order_id The order ID. * @param string $from_status The old status. * @param string $to_status The new status. * @param \WC_Order $order The order object. * * @return void * * @since 10.6.0 * * @internal */ public function handle_woocommerce_order_status_changed( $order_id, $from_status, $to_status, $order ): void { $this->invalidate( (int) $order_id ); $this->invalidate_orders_list(); } /** * Handle the woocommerce_order_refunded hook. * * @param int $order_id The parent order ID. * @param int $refund_id The refund ID. * * @return void * * @since 10.6.0 * * @internal */ public function handle_woocommerce_order_refunded( $order_id, $refund_id ): void { $order_id = (int) $order_id; $refund_id = (int) $refund_id; $this->invalidate( $order_id ); $this->invalidate_refund( $refund_id ); $this->invalidate_order_refunds_list( $order_id ); $this->invalidate_refunds_list(); } /** * Handle the woocommerce_refund_deleted hook. * * @param int $refund_id The refund ID. * @param int $order_id The parent order ID. * * @return void * * @since 10.6.0 * * @internal */ public function handle_woocommerce_refund_deleted( $refund_id, $order_id ): void { $order_id = (int) $order_id; $refund_id = (int) $refund_id; $this->invalidate( $order_id ); $this->invalidate_refund( $refund_id ); $this->invalidate_order_refunds_list( $order_id ); $this->invalidate_refunds_list(); } // phpcs:enable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed /** * Invalidate an order version string. * * @param int $order_id The order ID. * * @return void * * @since 10.6.0 */ public function invalidate( int $order_id ): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( "order_{$order_id}" ); } /** * Invalidate a refund version string. * * @param int $refund_id The refund ID. * * @return void * * @since 10.6.0 */ public function invalidate_refund( int $refund_id ): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( "refund_{$refund_id}" ); } /** * Invalidate the orders list version string. * * This should be called when orders are created, deleted, change status, * or change customer, as these operations affect collection/list endpoints. * * @return void */ private function invalidate_orders_list(): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( 'list_orders' ); } /** * Invalidate the refunds list version string. * * This should be called when refunds are created or deleted, * as these operations affect the /refunds collection endpoint. * * @return void */ private function invalidate_refunds_list(): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( 'list_refunds' ); } /** * Invalidate the refunds list version string for a specific order. * * This should be called when refunds are created or deleted for an order, * as these operations affect the /orders/{id}/refunds collection endpoint. * * @param int $order_id The parent order ID. * * @return void */ private function invalidate_order_refunds_list( int $order_id ): void { if ( $order_id > 0 ) { wc_get_container()->get( VersionStringGenerator::class )->delete_version( "list_order_refunds_{$order_id}" ); } } }