'Changing currency on checkout with WooCommerce Multilingual Plugin
I'm trying to implement currency change on checkout with WooCommerce Multilingual Plugin. I need a country with COP and the rest of countries with USD. First I tried to use the wcml_client_currency hook.
function modify_client_currency( $currency ) {
$country = WC()->customer->get_billing_country();
if ($country === 'CO') {
return 'COP';
} else {
return 'USD';
}
}
add_filter( 'wcml_client_currency', 'modify_client_currency', 10, 1 );
If I start with Colombia selected, the currency is in COP, but if I change to another country, the currency stay on COP. I need to change the country again to see the change.
I tried another approach with the woocommerce_checkout_update_order_review and analyzing how the multilingual plugin works.
function action_woocommerce_checkout_update_order_review( $post_data ) {
// This part will change dynamically based on the country contained in $post_data
$currency = 'COP';
global $woocommerce_wpml;
$woocommerce_wpml->multi_currency->set_client_currency($currency);
global $woocommerce, $current_user;
if(empty($woocommerce->session->data) && empty($current_user->ID)){
$woocommerce->session->set_customer_session_cookie(true);
}
do_action('wcml_switch_currency', $currency );
WC()->cart->calculate_totals();
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 1);
With this approach I have exactly the same problem, I need to change the country twice to see the currency change.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|