Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Translate WooCommerce mails into order language with TranslatePress before sending
// Save the current language in post_meta when checkout is processed (used to identify correct Email language)
add_action('woocommerce_checkout_update_order_meta', 'dartrax_save_language_on_checkout', 10, 2 );
function dartrax_save_language_on_checkout( $order_id, $posted ) {
if( ! class_exists('TRP_Translate_Press') ) return '';
global $TRP_LANGUAGE;
update_post_meta( $order_id, 'order_language', $TRP_LANGUAGE );
}
// Woocommerce Shipment Mails
add_action( 'woocommerce_order_status_processing_to_cancelled_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_on-hold_to_cancelled_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_pending_to_on-hold_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_failed_to_on-hold_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_cancelled_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_failed_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_on-hold_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_pending_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_fully_refunded_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_partially_refunded_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_pending_to_failed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_on-hold_to_failed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_pending_to_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_failed_to_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
add_action( 'woocommerce_order_status_cancelled_to_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 );
function dartrax_prepare_locale_for_Mail_with_order_id( $order_id ) {
// Get language code that may be stored in post meta data
$lang_code = get_post_meta($order_id, 'order_language', true);
if( $lang_code == '' || ! class_exists('TRP_Translate_Press') ) return '';
// Set it as current active language for translatepress
global $TRP_LANGUAGE;
$TRP_LANGUAGE = $lang_code;
}
// Woocommerce Mails when resend by Admin
add_action( 'woocommerce_before_resend_order_emails','dartrax_prepare_locale_for_resend_Mails', 5, 2 );
function dartrax_prepare_locale_for_resend_Mails( $order, $mail_type ) {
if( $mail_type == 'customer_invoice' )
dartrax_prepare_locale_for_Mail_with_order_id( $order->get_id() );
}
// Woocommerce Note to customer Mail
add_action( 'woocommerce_new_customer_note_notification','dartrax_prepare_locale_for_note_Mails', 5, 1 );
function dartrax_prepare_locale_for_note_Mails( $note_and_order_id ) {
dartrax_prepare_locale_for_Mail_with_order_id( $note_and_order_id['order_id'] );
}
// Override Locale when WooCommerce sends an eMail
add_filter( 'woocommerce_email_setup_locale', function() {
if( ! class_exists('TRP_Translate_Press') ) return '';
// Override translatepress 'locale'-function because that does not work in Admin interface
add_filter('locale','dartrax_force_trp_locale', 99999 + 1);
// Switch text domains to load the correct .po/.mo-file based translations
global $TRP_LANGUAGE;
switch_text_domains( $TRP_LANGUAGE );
return false;
} );
add_filter( 'woocommerce_email_restore_locale', function() {
// Undo overriding of translatepress' 'locale'-function
remove_filter('locale','dartrax_force_trp_locale', 99999 + 1);
return true;
} );
// Override translatepress 'locale'-function because that does not deliver $TRP_LANGUAGE in Admin interface
function dartrax_force_trp_locale($locale) {
global $TRP_LANGUAGE;
return $TRP_LANGUAGE;
}
// Switch to another text domain. Inspired by https://gist.github.com/Jon007/5b90e78289899bc28e9c39c12ef56e60
function switch_text_domains( $language ) {
if ( class_exists( 'TRP_Translate_Press' ) && class_exists( 'WooCommerce' ) ) {
global $locale, $woocommerce, $woocommerce_germanized;
// unload plugin's textdomains
unload_textdomain( 'default' );
unload_textdomain( 'woocommerce' );
// set locale to order locale
$locale = apply_filters( 'locale', $language );
// (re-)load plugin's textdomain with order locale
load_default_textdomain( $language );
$woocommerce->load_plugin_textdomain();
$wp_locale = new \WP_Locale();
}
}
@egcmi

This comment has been minimized.

Copy link

@egcmi egcmi commented Mar 28, 2021

hi @dartrax,
there was an extra ) on line 68, so i forked this and removed it. i still couldn't get it to send the translated emails, but at least i don't get an error when i install it ;)
feel free to update your gist with the correct syntax https://gist.github.com/egcmi/54cfd67931284e1527d70e5e409d0616#file-functions-php-L68
HTH :)

@dartrax

This comment has been minimized.

Copy link
Owner Author

@dartrax dartrax commented Mar 28, 2021

Thanks for spotting this! I've corrected it.

@lenamtl

This comment has been minimized.

Copy link

@lenamtl lenamtl commented Apr 4, 2021

Hi,
thanks for the function, I'm not decided yet if I will use Translatepress but your fonction is a great improvment.

@dartrax

This comment has been minimized.

Copy link
Owner Author

@dartrax dartrax commented Apr 4, 2021

Thank you, @lenamtl ;-)

@lenamtl

This comment has been minimized.

Copy link

@lenamtl lenamtl commented Apr 9, 2021

Are you using the same field name as WPML to save the order language value?
I was thinking if I use the field name that would work maybe with all other plugins that may check this values, is this make sense?

You both seems to enjoy TP
I'm wondering how do you deal with wp-cron in url that give 404 error page?
What are your settings?

Thanks

@dartrax

This comment has been minimized.

Copy link
Owner Author

@dartrax dartrax commented Apr 9, 2021

Hi,
I don't know what field names WPML or other plugins use. But you are free to change the fieldname to your needs, just search and replace the 'order_language' field name. When you test with other plugins, be sure to also check if the format of the data is the same. TranslatePress uses values like "de_DE_formal" or "en_US". If it works, it makes sense ;)

I do not use wp-cron, I've disabled it a long time ago and use the servers real cron for that. I did not notice any problems.

@phil00907

This comment has been minimized.

Copy link

@phil00907 phil00907 commented Apr 12, 2021

Hello @dartrax, thank you for sharing this, your code really makes a difference when using TranslatePress.
And thank you @lenamtl for the insights.
WPML uses the following table: wpml_language
And the following values: de, it or gb
Do you think that there is a way to "convert" the values and make so TranslatePress compatible with plugins that send out emails and are only compatible with WPML right now?

@dartrax

This comment has been minimized.

Copy link
Owner Author

@dartrax dartrax commented Apr 12, 2021

Hi @phil00907, thanks for your kind words!
Just add a function that converts the values. Like this:

function dartrax_convert_locale(string $l): string {
    $mapping = ['de_DE_formal' => 'de',
                'en_US' => 'gb',
                'it_IT' => 'it'];
    $mapping = array_merge($mapping, array_flip($mapping));
    return $mapping[$l] ?? $l;
}

Now you can use that to convert when the locale is written to the database (replace/update the corresponding line in the gist)
update_post_meta( $order_id, 'order_language', dartrax_convert_locale($TRP_LANGUAGE) );
...and when it's read out again:
$lang_code = dartrax_convert_locale( get_post_meta($order_id, 'order_language', true) );

Note that I did not test that, I only tested the function alone. Please report any bugs.

@lenamtl

This comment has been minimized.

Copy link

@lenamtl lenamtl commented Jun 23, 2021

I have tried WPML and this was a nightmare, I'm uninstalling it today and give a new try with Translatepress + your custom function.
I was wondering are you using translatepress in production at the moment have you encounter other issue ?
Thanks

@phil00907

This comment has been minimized.

Copy link

@phil00907 phil00907 commented Jun 23, 2021

@lenamtl I have switched from WPML to TranslatePress recently, as I had a long list of issues related to WPML.
It has not been easy to switch, with lots of manual correction work, but if you are starting a new site I would definitely go for TranslatePress right away.
TranslatePress has other issues and the support is slower in answering (2-5 working days usually).
I had problems finding a proper plugin for searching through the products on WooCommerce, finally solved with FiboSearch Pro.
Basically I am having issues where a language information needs to be stored and then used later. For example when requesting reviews based on order information or wanting to notify customers when a product is back in stock. Most of these plugins support WPML only.
The huge advantage of TranslatePress compared to WPML on the other side is that you won't have different product pages (and IDs) for the different languages, which often creates synchronisation issues.

@dartrax

This comment has been minimized.

Copy link
Owner Author

@dartrax dartrax commented Jun 23, 2021

@lenamtl, I've tested Polylang, Falang and Translatepress in a testing environment before I finally decided to go with Translatepress. I intentionally did not test WPML because I do not like to have everything doubled and I've seen too many people having issues with it. I also did not took anything into account that saves translations on other servers like Weglot.

In speed comparison, both Translatepress and Falang have been very fast, Polylang was slower.
(TTFB base language german: Plain 1140ms, TP 1270ms, Fa 1310ms, PL 1490ms)
(TTFB translated page english: Plain 1020ms, TP 1300ms, Fa 1360ms, PL 1520ms)
I did the speed comparison locally on a xampp stack.

Also with Polylang, every Category, Tag, and so on will show up doubled in the admin UI, so I excluded that soon.
I really liked Falang and I would recommend you looking at it, too. Stéphane is very quick with the support.
Finally I decided to go with Translatepress because it was the only plugin that is able to translate the product-specific, non-global attributes in woocommerce. Also I had a better feeling with it because it's not a "one-man-show" (well that may not mean anything at the end of the day...)

I had no big issues with Translatepress. I had no incompatible plugins. I use the Flatsome theme, and while the Flatsome product search did work out of the box, the ajax live search did not. But it was quite easy for me to get it work, since both provide filter functions that I could use to connect the live search with translatepress. (@phil00907, fyi: While I was digging the code I noticed that Translatepress has built-in compatibility with "Advanced Woocommerce Search").
The biggest issue was with the translation of mails, and I needed quite alot time figuring out how both, Translatepress and Woocommerce work internally, and luckily I was able to find a solution for that. Translatepress support is not very helpful with such kind of things, most of the time they only apologize that it's not supported and that they won't implement it soon. May be that changes if you go premium with Translatepress. (In these cases, you would probably get faster and better support from Stéphane, as he is really motivated to develop Falang further)

Yes, I use Translatepress in production, and you can find my page here: https://schlauhaus.biz/

@lenamtl

This comment has been minimized.

Copy link

@lenamtl lenamtl commented Jun 25, 2021

Hi,

@dartrax
Thanks for detailled feedback

For Translatepress
About Ajax Live Search that is not working was it not working only with the translated verion or with both main language and translated language?

About Email translation does this function is enough or it required more custom code ?
I'm not using any other third extension that send email.

As the store need to go online now and cannot wait more it will be 1 language for now.
I will do more test wih Falang and Translatepress on a test version, I'm using Woodmart theme & WP Bakery
The only other solutions I see is multisite or 2 seperate installations.

@phil00907 yes I agre, what a mess....
I'm cleaning the DB right now, all the product image where duplicated (2 images visible in the media backend) because they where duplicated in DB and because they are linked to the same image these cannot be deleted from the backend because it remove the image for all.
I'm cleaning it manually using PHPMyAdmin....

@dartrax

This comment has been minimized.

Copy link
Owner Author

@dartrax dartrax commented Jun 29, 2021

Hi @lenamtl
the Ajax Live Search was not working with the translated version, because it would not search the translated version of products without an additional hook. The default language continued to work.

For Email translation of woocommerce mails, this code should be enough.

@dartrax

This comment has been minimized.

Copy link
Owner Author

@dartrax dartrax commented Jul 13, 2021

Fix: Mail when a note to customer is sent wasn't translated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment