6

I found a solution online for this but it does not seem to work.

It says to edit the below file which I did a few days ago but somehow it is not working still.

/wp-content/plugins/woocommerce/templates/single-product/related.php

Which if I FTP to the server the file shows the below:

if ( $products->have_posts() ) : ?>

<div class="related products">

    <h2><?php _e('You may also like', 'woocommerce' ); ?></h2>

However the webpage still shows 'Related products' and not 'You may also like'

For some reason this is not taking place or being over ridden somewhere.

Any ideas?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mike Young
  • 321
  • 2
  • 4
  • 15

7 Answers7

30

i found this for the child functions.php: http://speakinginbytes.com/2013/10/gettext-filter-wordpress/

/**
 * Change text strings
 *
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 */
function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related Products' :
            $translated_text = __( 'Check out these related products', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

it works well here: https://mo-sound.com/en/shop/ball-speaker-classic-white/

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
user5217948
  • 301
  • 3
  • 3
9

The best way to override default templates is to copy the file to a folder named /woocommerce/single-product inside your current theme. Make changes to that file.

In general to override Woocommerce template files like

/wp-content/plugins/woocommerce/templates/<foldername>/<filename>

you copy the file to

/wp-content/<your-theme>/woocommerce/<foldername>/<filename>

RST
  • 3,899
  • 2
  • 20
  • 33
  • I suspect this is the answer. @user3710926 you should not edit WooCommerce core files directly. – helgatheviking Dec 02 '14 at 23:20
  • 1
    Just to clarify; that particular file should be copied over to your theme folder as `woocommerce/single-product/related.php`. You maintain the folder hierarchy found within the plugin template folder. – Dre Dec 03 '14 at 09:00
5

Here is user5217948's code with the needed case update from Frithir:

// CHANGE RELATED PRODUCTS TEXT
function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related products' :
            $translated_text = __( 'You may also like...', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

This code works with WooCommerce 3.3.1 and WordPress 4.9.4 (both circa Feb 2018).


.

MarkPraschan
  • 560
  • 6
  • 8
5

A friendly tip for those that have woocommerce/wordpress in another language

You would have to replace 'Related Products' to the text that is showing up on your language. In my case, related products translates to "productos relacionados"

Code to include in functions.php file

function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
    case 'Productos relacionados' :
        $translated_text = __( 'Completá el look', 'woocommerce' );
        break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
SHEESHRAM
  • 77
  • 8
Koni
  • 452
  • 1
  • 7
  • 19
  • Thanks man, you made my night. I was trying this switch with original US translation, because in function coding it's often about original U.S. terms. But here I also made it work by using my french traduction of the 'Related products' term. That being said, I had to change accented characters in non accented characters. The french traduction is "Produits apparentés". But in the switch part of the code : case 'Produits apparentés' won't work. Any Idea how to manage a statement in php with an accented string as the compared or switch condition? – BenDev Oct 08 '18 at 20:39
3

2020 Update

Instead of the suggested search-and-replace method, there is now a filter you can use, which is more accurate.

You add this snippet to your functions.php file, in your child theme.

/*-------------- Change related products text -------------*/
add_filter( 'woocommerce_product_related_products_heading', 'single_related_text' );

function single_related_text(){
    return __('These products might be if your interest as well!', 'woocommerce');
}
0

A little PHP snippet. Place PHP snippets at the bottom of your child theme functions.php file: wp-content/themes/mythemename/functions.php

add_filter( 'gettext', 'mythemename_translate_woocommerce_strings', 999, 3 );

function mythemename_translate_woocommerce_strings( $translated, $text, $domain ) {

    $translated = str_ireplace( 'text EN', 'text translated PT_BR', $translated );

    return $translated;
}
Fabiano Monteiro
  • 401
  • 6
  • 13
0

I have just used the following code successfully on one of my own sites. It needs to be placed in the functions.php in your child theme. Replace 'Your Custom Text Here' with the words you wish to use.

add_filter( 'gettext', 'wtd_related_products_text', 20, 3 );
function wtd_related_products_text( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Related products' :
$translated_text = __( 'Your Custom Text Here', 'woocommerce' );
break;
}
return $translated_text;
}
Gary
  • 3
  • 5