Search code examples
phpwordpresswoocommercecheckoutgettext

How to change "Have a coupon?" text in Woocommerce checkout page


I'm trying to translate "Have a coupon?" text in Woocommerce checkout page using this code:

function custom_strings_translation( $translated_text, $text, $domain ) {
  switch ( $translated_text ) {
    case 'HAVE A COUPON?' : 
        $translated_text =  __( 'TIENES UN CUPÓN?', '__x__' );
        break;
}

  return $translated_text;
}
add_filter('gettext', 'custom_strings_translation', 20, 3);

But It doesn't work.

Please can someone tell me why?


Solution

  • It doesn't work because the text is not in capitals and you are not using the right variable.

    There is 2 ways to change this text (the second way seems the best):

    1) Using gettext filter hook this way:

    add_filter('gettext', 'custom_strings_translation', 20, 3);
    function custom_strings_translation( $translated_text, $text, $domain ) {
        if( $text == 'Have a coupon?' ){
            $translated_text =  __( 'Tienes un cupón?', $domain );
        }
        return $translated_text;
    }
    

    Code goes in function.php file of the active child theme (or active theme). Tested and works.


    2) Using woocommerce_checkout_coupon_message filter hook that will allow you more changes:

    add_filter( 'woocommerce_checkout_coupon_message', 'custom_checkout_coupon_message', 20, 1 );
    function custom_checkout_coupon_message( $notice ) {
        return __( 'Tienes un cupón?', 'woocommerce' ) . ' <a href="#" class="showcoupon">' . __( 'Haga clic aquí para ingresar su código', 'woocommerce' ) . '</a>'
    }
    

    Code goes in function.php file of the active child theme (or active theme). Tested and works.


    Related: