Skip to content

Commit

Permalink
Fix payment method title for Express Checkout Element orders (#9090)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo authored Jul 12, 2024
1 parent 93eaed1 commit 76d2cd5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 32 deletions.
4 changes: 4 additions & 0 deletions changelog/2024-07-11-21-06-01-653423
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Fix payment method title for Express Checkout Element orders.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 76d2cd5

Please sign in to comment.