I need to use different language versions with separate currencies depending on tax rate because of my payment gateway limitations. I have configured multiple tax rates for different locations and wrote code to change default customer delivery country depending on language version of my site they use. I am however having issues with checkout, I need to force customers to switch language version if they choose delivery to non EU country. How can I do it while keeping customer session?
How can I programatically switch customer session to en-US translation of my site via WPML depending on country they select for delivery? Thank you
Ok i found a workaround, the simplest solution is to add a check after the order is finalized by buyer (proceed to payment button click), and show a native woocommerce error with request to manually switch language/currency. like this:
add_action('woocommerce_checkout_process', 'validate_non_eu_shipping_country');
function validate_non_eu_shipping_country() {
// Define EU countries
$eu_countries = ['AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK'];
// Get the shipping country from the checkout data
$shipping_country = WC()->customer->get_shipping_country();
$current_language = ICL_LANGUAGE_CODE;
// Define error message with WPML translation
$error_message = __('Non-EU orders need to be processed in USD, please click 🇺🇸 in menu', 'your-text-domain');
// Add inline CSS for bold and red color
$styled_message = '<span style="color: red; font-weight: bold;">' . $error_message . '</span>';
// Check if the shipping country is non-EU and the current language is not 'en-US'
if ($shipping_country && !in_array($shipping_country, $eu_countries) && $current_language !== 'en-US') {
// Save the cart contents for restoration after redirect
WC()->session->set('redirect_cart', WC()->cart->get_cart());
// Add error notice
wc_add_notice($styled_message, 'error');
}
}