3

I'm simply trying to change the string "SKU" to show something else inside the single-product meta.php file. I'm using a child theme. The parent theme got a woocommerce.php file in the path /inc/compat/. Because of this, as far as I understood, I can't override by simply change the meta.php inside the theme.

So, I've tried to add this function inside woocommerce.php of the parent theme:

function translate_woocommerce($translation, $text, $domain) {
    if ($domain == 'woocommerce') {
        switch ($text) {
            case 'SKU':
                $translation = 'TEST';
                break;
            case 'SKU:':
                $translation = 'TEST';
                break;
        }
    }
    return $translation; }

add_filter('gettext', 'translate_woocommerce', 10, 3);

But it's not working. Could you please help me?

Thank you very much.

Just a little update: if I put the above code in functions.php of the parent or child theme, I can see that SKU string is changed inside Wordpress dashboard (please check the following image):

enter image description here

Andrea
  • 31
  • 2

1 Answers1

0

Try increasing the filter priority.

Therefore:

add_filter( 'gettext', 'translate_woocommerce', 9999, 3 );
function translate_woocommerce( $translation, $text, $domain ) {
    if ( $domain == 'woocommerce' ) {
        switch ( $text ) {
            case 'SKU':
                $translation = 'TEST';
                break;
            case 'SKU:':
                $translation = 'TEST';
                break;
        }
    }
    return $translation;
}

I tested your code and it works for me.

As reported in the filter documentation: https://developer.wordpress.org/reference/hooks/gettext/ using the switch with the $text variable you refer to untranslated text.

You may want to use $translation instead to refer to the translated text visible in the frontend / backend.

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32