Home > Blockchain >  Custom text for WooCommerce Payments gateway title
Custom text for WooCommerce Payments gateway title

Time:03-29

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?

CodePudding user response:

Go to woocommerce-> settings -> payments, click "set up" on your payment "Popular payment methods", and there you will see Title input field. enter image description here

CodePudding user response:

add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title', 25, 2 );

function change_payment_gateway_title( $title, $gateway_id ){
    
    if( 'gateway_id' === $gateway_id ) {
        $title = 'By Cash or Credit Card on delivery';
    }

    return $title;
}

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.

  • Related