Translation string not working for WooCommerce product up-sells heading

50 views Asked by At

I recently used this bit of code to translate the WooCommerce bits that other translation plugin didn't pick up - however, this code doesn't seem to work on some strings like this one. Any idea why it doesn't work?

Loco translate didn't work on this one, and while the filter code was working on 95% of other untranslated strings, this one remained stubborn.

// Alter WooCommerce View Cart Text
add_filter( 'gettext', function( $translated_text ) {
    if ( 'You may also like…' === $translated_text ) {
        $translated_text = 'To moze Ci sie spodobac';
    }
    return $translated_text;
} );
1

There are 1 answers

0
LoicTheAztec On

You can use woocommerce_product_upsells_products_heading related available filter hook:

add_filter( 'woocommerce_product_upsells_products_heading', 'filter_product_upsells_products_heading' );
function filter_product_upsells_products_heading( $heading_text ) {
     return 'To moze Ci sie spodobac';
}

Or your revised code (where "…" character requires being replaced with …):

add_filter( 'gettext', 'change_product_upsells_heading_text', 10, 3 );
function change_product_upsells_heading_text( $translated_text, $original_text, $domain ) {
    if( 'You may also like…' === $original_text ) {
        $translated_text = 'To moze Ci sie spodobac';
    }
    return $translated_text;
}

Code goes on functions.php file of your child theme (or in a plugin). Both ways work.

enter image description here