7

I used to have the following function working to change to Related Products text in Woocommerce.

function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related Products' :
            $translated_text = __( 'Related Books', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

It always worked perfectly, but as of Woocommerce version 3.0 or so, this function no longer works.

How should I fix this in order to make it working in the version 3.0 and up?

3 Answers 3

14

There is now a filter for that. Name is "woocommerce_product_related_products_heading"

So you can add a little snippet in your own theme functions.php file like :

add_filter('woocommerce_product_related_products_heading',function(){

   return 'My Custom nice related title';

});
11

Try this, it's working with me

add_filter(  'gettext',  'wps_translate_words_array'  );
add_filter(  'ngettext',  'wps_translate_words_array'  );
function wps_translate_words_array( $translated ) {
     $words = array(
                // 'word to translate' = > 'translation'
               'Related Products' => 'Check out these related products',  
     );
     $translated = str_ireplace(  array_keys($words),  $words,  $translated );
     return $translated;
}
0
6

A simple alternative way

Overriding Woocommerce templates via your theme for the single-product/related.php template file, where you can rename it directly from:

<h2><?php esc_html_e( 'Related products', 'woocommerce' ); ?></h2>

To:

<h2><?php esc_html_e( 'Related Books', 'woocommerce' ); ?></h2>
2
  • not the best idea, if you receive updates
    – Mimouni
    Commented Jul 19, 2023 at 15:44
  • @Mimouni You need to have a child theme as it's recommended by WooCommerce and WordPress. Then you do not lose anything on any updates. Commented Jul 19, 2023 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.