Skip to content

Instantly share code, notes, and snippets.

@dartrax
Last active June 9, 2024 18:27
Show Gist options
  • Save dartrax/85b3ab77b8210e92cc50e1b68de83c73 to your computer and use it in GitHub Desktop.
Save dartrax/85b3ab77b8210e92cc50e1b68de83c73 to your computer and use it in GitHub Desktop.
Translate WooCommerce mails into order language with TranslatePress before sending. Note: As of TranslatePress Version 2.3.5, this functionality was implemented natively by TranslatePress, making this Code obsolete. Read more here: https://translatepress.com/docs/translating-woocommerce-emails/
// 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;
}
// Override 'plugin_locale'-function so Woocommerce won't use the admin profile language
function dartrax_force_woo_locale($locale, $plugin) {
global $TRP_LANGUAGE;
return $plugin == 'woocommerce' ? $TRP_LANGUAGE : $locale;
}
// 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 );
// Woocommerce uses the admin profile language instead of the side language. Override with the desired language
add_filter('plugin_locale', 'dartrax_force_woo_locale', 10, 2);
// (re-)load plugin's textdomain with order locale
load_default_textdomain( $language );
$woocommerce->load_plugin_textdomain();
$wp_locale = new \WP_Locale();
// Clean up
remove_filter('plugin_locale', 'dartrax_force_woo_locale', 10);
}
}
@dartrax
Copy link
Author

dartrax commented Jul 9, 2022

Hi @natachao,
Password reset and new account mails should work out of the box with Translatepress, my Code here isn't needed for that. Are you sure you have the language packs installed for the languages you use? If you want to customize the mail's content, you could use conditional shortcodes (see https://wordpress.org/support/topic/multi-language-emails-3/).

@natachao
Copy link

natachao commented Jul 10, 2022 via email

@jverne33
Copy link

jverne33 commented Aug 5, 2022

Thanks for writing this code! I just implemented it on my site and it works smoothly.

@razvan-translatepress
Copy link

Hi,

I'm Razvan, developer at TranslatePress. We implemented this feature in TranslatePress - Multilingual plugin and it's available in the latest free version.
Check out the documentation on it.

Thanks to everyone that contributed to this custom code and thread. It helped our development team implement the feature.

For the projects that already have this custom code, it should be safe to remove it and update TranslatePress. For orders already placed that were not yet marked as finished, default language will be used in emails. You can change wp_postmeta meta_key order_language to trp_language in order for the current unfinished orders to have further emails sent in the checkout language.

Best regards,
Razvan

@dartrax
Copy link
Author

dartrax commented Sep 2, 2022

Hi @razvan-translatepress,
thanks for your reply - it is very nice that you showed up here to say thank you 👍 I'm glad that the code helped to implement this important feature. I've not updated TranslatePress yet, but I certainly will and test this!
Your explanation on how to migrate is also very appreciated.

Are there any restrictions regarding the minimum WooCommerce Version supporting this feature? I've read that you needed WooCommerce to implement some changes in their code in order to make this work somewhere else - is this still true?

@razvan-translatepress
Copy link

As written in the documentation I linked earlier, these are the version requirements:
TranslatePress - Multilingual, version 2.3.5+
WooCommerce, version 6.8+

@mystertaran
Copy link

Hello everyone. Is this work with Polylang for WooCommerce? I am asking because my shop have two languages: main Polish and second English. When customer order by choosing eng language the first e-mail is translated perfectly, but others like invocie or order aren’t.

@dartrax
Copy link
Author

dartrax commented Feb 26, 2024

Hi @mystertaran,
thanks for the question!
It is only for Translatepress - however, this is obsolete now, as Translatepress has this function built in since version 2.3.5!

@medchehida
Copy link

I do have the last version, still same issue, first order email in user's language, shipping statue emails are sent in the website default language.
Any clear instruction on how did you get it to work?

@dartrax
Copy link
Author

dartrax commented Jun 9, 2024

Hi @medchehida,
do you use Translatepress? The newer versions do have this feature already built in.

@medchehida
Copy link

@dartrax Yes i'm using Translatepress, the latest version
image

Tried your shortcode same issue.
The only way i found is to use Yaymail premium, but i'm looking for a free alternative.

@dartrax
Copy link
Author

dartrax commented Jun 9, 2024

Translatepress should do this natively since version 2.3.5 - please ask the Translatepress team for support in this matter!

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