Skip to content

Instantly share code, notes, and snippets.

@egcmi
Forked from dartrax/functions.php
Created March 28, 2021 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egcmi/54cfd67931284e1527d70e5e409d0616 to your computer and use it in GitHub Desktop.
Save egcmi/54cfd67931284e1527d70e5e409d0616 to your computer and use it in GitHub Desktop.
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_new_customer_note_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() );
}
// 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();
}
}
@mylzmylo
Copy link

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

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