User
Forum Replies Created
-
Forum: Plugins
In reply to: [Polylang] First email is translated, next are not.Hi @mystertaran, I have solved this issue.
I retrieve the language in which the order was placed in Polylang (pll_get_post_language($order->get_id(), 'slug')) and then I temporary switch the locale (switch_to_locale($order_locale)) so that the email is send in the correct language. It’s similar to the approach described in the Polylang documentation.
Please find the code below – the // Check if the object is a Shipment or Invoice from Germanized for WooCommerce part can be removed as it’s specific for the Germanized for WooCommerce plugin.<?php
// WooCommerce - Order email notifications language set based on order using Polylang
// Last update: 2024-08-29
// To prevent issues where simultaneous purchases by different users in different languages might result in email notifications being sent in the wrong language, the code avoids using global variables. Instead, it handles locale switching in an isolated manner for each request.
if (class_exists('WooCommerce') && WC() && class_exists('Polylang')) {
class WC_Email_Locale_Handler
{
public function __construct()
{
// Hook into the email header to switch locale before email content
add_action($hook_name = 'woocommerce_email_header', $callback = [$this, 'set_email_locale_based_on_order'], $priority = 10, $accepted_args = 2);
// Hook into the email footer to restore the original locale after email content
add_action($hook_name = 'woocommerce_email_footer', $callback = [$this, 'restore_email_locale'], $priority = 10, $accepted_args = 1);
// Hook into resending emails to handle locale switching
add_action($hook_name = 'woocommerce_before_resend_order_emails', $callback = [$this, 'set_locale_before_resending_email'], $priority = 10, $accepted_args = 1);
}
public function set_email_locale_based_on_order($email_heading, $email)
{
if (isset($email->object)) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($email->object);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
public function restore_email_locale()
{
restore_previous_locale();
}
public function set_locale_before_resending_email($order)
{
if ($order) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($order);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
private function map_language_to_locale($language_slug)
{
// Define a mapping of language slugs to locale codes
$mapping = array(
'de' => 'de_DE',
'en' => 'en_US',
'pt' => 'pt_BR',
// Add more languages as needed
);
return isset($mapping[$language_slug]) ? $mapping[$language_slug] : false;
}
private function get_locale_from_object($object)
{
$order = null;
// Check if the object is a WC_Order
if (is_a($object, 'WC_Order')) {
$order = $object;
}
// Check if the object is a Shipment or Invoice from Germanized for WooCommerce
elseif (is_a($object, 'Vendidero\Germanized\Shipments\Shipment') || is_a($object, 'Vendidero\StoreaBill\Invoice\Invoice')) {
$order = $object->get_order();
}
if ($order && function_exists('pll_get_post_language')) {
// Get the language slug of the order
$order_language = pll_get_post_language($order->get_id(), 'slug');
// Map the language slug to a locale code
return $this->map_language_to_locale($order_language);
}
return null;
}
}
// Initialize the handler class
new WC_Email_Locale_Handler();
}Forum: Plugins
In reply to: [Germanized for WooCommerce] Email Notifications in Different LanguagesHi Dennis, thanks – I will share the final code below in case anyone else needs it.
Thank you for your support.<?php
// WooCommerce - Order email notifications language set based on order using Polylang
// Last update: 2024-08-29
// To prevent issues where simultaneous purchases by different users in different languages might result in email notifications being sent in the wrong language, the code avoids using global variables. Instead, it handles locale switching in an isolated manner for each request.
if (class_exists('WooCommerce') && WC() && class_exists('Polylang')) {
class WC_Email_Locale_Handler
{
public function __construct()
{
// Hook into the email header to switch locale before email content
add_action($hook_name = 'woocommerce_email_header', $callback = [$this, 'set_email_locale_based_on_order'], $priority = 10, $accepted_args = 2);
// Hook into the email footer to restore the original locale after email content
add_action($hook_name = 'woocommerce_email_footer', $callback = [$this, 'restore_email_locale'], $priority = 10, $accepted_args = 1);
// Hook into resending emails to handle locale switching
add_action($hook_name = 'woocommerce_before_resend_order_emails', $callback = [$this, 'set_locale_before_resending_email'], $priority = 10, $accepted_args = 1);
}
public function set_email_locale_based_on_order($email_heading, $email)
{
if (isset($email->object)) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($email->object);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
public function restore_email_locale()
{
restore_previous_locale();
}
public function set_locale_before_resending_email($order)
{
if ($order) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($order);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
private function map_language_to_locale($language_slug)
{
// Define a mapping of language slugs to locale codes
$mapping = array(
'de' => 'de_DE',
'en' => 'en_US',
'pt' => 'pt_BR',
// Add more languages as needed
);
return isset($mapping[$language_slug]) ? $mapping[$language_slug] : false;
}
private function get_locale_from_object($object)
{
$order = null;
// Check if the object is a WC_Order
if (is_a($object, 'WC_Order')) {
$order = $object;
}
// Check if the object is a Shipment or Invoice from Germanized for WooCommerce
elseif (is_a($object, 'Vendidero\Germanized\Shipments\Shipment') || is_a($object, 'Vendidero\StoreaBill\Invoice\Invoice')) {
$order = $object->get_order();
}
if ($order && function_exists('pll_get_post_language')) {
// Get the language slug of the order
$order_language = pll_get_post_language($order->get_id(), 'slug');
// Map the language slug to a locale code
return $this->map_language_to_locale($order_language);
}
return null;
}
}
// Initialize the handler class
new WC_Email_Locale_Handler();
}Forum: Plugins
In reply to: [Germanized for WooCommerce] Email Notifications in Different LanguagesThanks for the quick answer! I have just added “Vendidero\StoreaBill\Invoice\Invoice” to the code and it worked as expected! The invoice notification email was sent in the language that the customer ordered.
Are there any additional objects besides shipping object (“Vendidero\Germanized\Shipments\Shipment“) and invoice object (“Vendidero\StoreaBill\Invoice\Invoice“) I should consider on my code?
Also, is there any method you recommend me to use to check whether Germanized plugin is activated? I am currently using:if (class_exists('Vendidero\Germanized\Packages').
Thanks!<?php
// WooCommerce - Order email notifications language set based on order using Polylang
// Last update: 2024-08-29
if (class_exists('WooCommerce') && WC() && class_exists('Polylang')) {
class WC_Email_Locale_Handler
{
public function __construct()
{
// Hook into the email header to switch locale before email content
add_action($hook_name = 'woocommerce_email_header', $callback = [$this, 'set_email_locale_based_on_order'], $priority = 10, $accepted_args = 2);
// Hook into the email footer to restore the original locale after email content
add_action($hook_name = 'woocommerce_email_footer', $callback = [$this, 'restore_email_locale'], $priority = 10, $accepted_args = 1);
// Hook into resending emails to handle locale switching
add_action($hook_name = 'woocommerce_before_resend_order_emails', $callback = [$this, 'set_locale_before_resending_email'], $priority = 10, $accepted_args = 1);
}
public function set_email_locale_based_on_order($email_heading, $email)
{
if (isset($email->object)) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($email->object);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
public function restore_email_locale()
{
restore_previous_locale();
}
public function set_locale_before_resending_email($order)
{
if ($order) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($order);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
private function map_language_to_locale($language_slug)
{
// Define a mapping of language slugs to locale codes
$mapping = array(
'de' => 'de_DE',
'en' => 'en_US',
'pt' => 'pt_BR',
// Add more languages as needed
);
return isset($mapping[$language_slug]) ? $mapping[$language_slug] : false;
}
private function get_locale_from_object($object)
{
$order = null;
// Check if the object is a WC_Order
if (is_a($object, 'WC_Order')) {
$order = $object;
}
// Check if the object is a Shipment or Invoice from Germanized
elseif (class_exists('Vendidero\Germanized\Packages') && (is_a($object, 'Vendidero\Germanized\Shipments\Shipment') || is_a($object, 'Vendidero\StoreaBill\Invoice\Invoice'))) {
$order = $object->get_order();
}
if ($order && function_exists('pll_get_post_language')) {
// Get the language slug of the order
$order_language = pll_get_post_language($order->get_id(), 'slug');
// Map the language slug to a locale code
return $this->map_language_to_locale($order_language);
}
return null;
}
}
// Initialize the handler class
new WC_Email_Locale_Handler();
}Forum: Plugins
In reply to: [Germanized for WooCommerce] Email Notifications in Different LanguagesHi Dennis,
Thank you for your answer. From here I understood that the shipment object is “Vendidero\Germanized\Shipments\Shipment“. Is that correct?
What would the invoice object be (I haven’t found this one in the documentation)?
I have updated the code and included a new function calledget_locale_from_object(). The code works for re-submitting “Send order details to customer” and “Resend order confirmation” email notifications.
From what I understood from your message, I should pass$email->objectand check whether it is ais_a($email->object, 'Vendidero\Germanized\Shipments\Shipment')object, if it is, I should get the$order = $email->object->get_order();. Is that right?
Thanks!<?php
// WooCommerce - Order email notifications language set based on order using Polylang
// Last update: 2024-08-29
if (class_exists('WooCommerce') && WC() && class_exists('Polylang')) {
class WC_Email_Locale_Handler
{
public function __construct()
{
// Hook into the email header to switch locale before email content
add_action($hook_name = 'woocommerce_email_header', $callback = [$this, 'set_email_locale_based_on_order'], $priority = 10, $accepted_args = 2);
// Hook into the email footer to restore the original locale after email content
add_action($hook_name = 'woocommerce_email_footer', $callback = [$this, 'restore_email_locale'], $priority = 10, $accepted_args = 1);
// Hook into resending emails to handle locale switching
add_action($hook_name = 'woocommerce_before_resend_order_emails', $callback = [$this, 'set_locale_before_resending_email'], $priority = 10, $accepted_args = 1);
}
public function set_email_locale_based_on_order($email_heading, $email)
{
if (isset($email->object)) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($email->object);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
public function restore_email_locale()
{
restore_previous_locale();
}
public function set_locale_before_resending_email($order)
{
if ($order) {
// Get the language locale of the order
$order_locale = $this->get_locale_from_object($order);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
private function map_language_to_locale($language_slug)
{
// Define a mapping of language slugs to locale codes
$mapping = array(
'de' => 'de_DE',
'en' => 'en_US',
'pt' => 'pt_BR',
// Add more languages as needed
);
return isset($mapping[$language_slug]) ? $mapping[$language_slug] : false;
}
private function get_locale_from_object($object)
{
$order = null;
// Check if the object is a WC_Order
if (is_a($object, 'WC_Order')) {
$order = $object;
}
// Check if the object is a Shipment from Germanized
elseif (is_a($object, 'Vendidero\Germanized\Shipments\Shipment')) {
$order = $object->get_order();
}
if ($order && function_exists('pll_get_post_language')) {
// Get the language slug of the order
$order_language = pll_get_post_language($order->get_id(), 'slug');
// Map the language slug to a locale code
return $this->map_language_to_locale($order_language);
}
return null;
}
}
// Initialize the handler class
new WC_Email_Locale_Handler();
}Forum: Plugins
In reply to: [Germanized for WooCommerce] Email Notifications in Different LanguagesHi Dennis,
Thank you for your reply. I investigated it further and was able to partially solve the problem.
I get the language in which the order was placed in Polylang ($order_language = pll_get_post_language($order->get_id(), 'slug')) and then I temporary switch the locale (switch_to_locale($order_locale)) so that the email is send in the correct language. It works when I manually click to submit the email.
Unfortunately it’s not working when I submit the invoice to the client (and I expect that it will also not work when I send the order shipped notification to the client – but this was yet not tested).
Maybe Germanized uses custom hooks or methods for sending emails? Could you support me here?
Thanks!<?php
// WooCommerce - Order email notifications language set based on order using Polylang
// Last update: 2024-08-28
// To prevent issues where simultaneous purchases by different users in different languages might result in email notifications being sent in the wrong language, the code avoids using global variables. Instead, it handles locale switching in an isolated manner for each request.
if (class_exists('WooCommerce') && WC() && class_exists('Polylang')) {
class WC_Email_Locale_Handler
{
public function __construct()
{
// Hook into the email header to switch locale before email content
add_action($hook_name = 'woocommerce_email_header', $callback = [$this, 'set_email_locale_based_on_order'], $priority = 10, $accepted_args = 2);
// Hook into the email footer to restore the original locale after email content
add_action($hook_name = 'woocommerce_email_footer', $callback = [$this, 'restore_email_locale'], $priority = 10, $accepted_args = 1);
// Hook into resending emails to handle locale switching
add_action($hook_name = 'woocommerce_before_resend_order_emails', $callback = [$this, 'set_locale_before_resending_email'], $priority = 10, $accepted_args = 1);
}
public function set_email_locale_based_on_order($email_heading, $email)
{
if (isset($email->object) && is_a($email->object, 'WC_Order')) {
$order = $email->object;
if (function_exists('pll_get_post_language')) {
// Get the language slug of the order
$order_language = pll_get_post_language($order->get_id(), 'slug');
// Map the language slug to a locale code
$order_locale = $this->map_language_to_locale($order_language);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
}
public function restore_email_locale()
{
restore_previous_locale();
}
public function set_locale_before_resending_email($order)
{
if (is_a($order, 'WC_Order')) {
if (function_exists('pll_get_post_language')) {
// Get the language slug of the order
$order_language = pll_get_post_language($order->get_id(), 'slug');
// Map the language slug to a locale code
$order_locale = $this->map_language_to_locale($order_language);
if ($order_locale) {
// Switch to the order's locale
switch_to_locale($order_locale);
}
}
}
}
private function map_language_to_locale($language_slug)
{
// Define a mapping of language slugs to locale codes
$mapping = array(
'de' => 'de_DE',
'en' => 'en_US',
'pt' => 'pt_BR',
// Add more languages as needed
);
return isset($mapping[$language_slug]) ? $mapping[$language_slug] : false;
}
}
// Initialize the handler class
new WC_Email_Locale_Handler();
}Forum: Plugins
In reply to: [Polylang] First email is translated, next are not.Hi @mystertaran, I tried to solve this issue today with the following code. Unfortunately it did not work.
// WooCommerce - Order email notifications language set based on order using Polylang
// Last update: 2024-08-27
if (class_exists('WooCommerce') && WC() && class_exists('Polylang')) {
add_action($hook_name = 'woocommerce_email_header', $callback = 'set_email_language_based_on_order', $priority = 10, $accepted_args = 2);
function set_email_language_based_on_order($email_heading, $email)
{
// Ensure it's an order-related email
if (isset($email->object) && is_a($email->object, 'WC_Order')) {
$order = $email->object;
$order_language = pll_get_post_language($order->get_id());
$current_language = pll_current_language(); // Get the current language
if ($order_language && $order_language !== $current_language) {
// Switch language if it's different from the current language
pll_switch_language($order_language);
}
}
}
add_action($hook_name = 'woocommerce_email_footer', $callback = 'restore_language_after_email', $priority = 10, $accepted_args = 1);
function restore_language_after_email($email)
{
// Restore the language to the default or previously active language
pll_restore_previous_language();
}
}Forum: Plugins
In reply to: [Polylang] First email is translated, next are not.Hi @mystertaran did you solve this issue?
Forum: Plugins
In reply to: [Germanized for WooCommerce] Email Notifications in Different LanguagesHi, thank you for your answer.
So if I understood it right, the Germanized email templates like the customer-shipment.php retrieves the translations fromwp-content\languages\plugins\woocommerce-germanized-de_DE.l10n.php.But how is the language retrieved from the user (or is it coming from the order)? Where does Germanized get this information?
Is there no other solution to solve the problem of emails for a same order being send into multiple languages?
Forum: Plugins
In reply to: [Germanized for WooCommerce] Email Notifications in Different LanguagesHi Dennis,
Thank your for your answer.
It would be great to have Polylang support in the future.
In the meantime, is there a way to find/change the language of the emails? Polylang has a documentation on how to translate strings (as described here). I did not find the words stated in the “Order shipped” and “Invoice (PDF)” email notification, like:
“Your order on XXXX has been shipped via DHL. Find details below for your reference”
“Invoice 2024 XXXX has been attached to this email. Find details below for your reference”
My questions:
– Are these templates coming from Germanized? If yes, where are they located (path)?
– Can I force the template to be send out in German? Given the lack of support to Polylang, I would prefer to send all these emails in German.
Thanks and kind regardsForum: Plugins
In reply to: [Complianz - GDPR/CCPA Cookie Consent] Google Maps Placeholder Not WorkingDear all,
Any news regarding this request?
Thanks and kind regardsThanks @rickdebilt for the update.
It’s unfortunate that Elementor hasn’t implemented this feature yet. It will most likely take a long time before they do, if at all.Hi @rickdebilt, for me this issue was never solved. I had to go back to the “legacy” local pick-up.
Hope Elementor’s Team solves this issue. I would like to migrate to the newer pick-up.Kind regards
Forum: Plugins
In reply to: [Complianz - GDPR/CCPA Cookie Consent] Google Maps Placeholder Not WorkingThanks!
Forum: Plugins
In reply to: [Complianz - GDPR/CCPA Cookie Consent] Google Maps Placeholder Not WorkingHi @antoiub, the map is in the end of the page. I have re-created the temporary URL to last 24 hours: https://www.temporary-url.com/4CB44
To see the map, initially accept the cookies.
If you re-enter the same page without accepting the cookies, you will see an empty space.
Please let me know if it worked this time and you could reproduce the issue.
Kind regards,
RobertoForum: Plugins
In reply to: [Complianz - GDPR/CCPA Cookie Consent] Google Maps Placeholder Not WorkingHi @antoiub, thanks for your response. Given that I am not able to edit the main thread and avoiding creating a new one with the exact same issue, I have activated a temporary URL. The URL will expire in 10 hours: https://www.temporary-url.com/0B280D
Thanks