diff --git a/changelog/2024-07-11-21-06-01-653423 b/changelog/2024-07-11-21-06-01-653423 new file mode 100644 index 00000000000..81ed2da10c2 --- /dev/null +++ b/changelog/2024-07-11-21-06-01-653423 @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Fix payment method title for Express Checkout Element orders. diff --git a/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php index c4ba8cff02b..e912ca8d262 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php @@ -112,7 +112,7 @@ public function ajax_pay_for_order() { throw new Exception( __( 'This order does not require payment!', 'woocommerce-payments' ) ); } - $this->add_order_meta( $order_id ); + $this->express_checkout_button_helper->add_order_payment_method_title( $order_id ); // Load the gateway. $all_gateways = WC()->payment_gateways->get_available_payment_gateways(); @@ -431,35 +431,4 @@ public function ajax_empty_cart() { wp_send_json( [ 'result' => 'success' ] ); } - - /** - * Add needed order meta - * - * @param integer $order_id The order ID. - * - * @return void - */ - public function add_order_meta( $order_id ) { - if ( empty( $_POST['express_payment_type'] ) || ! isset( $_POST['payment_method'] ) || 'woocommerce_payments' !== $_POST['payment_method'] ) { // phpcs:ignore WordPress.Security.NonceVerification - return; - } - - $order = wc_get_order( $order_id ); - - $express_payment_type = wc_clean( wp_unslash( $_POST['express_payment_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification - - $express_payment_titles = [ - 'apple_pay' => 'Apple Pay', - 'google_pay' => 'Google Pay', - ]; - - $suffix = apply_filters( 'wcpay_payment_request_payment_method_title_suffix', 'WooPayments' ); - if ( ! empty( $suffix ) ) { - $suffix = " ($suffix)"; - } - - $payment_method_title = isset( $express_payment_titles[ $express_payment_type ] ) ? $express_payment_titles[ $express_payment_type ] : 'Express Payment'; - $order->set_payment_method_title( $payment_method_title . $suffix ); - $order->save(); - } } diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php index 6a8d66d1bd8..9c62be93c45 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php @@ -94,6 +94,8 @@ public function init() { add_filter( 'woocommerce_registration_redirect', [ $this, 'get_login_redirect_url' ], 10, 3 ); add_action( 'wp_enqueue_scripts', [ $this, 'scripts' ] ); add_action( 'before_woocommerce_pay_form', [ $this, 'display_pay_for_order_page_html' ], 1 ); + add_filter( 'woocommerce_gateway_title', [ $this, 'filter_gateway_title' ], 10, 2 ); + add_action( 'woocommerce_checkout_order_processed', [ $this->express_checkout_helper, 'add_order_payment_method_title' ], 10, 2 ); $this->express_checkout_ajax_handler->init(); } @@ -402,4 +404,31 @@ public function get_login_redirect_url( $redirect ) { return $url; } + + /** + * Filters the gateway title to reflect the button type used. + * + * @param string $title Gateway title. + * @param string $id Gateway ID. + */ + public function filter_gateway_title( $title, $id ) { + if ( 'woocommerce_payments' !== $id || ! is_admin() ) { + return $title; + } + + $order = $this->express_checkout_helper->get_current_order(); + $method_title = is_object( $order ) ? $order->get_payment_method_title() : ''; + + if ( ! empty( $method_title ) ) { + if ( + strpos( $method_title, 'Apple Pay' ) === 0 + || strpos( $method_title, 'Google Pay' ) === 0 + || strpos( $method_title, 'Payment Request' ) === 0 // Legacy PRB title. + ) { + return $method_title; + } + } + + return $title; + } } diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php b/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php index 8f9d0d7ac9c..5d52fa94691 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php @@ -329,6 +329,26 @@ public function get_product() { return null; } + /** + * Used to get the order in admin edit page. + * + * @return WC_Order|WC_Order_Refund|bool + */ + public function get_current_order() { + global $theorder; + global $post; + + if ( is_object( $theorder ) ) { + return $theorder; + } + + if ( is_object( $post ) ) { + return wc_get_order( $post->ID ); + } + + return false; + } + /** * Returns true if the provided WC_Product is a subscription, false otherwise. * @@ -1033,6 +1053,39 @@ public function update_shipping_method( $shipping_methods ) { WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); } + /** + * Add express checkout payment method title to the order. + * + * @param integer $order_id The order ID. + * + * @return void + */ + public function add_order_payment_method_title( $order_id ) { + if ( empty( $_POST['express_payment_type'] ) || ! isset( $_POST['payment_method'] ) || 'woocommerce_payments' !== $_POST['payment_method'] ) { // phpcs:ignore WordPress.Security.NonceVerification + return; + } + + $express_payment_type = wc_clean( wp_unslash( $_POST['express_payment_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification + $express_payment_titles = [ + 'apple_pay' => 'Apple Pay', + 'google_pay' => 'Google Pay', + ]; + $payment_method_title = $express_payment_titles[ $express_payment_type ] ?? false; + + if ( ! $payment_method_title ) { + return; + } + + $suffix = apply_filters( 'wcpay_payment_request_payment_method_title_suffix', 'WooPayments' ); + if ( ! empty( $suffix ) ) { + $suffix = " ($suffix)"; + } + + $order = wc_get_order( $order_id ); + $order->set_payment_method_title( $payment_method_title . $suffix ); + $order->save(); + } + /** * Calculate and set shipping method. *