Woocommerce Currency Switcher Shortcodes

Below, you’ll find a detailed guide on how to add the Woocommerce Currency Switcher Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Woocommerce Currency Switcher Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Woocommerce Currency Switcher Plugin and the shortcodes it provides:

Plugin Icon
FOX – Currency Switcher Professional for WooCommerce

"FOX – Currency Switcher Professional for WooCommerce is a powerful plugin enabling online stores to display prices and accept payments in multiple currencies, enhancing global eCommerce."

★★★★✩ (222) Active Installs: 60000+ Tested with: 6.3.2 PHP Version: 7.2
Included Shortcodes:
  • [woocs]
  • [woocs_get_sign_rate]
  • [woocs_converter]
  • []
  • [woocs_show_current_currency]
  • [woocs_show_custom_price]
  • [woocs_geo_hello]
  • [woocs_price]

Woocommerce Currency Switcher [woocs] Shortcode

The Woocommerce Currency Switcher (WOOCS) shortcode is designed to facilitate currency switching in your Woocommerce store. It allows you to enqueue styles and scripts, and render HTML views based on specific arguments. This shortcode works by checking if there are any arguments passed and setting them as shortcode parameters. If the ‘sd’ argument is set and is greater than 0, it enqueues specific styles and scripts. The shortcode also checks if the shop is cached and enqueues an additional script if true. It then sets ‘sd_id’ and ‘sd_settings’ arguments based on the ‘sd’ argument value. Finally, it returns the HTML view for the shortcode.

Shortcode: [woocs]

Parameters

Here is a list of all possible woocs shortcode parameters and attributes:

  • sd – Determines the style dimension for the currency switcher
  • sd_id – Retrieves the specific style settings for the currency switcher
  • sd_settings – Fetches the style settings for the selected dimension
  • shortcode_params – Carries all parameters used in the shortcode

Examples and Usage

Basic example – The following shortcode will call the ‘woocs’ function without any parameters. This will initialize the function with an empty array.

[woocs /]

Advanced examples

The following shortcode will call the ‘woocs’ function with the ‘sd’ parameter set to 1. This will enqueue the necessary styles and scripts for the plugin, and set the ‘sd_id’ and ‘sd_settings’ parameters based on the ‘sd’ value.

[woocs sd=1 /]

In this next advanced example, we’re defining multiple parameters. We’re setting ‘sd’ to 1, and adding additional parameters ‘param1’ and ‘param2’ with values ‘value1’ and ‘value2’ respectively. These additional parameters will be included in the ‘shortcode_params’ array.

[woocs sd=1 param1=value1 param2=value2 /]

PHP Function Code

In case you have difficulties debugging what causing issues with [woocs] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('woocs', array($this, 'woocs_shortcode'));

Shortcode PHP function:

function woocs_shortcode($args) {
        if (empty($args)) {
            $args = array();
        }

        $args['shortcode_params'] = $args;

        if (isset($args['sd']) AND intval($args['sd']) > 0) {
            wp_enqueue_style('woocs-sd-selectron23', WOOCS_LINK . 'css/sd/selectron23.css', [], WOOCS_VERSION);
            wp_enqueue_script('woocs-sd-selectron23', WOOCS_LINK . 'js/sd/selectron23.js', [], WOOCS_VERSION);
            wp_enqueue_script('woocs-sd-front', WOOCS_LINK . 'js/sd/front.js', ['woocs-sd-selectron23'], WOOCS_VERSION);

            if ($this->shop_is_cached) {
                wp_enqueue_script('woocs-sd-front-cache', WOOCS_LINK . 'js/sd/front-cache.js', ['woocs-sd-front'], WOOCS_VERSION);
            }

            global $WOOCS_SD;
            $args['sd_id'] = intval($args['sd']);
            $args['sd_settings'] = $WOOCS_SD->get(intval($args['sd']));
        }

        return $this->render_html(WOOCS_PATH . 'views/shortcodes/woocs.php', $args);
    }

Code file location:

woocommerce-currency-switcher/woocommerce-currency-switcher/classes/woocs.php

Woocommerce Currency Switcher [woocs_get_sign_rate] Shortcode

The Woocommerce Currency Switcher shortcode is a function that fetches the exchange rate for a specified currency. This shortcode takes a ‘sign’ parameter, which corresponds to a currency symbol. It then retrieves the rate for that currency from the plugin’s stored list of currencies. If the currency isn’t found, it returns a rate of 0.

Shortcode: [woocs_get_sign_rate]

Parameters

Here is a list of all possible woocs_get_sign_rate shortcode parameters and attributes:

  • sign – The currency symbol to retrieve the rate for.

Examples and Usage

Basic example – A simple usage of the ‘woocs_get_sign_rate’ shortcode to fetch the currency rate of USD.

[woocs_get_sign_rate sign="USD"]

Advanced examples

Using the ‘woocs_get_sign_rate’ shortcode to fetch the currency rate of EUR. If the currency sign is not found, it will return 0.

[woocs_get_sign_rate sign="EUR"]

Another example using the ‘woocs_get_sign_rate’ shortcode to fetch the currency rate of GBP. Remember that the shortcode is case-insensitive, so “gbp” and “GBP” will return the same result.

[woocs_get_sign_rate sign="gbp"]

Please note that the ‘woocs_get_sign_rate’ shortcode will only return the currency rate. If the currency sign does not exist in your WooCommerce settings, it will return 0.

PHP Function Code

In case you have difficulties debugging what causing issues with [woocs_get_sign_rate] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('woocs_get_sign_rate', array($this, 'get_sign_rate'));

Shortcode PHP function:

function get_sign_rate($atts) {
        $sign = strtoupper($atts['sign']);
        $currencies = $this->get_currencies();
        $rate = 0;

        if (isset($currencies[$sign])) {
            $rate = esc_html($currencies[$sign]['rate']);
        }

        return $rate;
    }

Code file location:

woocommerce-currency-switcher/woocommerce-currency-switcher/classes/woocs.php

Woocommerce Currency Switcher [woocs_converter] Shortcode

The Woocommerce Currency Switcher (WOOCS) shortcode is a function that converts the currency displayed on your WordPress site. It uses the ‘woocs_converter’ shortcode, which is a dynamic tool that allows you to specify the currency conversion you want to implement. This shortcode works by calling the ‘woocs_converter’ function. If no arguments are passed, it will default to an empty array. It then renders the HTML found in the ‘woocs_converter.php’ file located in the ‘views/shortcodes’ directory of the WOOCS plugin.

Shortcode: [woocs_converter]

Examples and Usage

Basic example – A simple use of the woocs_converter shortcode without any parameters.

[woocs_converter]

PHP Function Code

In case you have difficulties debugging what causing issues with [woocs_converter] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('woocs_converter', array($this, 'woocs_converter'));

Shortcode PHP function:

function woocs_converter($args) {
        if (empty($args)) {
            $args = array();
        }

        return $this->render_html(WOOCS_PATH . 'views/shortcodes/woocs_converter.php', $args);
    }

Code file location:

woocommerce-currency-switcher/woocommerce-currency-switcher/classes/woocs.php

Woocommerce Currency Switcher [woocs_show_current_currency] Shortcode

The Woocommerce Currency Switcher shortcode is a powerful tool that displays the current currency on your site. This shortcode extracts the current currency, flag, and code, and allows customization of the display text. It then renders this information in HTML for display. This function is essential for global e-commerce sites, providing clarity for international customers.

Shortcode: [woocs_show_current_currency]

Parameters

Here is a list of all possible woocs_show_current_currency shortcode parameters and attributes:

  • text – Defines the introductory text before currency details.
  • currency – Specifies the currency to be displayed.
  • flag – If set to 1, the country flag will be shown; 0 to hide.
  • code – If set to 1, currency code will be displayed; 0 to hide.

Examples and Usage

Basic example – The shortcode is used to display the current currency of the WooCommerce store.

[woocs_show_current_currency]

Advanced examples

Using the shortcode to display the current currency with a custom text. The text will be displayed before the currency.

[woocs_show_current_currency text="Your selected currency is"]

Using the shortcode to display the current currency without the flag. The flag parameter determines whether the flag of the country of the currency will be displayed or not. By setting it to 0, the flag will not be displayed.

[woocs_show_current_currency flag=0]

Using the shortcode to display the current currency without the currency code. The code parameter determines whether the currency code will be displayed or not. By setting it to 0, the currency code will not be displayed.

[woocs_show_current_currency code=0]

Using the shortcode to display the current currency with a custom text and without the flag and currency code.

[woocs_show_current_currency text="Your selected currency is" flag=0 code=0]

PHP Function Code

In case you have difficulties debugging what causing issues with [woocs_show_current_currency] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('woocs_show_current_currency', array($this, 'woocs_show_current_currency'));

Shortcode PHP function:

function woocs_show_current_currency($atts) {
        $currencies = $this->get_currencies();
        extract(shortcode_atts(array(
            'text' => esc_html__('Current currency is:', 'woocommerce-currency-switcher'),
            'currency' => $this->current_currency,
            'flag' => 1,
            'code' => 1,
                        ), $atts));

        $args = array();
        $args['currencies'] = $currencies;
        $args['text'] = $text;
        $args['currency'] = $currency;
        $args['flag'] = $flag;
        $args['code'] = $code;

        return $this->render_html(WOOCS_PATH . 'views/shortcodes/woocs_show_current_currency.php', $args);
    }

Code file location:

woocommerce-currency-switcher/woocommerce-currency-switcher/classes/woocs.php

Woocommerce Currency Switcher [woocs_show_custom_price] Shortcode

The Woocommerce Currency Switcher shortcode is used to display custom prices in different currencies. The function ‘woocs_show_custom_price’ takes parameters like value, decimals, and currency. It extracts these parameters and converts the value into the specified currency. If the currency is not specified, it uses the current currency. It also allows you to set the number of decimal places. The function returns a span element with the converted price and data attributes for value, decimals, and currency. If the specified currency is not found, it returns an error message.

Shortcode: [woocs_show_custom_price]

Parameters

Here is a list of all possible woocs_show_custom_price shortcode parameters and attributes:

  • value – Sets the initial value of the currency.
  • decimals – Determines the number of decimals in the price.
  • currency – Specifies the currency to be displayed.

Examples and Usage

Basic example: Display a custom price in the current currency.

[woocs_show_custom_price value=100]

Advanced examples

Display a custom price with specific decimals in the current currency.

[woocs_show_custom_price value=100 decimals=2]

Display a custom price in a specific currency. In this case, the currency is set to USD.

[woocs_show_custom_price value=100 currency='USD']

Display a custom price with specific decimals in a specific currency. In this case, the currency is set to EUR and decimals to 2.

[woocs_show_custom_price value=100 decimals=2 currency='EUR']

PHP Function Code

In case you have difficulties debugging what causing issues with [woocs_show_custom_price] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('woocs_show_custom_price', array($this, 'woocs_show_custom_price'));

Shortcode PHP function:

function woocs_show_custom_price($atts) {
        $atts = wc_clean($atts);
        extract(shortcode_atts(array(
            'value' => 0,
            'decimals' => -1,
            'currency' => '',
                        ), $atts));

        $currencies = $this->get_currencies();
        $convert = true;
        $_REQUEST['woocs_show_custom_price'] = TRUE;
        $args = array(
            'currency' => $currencies[$this->current_currency]['name'],
        );

        if ($decimals != -1) {
            $args['decimals'] = intval($decimals);
        }

        $currency_tmp = $this->current_currency;
        if ($currency && $currency != $currency_tmp && isset($currencies[$currency])) {
            $args['currency'] = $currency;
            $this->set_currency($currency);
        }

        $wc_price = $this->wc_price(floatval($value), $convert, $args);

        if ($currency && isset($currencies[$currency]) && $currency_tmp != $currency) {
            $this->set_currency($currency_tmp);
        }

        unset($_REQUEST['woocs_show_custom_price']);

        if (!empty($currency)) {
            if (!isset($currencies[$currency])) {
                return esc_html__('Wrong currency code', 'woocommerce-currency-switcher');
            }
        }

        return '<span class="woocs_amount_custom_price" data-value="' . floatval($value) . '" '
                . ' data-decimals="' . intval($decimals) . '" '
                . ' data-currency="' . esc_html($currency) . '" >'
                . $wc_price . "</span>";
    }

Code file location:

woocommerce-currency-switcher/woocommerce-currency-switcher/classes/woocs.php

Woocommerce Currency Switcher [woocs_geo_hello] Shortcode

The Woocommerce Currency Switcher shortcode ‘woocs_geo_hello’ displays the user’s country using WooCommerce GeoIP functionality. If the country is undefined, it shows a notification.

Shortcode: [woocs_geo_hello]

Examples and Usage

Basic example – Displays the visitor’s country as determined by WooCommerce’s GeoIP functionality. If the country is not defined, it will display a message indicating trouble with the GeoIP service.

[woocs_geo_hello /]

PHP Function Code

In case you have difficulties debugging what causing issues with [woocs_geo_hello] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('woocs_geo_hello', array($this, 'woocs_geo_hello'));

Shortcode PHP function:

function woocs_geo_hello($atts = '') {

        $pd = array();
        $countries = array();
        $text = '';
        if (class_exists('WC_Geolocation')) {
            $c = new WC_Countries();
            $countries = $c->get_countries();
            $pd = WC_Geolocation::geolocate_ip();
        }

        if (!empty($pd) AND !empty($countries) AND $pd['country']) {
            $text = '<span class="woocs_geo_hello">' . sprintf(esc_html__('Your country is: %s. (defined by woocommerce GeoIP functionality)', 'woocommerce-currency-switcher'), esc_html($countries[$pd['country']])) . '</span>';
        } else {
            $text = '<i class="woocs_geo_hello_not">' . esc_html__('Your country is not defined! Troubles with GeoIp service.', 'woocommerce-currency-switcher') . '</i>';
        }

        return $text;
    }

Code file location:

woocommerce-currency-switcher/woocommerce-currency-switcher/classes/woocs.php

Woocommerce Currency Switcher [woocs_price] Shortcode

The Woocommerce Currency Switcher shortcode allows you to display product prices in different currencies. It takes ‘id’ and ‘currency’ as arguments. If ‘id’ isn’t provided, it fetches the global product object. If a currency is specified, it temporarily switches to that currency, retrieves the product price, then reverts back to the original currency.

Shortcode: [woocs_price]

Parameters

Here is a list of all possible woocs_price shortcode parameters and attributes:

  • id – Unique identifier of the WooCommerce product
  • currency – Represents the desired currency for product price

Examples and Usage

Basic example – Display the price of a product with a specific ID.

[woocs_price id=123 /]

Advanced examples

Display the price of a product with a specific ID in a specified currency. This is useful when you want to show the price of a product in a different currency than the default one set in your WooCommerce settings.

[woocs_price id=123 currency="USD" /]

Display the price of the current product in a specified currency. This is useful when you want to show the price of the product in a different currency on the product page. In this case, you don’t need to specify the product ID as the shortcode will automatically take the ID of the current product.

[woocs_price currency="EUR" /]

Please note that the ‘currency’ parameter in the shortcode should be one of the currencies available in your WooCommerce Currency Switcher settings. If the specified currency is not available, the shortcode will display the price in the default currency.

PHP Function Code

In case you have difficulties debugging what causing issues with [woocs_price] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('woocs_price', array($this, 'woocs_price_shortcode'));

Shortcode PHP function:

function woocs_price_shortcode($args) {
        $price = "";
        $product_o = null;

        if (empty($args)) {
            $args = array();
        }

        if (!isset($args['id']) AND is_product()) {
            global $product;
            $product_o = $product;
        } else {
            if (isset($args['id'])) {
                $product_o = wc_get_product($args['id']);
            }
        }

        $currency = '';
        $tmp_currency = $this->current_currency;
        $currencies = $this->get_currencies();

        if (isset($args['currency']) && isset($currencies[$args['currency']]) && $args['currency'] != $tmp_currency) {
            $currency = $args['currency'];
            $this->set_currency($currency);
        }

        if (is_object($product_o) AND method_exists($product_o, 'get_price_html')) {
            $price = $product_o->get_price_html();
        }

        if ($currency && $currency != $tmp_currency) {
            $this->set_currency($tmp_currency);
        }

        return apply_filters('woocs_price_shortcode', $price, $product_o);
    }

Code file location:

woocommerce-currency-switcher/woocommerce-currency-switcher/classes/woocs.php

Conclusion

Now that you’ve learned how to embed the Woocommerce Currency Switcher Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *