Hi @alexcozmoslabs,
Just chiming in, @antoska sorry for hijacking this thread… but this is exactly the same issue as reported !3 months ago! and the thread was simply closed.
Maybe if i can clarify things a bit @alexcozmoslabs can fix this for us! ??
The issue
There is a bug/misconception in the plugin that needs to be fixed for emails to be sent in the customers language if not a registered user.
In your public function trp_woo_setup_locale( $bool, $wc_email ) function
you rely on the recipient of the email to determine the language
$recipients = explode( ',', $wc_email->get_recipient() );
Line 145 in class-woocommerce-emails.php of Translatepress.
BUT at the time trp_woo_setup_locale
is called via the woocommerce_allow_switching_email_locale
hook the recipient in the email object is still NULL, which by the way causes a deprecation warning as i noted here https://www.ads-software.com/support/topic/php-error-when-using-woocommerce-and-the-order-email-is-send-php-8-2/
the woocommerce_allow_switching_email_locale
hook gets called via the setup_locale()
function in the individual woocommerce emails. Here the trigger function from class-wc-email-customer-processing-order.php as an example:
/**
* Trigger the sending of this email.
*
* @param int $order_id The order ID.
* @param WC_Order|false $order Order object.
*/
public function trigger( $order_id, $order = false ) {
$this->setup_locale();
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( $this->is_enabled() && $this->get_recipient() ) {
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
$this->restore_locale();
}
As you can clearly see the $this->recipient field of wc_email is initialized way past the setup_locale
function which calls woocommerce_allow_switching_email_locale
to which your trp_woo_setup_locale
function is hooked. So there is now way you can customize the emails for a guest customer when you rely on the $recipients field of the wc_email object. Which you do! The only way you can make it work like the structure is now is by getting the billing_email manually from the order object instead of relying on the wc_email object.
In short
This is translation flow is not working:
trigger
-> setup_locale
-> woocommerce_allow_switching_email_locale
-> trp_woo_setup_locale
-> the recipient is set up. cause trp_woo_setup_locale
relies on the recipient.
Even worse it will cause an error in php > 8.3….. explode(): Passing null to parameter #2 ($string) of type string
You need to come up with something clever to fix this or simply use the get_billing_email(); function of the order directly to deterime the language in trp_woo_setup_locale
.
I really hope this gets fixed soon so @antoska, all the others not even aware of the issue and me can finally send translated mails!
LET’S fix this, please!!!
Cheers,
Paul
PS: I haven’t found a git repo of yours but i would be happy to submit a pull-request directly if needed.