phpwordpresswoocommercetranslationgettext

How to insert just few words in WooCommerce translated strings


Searching how to modify just a few words in the «new account» WooCommerce email to customer. I'm building WooCommerce shop in French, but it will be multilanguage.

I have to make this change in the theme for this new account mail and also for the «my account» page.

I Need to change the phrase in the new account mail to customer like this:
…vos commandes, créer ou changer votre…
where "créer ou" is inserted.

And in the «my account» page:
…ainsi que créer ou changer votre…
where "créer ou" is inserted.

Please take a look at the screenshots:

WooCommerce email notification - new account to customer:

enter image description here

WooCommerce wording in my account page:

enter image description here

i have tried to edit the specific woocommerce template in the theme code, but if change this words in english because the template is in english, its generating a email in english. So making changes there keeping the translation options is over my basic knowledge.


Solution

  • You can use the following, to insert a string into some translated strings:

    add_filter( 'gettext', 'customize_woocommerce_translated_strings', 10, 3 );
    function customize_woocommerce_translated_strings( $translated_text, $original_text, $domain ) {
        // Not in cart or checkout pages
        $targeted_string1 = 'changer votre mot de passe et les détails de votre compte';
        $targeted_string2 = 'changer votre mot de passe, et plus encore ici';
        $string_to_insert = 'créer ou ';
    
        if( is_account_page() && strpos( $translated_text, $targeted_string1) !== false ) {
            $translated_text = str_replace($targeted_string1, $string_to_insert.$targeted_string1, $translated_text);
        }
    
        if( ! is_admin() && strpos( $translated_text, $targeted_string2) !== false ) {
            $translated_text = str_replace($targeted_string2, $string_to_insert.$targeted_string2, $translated_text);
        }
    
        return $translated_text;
    }
    

    Code goes on functions.php file of your child theme (or in a plugin). Tested and works.

    On my account dashboard:

    enter image description here

    On New account email notification:

    enter image description here

    Note: You can also use Loco Translate plugin to change translated strings.