• Resolved Group Of Oceninfo

    (@princebhalani143)


    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.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.