I was trying to change the label of WooCommerce Payments from "Popular payment methods" to something else on the checkout page.
I added this snippet to functions.php of the child theme
add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title', 100, 2 );
function change_payment_gateway_title( $title, $payment_id ){
if( $payment_id === 'woocommerce_payments' ) {
$title = __("custom text", "woocommerce");
}
return $title;
}
and this
add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title' );
function change_payment_gateway_title( $payment_id ){
return str_replace( 'Popular payment methods', 'custom text', $payment_id );
}
They both work for a fraction of a second when the checkout page is still loading, but the title jumps back to the original text ("Popular payment methods") when the page has finished loading. They work without any problem for other payment methods though.
Is there anything wrong with my code?
2
Answers
What is $gateway_id and where to get it? I think the easiest way is to use this code snippet which adds a payment gateway ID column in WooCommerce settings.
The original text getting restored originates from the js variable "wcpay_config" that is created by woocommerce payments. It is contained in the key "checkoutTitle" and passed through wp_localize_script.
Because I didn’t find a way to hook into this function I used this answer implementing a subclass and your first solution combined, to achieve a full replacement:
Also take a look at this answer, if other plugins stop working.