Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I translate Text Elements in Woocommerce checkout page?

I'm trying to replace missing translations in the Checkout section in WooCommerce with the following code in functions.php.

The code doesn't work with the check that the translation is from the WooCommerce plugin:

function my_custom_text($translation, $text, $domain) {
    if ($domain == 'woocommerce' && $text == 'Apply') {
        return 'New Button Text';
    }
    return $translation;
}
add_filter('gettext', 'my_custom_text', 10, 3);

If I leave out the check for Text from WooCommerce plugin the code works:

function my_custom_text($translation, $text, $domain) {
    if ($text == 'Apply') {
        return 'New Button Text';
    }
    return $translation;
}
add_filter('gettext', 'my_custom_text', 10, 3);

Do I need the check that the translation is from the WooCommerce plugin?

Is this type of translation actually best practice? Or does security or performance suffer as a result?

like image 831
Penumbra Avatar asked Oct 29 '25 10:10

Penumbra


1 Answers

Your 2nd function is just fine, you don't really need to check the text domain and this isn't an issue at all.

Now, to make that function only active in checkout, use the following:

add_action('woocommerce_checkout_init', function(){
    add_filter('gettext', 'my_custom_checkout_text', 10, 3);
});

function my_custom_checkout_text($translation, $text, $domain) {
    if ($text == 'Apply') {
        return 'New Button Text';
    }
    return $translation;
}

You could also use the conditional function is_checkout(), but it's less efficient.

like image 142
LoicTheAztec Avatar answered Nov 01 '25 14:11

LoicTheAztec