Currency Selection and Render Based On Cookie Value
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page pleaseIf you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.
Quote from prince on January 8, 2024, 07:31Hello Team,
I hope this message finds you well. I am utilizing the pro version of this plugin and has successfully implemented a currency switcher on the checkout page using the following code placed within the
functions.phpfile. The setup is functioning seamlessly on the/checkout/page:// Add a custom select field in checkout
add_action('woocommerce_before_checkout_billing_form', 'add_custom_checkout_select_field');
function add_custom_checkout_select_field( $checkout ) {
// Here set in the function your product category term ID, slugs, names or array
if ( ! has_product_category_in_cart( 'concierge' ) && shortcode_exists( 'woocs' ) ) {
echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
echo '<div class="own">' . do_shortcode('[woocs]') . '</div>';
}
}// Custom Checkout fields validation
add_action('woocommerce_checkout_process', 'custom_checkout_select_field_validation');
function custom_checkout_select_field_validation() {
if ( isset($_POST['payopt']) && empty($_POST['payopt']) )
wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
}// Reset currency selector if it's not checkout
add_filter('wp_head',function(){
if(!is_checkout()){
global $WOOCS;
$WOOCS->reset_currency();
}
});However, the website is not a standard WooCommerce setup. The single product page has been customized, incorporating a third-party gadget that dynamically updates prices akin to the hotel industry. Upon adding a product to the cart, it defaults to USD due to WooCommerce settings, whereas their gadget provides a currency switcher functionality.
To address this, I’ve implemented cookies with the name “Usr5electedCurr3ncy” to store the selected currency from third-party gadget. The challenge arises when attempting to read this cookie value and pass it to the Woocs gadget on the checkout page. The JavaScript code below is intended to select the currency based on the cookie value. Yet, it only sets but doesn’t switch the Woocs currency selector as intended. Manually selecting a currency from the dropdown works seamlessly.
document.addEventListener('DOMContentLoaded', function() {
const selectElement = document.querySelector('select[name="woocommerce-currency-switcher"]');// Function to get the value of the specified cookie
function getCookie(name) {
const cookies = document.cookie.split('; ');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].split('=');
if (cookie[0] === name) {
return cookie[1];
}
}
return null;
}// Get the value of 'Usr5electedCurr3ncy' cookie
const selectedCurrency = getCookie('Usr5electedCurr3ncy');if (selectedCurrency) {
// Loop through options to find and select the matching currency
for (let i = 0; i < selectElement.options.length; i++) {
if (selectElement.options[i].value === selectedCurrency) {
selectElement.value = selectedCurrency;
break;
}
}
}
});I seek your expertise in resolving this issue. I aim to set the currency based on the cookie and enable the switch in checkout information, allowing customers to transact in their selected currency rather than defaulting to USD. Your guidance in this matter would be greatly appreciated.
Thank you for your time and assistance.
Hello Team,
I hope this message finds you well. I am utilizing the pro version of this plugin and has successfully implemented a currency switcher on the checkout page using the following code placed within the functions.php file. The setup is functioning seamlessly on the /checkout/ page:
// Add a custom select field in checkout
add_action('woocommerce_before_checkout_billing_form', 'add_custom_checkout_select_field');
function add_custom_checkout_select_field( $checkout ) {
// Here set in the function your product category term ID, slugs, names or array
if ( ! has_product_category_in_cart( 'concierge' ) && shortcode_exists( 'woocs' ) ) {
echo '<label for="payment_option" class="payment_option">'.__('Preferred currency').'</label>';
echo '<div class="own">' . do_shortcode('[woocs]') . '</div>';
}
}
// Custom Checkout fields validation
add_action('woocommerce_checkout_process', 'custom_checkout_select_field_validation');
function custom_checkout_select_field_validation() {
if ( isset($_POST['payopt']) && empty($_POST['payopt']) )
wc_add_notice( '<strong>Please select a currency</strong>', 'error' );
}
// Reset currency selector if it's not checkout
add_filter('wp_head',function(){
if(!is_checkout()){
global $WOOCS;
$WOOCS->reset_currency();
}
});
However, the website is not a standard WooCommerce setup. The single product page has been customized, incorporating a third-party gadget that dynamically updates prices akin to the hotel industry. Upon adding a product to the cart, it defaults to USD due to WooCommerce settings, whereas their gadget provides a currency switcher functionality.
To address this, I’ve implemented cookies with the name “Usr5electedCurr3ncy” to store the selected currency from third-party gadget. The challenge arises when attempting to read this cookie value and pass it to the Woocs gadget on the checkout page. The JavaScript code below is intended to select the currency based on the cookie value. Yet, it only sets but doesn’t switch the Woocs currency selector as intended. Manually selecting a currency from the dropdown works seamlessly.
document.addEventListener('DOMContentLoaded', function() {
const selectElement = document.querySelector('select[name="woocommerce-currency-switcher"]');
// Function to get the value of the specified cookie
function getCookie(name) {
const cookies = document.cookie.split('; ');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].split('=');
if (cookie[0] === name) {
return cookie[1];
}
}
return null;
}
// Get the value of 'Usr5electedCurr3ncy' cookie
const selectedCurrency = getCookie('Usr5electedCurr3ncy');
if (selectedCurrency) {
// Loop through options to find and select the matching currency
for (let i = 0; i < selectElement.options.length; i++) {
if (selectElement.options[i].value === selectedCurrency) {
selectElement.value = selectedCurrency;
break;
}
}
}
});
I seek your expertise in resolving this issue. I aim to set the currency based on the cookie and enable the switch in checkout information, allowing customers to transact in their selected currency rather than defaulting to USD. Your guidance in this matter would be greatly appreciated.
Thank you for your time and assistance.
Quote from Pablo Borysenco on January 8, 2024, 12:49Hello
Please renew your support
It depends on the type of switcher. Depending on the JS library, you should assign a currency and call the trigger. An example:
if (woocs_drop_down_view == 'chosen' || woocs_drop_down_view == 'chosen_dark') {
try {
if (jQuery("select.woocommerce-currency-switcher").not('.woocs_clean_select').length) {
jQuery("select.woocommerce-currency-switcher").not('.woocs_clean_select').chosen({
disable_search_threshold: 10
});
jQuery('select.woocommerce-currency-switcher').trigger("chosen:updated");
}
} catch (e) {
console.log(e);
}
}
Hello
Please renew your support
It depends on the type of switcher. Depending on the JS library, you should assign a currency and call the trigger. An example:
if (woocs_drop_down_view == 'chosen' || woocs_drop_down_view == 'chosen_dark') {
try {
if (jQuery("select.woocommerce-currency-switcher").not('.woocs_clean_select').length) {
jQuery("select.woocommerce-currency-switcher").not('.woocs_clean_select').chosen({
disable_search_threshold: 10
});
jQuery('select.woocommerce-currency-switcher').trigger("chosen:updated");
}
} catch (e) {
console.log(e);
}
}