diff --git a/plugins/woocommerce/changelog/fix-36537 b/plugins/woocommerce/changelog/fix-36537 new file mode 100644 index 00000000000..def07131193 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-36537 @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Add filter `woocommerce_redirect_order_location` for consistency with posts and HPOS. diff --git a/plugins/woocommerce/src/Internal/Admin/Orders/Edit.php b/plugins/woocommerce/src/Internal/Admin/Orders/Edit.php index 1d902d046f5..5150f9fd391 100644 --- a/plugins/woocommerce/src/Internal/Admin/Orders/Edit.php +++ b/plugins/woocommerce/src/Internal/Admin/Orders/Edit.php @@ -55,6 +55,13 @@ class Edit { */ private $message; + /** + * Controller for orders page. Used to determine redirection URLs. + * + * @var PageController + */ + private $orders_page_controller; + /** * Hooks all meta-boxes for order edit page. This is static since this may be called by post edit form rendering. * @@ -104,6 +111,18 @@ class Edit { wp_enqueue_script( 'post' ); // Ensure existing JS libraries are still available for backward compat. } + /** + * Returns the PageController for this edit form. This method is protected to allow child classes to overwrite the PageController object and return custom links. + * + * @return PageController PageController object. + */ + protected function get_page_controller() { + if ( ! isset( $this->orders_page_controller ) ) { + $this->orders_page_controller = wc_get_container()->get( PageController::class ); + } + return $this->orders_page_controller; + } + /** * Setup hooks, actions and variables needed to render order edit page. * @@ -215,9 +234,37 @@ class Edit { // Order updated message. $this->message = 1; - // Refresh the order from DB. - $this->order = wc_get_order( $this->order->get_id() ); - $theorder = $this->order; + $this->redirect_order( $this->order ); + } + + /** + * Helper method to redirect to order edit page. + * + * @param \WC_Order $order Order object. + */ + private function redirect_order( \WC_Order $order ) { + $redirect_to = $this->get_page_controller()->get_edit_url( $order->get_id() ); + if ( isset( $this->message ) ) { + $redirect_to = add_query_arg( 'message', $this->message, $redirect_to ); + } + wp_safe_redirect( + /** + * Filter the URL used to redirect after an order is updated. Similar to the WP post's `redirect_post_location` filter. + * + * @param string $redirect_to The redirect destination URL. + * @param int $order_id The order ID. + * @param \WC_Order $order The order object. + * + * @since 8.0.0 + */ + apply_filters( + 'woocommerce_redirect_order_location', + $redirect_to, + $order->get_id(), + $order + ) + ); + exit; } /** @@ -279,10 +326,10 @@ class Edit { private function render_wrapper_start( $notice = '', $message = '' ) { $post_type = get_post_type_object( $this->order->get_type() ); - $edit_page_url = wc_get_container()->get( PageController::class )->get_edit_url( $this->order->get_id() ); + $edit_page_url = $this->get_page_controller()->get_edit_url( $this->order->get_id() ); $form_action = 'edit_order'; $referer = wp_get_referer(); - $new_page_url = wc_get_container()->get( PageController::class )->get_new_page_url( $this->order->get_type() ); + $new_page_url = $this->get_page_controller()->get_new_page_url( $this->order->get_type() ); ?>