-
-
Save dartrax/85b3ab77b8210e92cc50e1b68de83c73 to your computer and use it in GitHub Desktop.
// 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); | |
} | |
} |
@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/
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....
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.
Fix: Mail when a note to customer is sent wasn't translated
I'm in the same situation
If this code solves the problem of the translation of the emails?
let me know what to do, I need woocommerce to work with two languages after checkout
Hi @mylzmylo,
yes, this code solves this. You need to add it to the functions.php file of your theme.
I use it in production with woocommerce 5.6 and newest translatepress version.
If you use the plugin „germanized“, please note I have a special version of my code in another gist that also covers those additional mails from germanized.
Thanks this code solves my problem 👯♂️
Hello and thank you for the great work!
I use TranslatePress on several sites, but so far I have not had to use it with Woocommerce. I'm still thinking about using it and I have a few questions for those of you that using it. I usually use the YayMail - WooCommerce Email Customizer plugin to style my emails and here my question is can I continue to use this plugin and somehow style the emails for two languages?
Hi @dimitrow7,
thank you for your kind words!
As nobody of the followers of this gist seem to use YayMail yet, I've tried it in my testing environment.
I can report that it works. Whenever you define text within YayMail, you'll need to use the Translatepress Conditional Shortcode to store the text in the different languages you use, like this:
Fix: When the admin profile language did not match the side default language, instead of the order language the admin profile language was used for the translations covered by the woocommerce .mo/.po-files.
@dartrax Thanks for your time to test it!
Hi,
First of all thanks for a great work :)
I've got some problem and i wonder if anyone had similar.
On locahost everything is working fine. But after moving to hosting i facing an issue.
My page is made in two languages main en_GB and second pl_PL. When someone is ordering in PL first mail is in PL. But all other emails send during order processing are in EN.
Maybe someone know what could be the problem?
Hi @biedronjerzy,
thanks for your interest in this code! What you describe is the „default“ behavior of Translatepress without any modification involved, so this code here is not working for you. Now let‘s see why.
Can you please check if order_language is successfully stored with your test order? You should see it in the custom fields block of the order. Which value does it have?
the value is stored correctly. for orders in pl value is pl_PL
Okay! We should concentrate on one mail first and try to get this out in correct language. Let's take the "Your ... order #... has been cancelled" mail. In my WooCommerce Settings, there are two mails for this, one for the customer and one for myself, the site/shop administrator. Please be sure to look for the customers mail, because only that will be translated, not the one for the shop administrator.
First thing to check is if the locale gets correctly read from database and set for translatePress. Please set the subject for the customer's cancellation mail in WooCommerce Mail Settings to this value:
[trp_language language="pl_PL"]Polish[/trp_language][trp_language language="en_GB"]English[/trp_language]
Change the order status of the test customer's order to processing and to cancelled. Is the Subject of the customer's mail "Polish" or "English" (or empty)?
If it's "Polish", that means the translation by TranslatePress works, but native translation by .mo/.po-files doesn't. Next step is to check if the language files for polish are installed at all. You can switch the main language to polish, this should install any missing language files. Check the mails if they are now translated. Switch back the main language and check again if it works now.
problem solved.
after moving page to hosting there was no language pack for wocommerce. just english so it could not use pl templates because it wasn't there...
thanks for answet and helping with the problem :)
Hi, @dartrax thank you so much for putting together this code, it helped me a lot! :)
I'm a designer trying to cover everything, but I can only pretty much read PHP code and somewhat understand what it does.
Your code worked fine for the transaction emails but didn't work in case of reset passwords and new account emails. I believe this happened because the language in both cases is defined on other pages of the website rather than the Checkout. Is there any way I can tweak your code to also include these options or is it not possible?
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/).
Thanks for writing this code! I just implemented it on my site and it works smoothly.
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
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?
As written in the documentation I linked earlier, these are the version requirements:
TranslatePress - Multilingual, version 2.3.5+
WooCommerce, version 6.8+
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.
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!
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?
Hi @medchehida,
do you use Translatepress? The newer versions do have this feature already built in.
@dartrax Yes i'm using Translatepress, the latest version
Tried your shortcode same issue.
The only way i found is to use Yaymail premium, but i'm looking for a free alternative.
Translatepress should do this natively since version 2.3.5 - please ask the Translatepress team for support in this matter!
@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.