Woo Multi Currency Shortcodes

Below, you’ll find a detailed guide on how to add the Woo Multi Currency 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 Woo Multi Currency Plugin shortcodes not to show or not to work correctly.

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

Plugin Icon
CURCY – Multi Currency for WooCommerce – The best free currency exchange plugin – Run smoothly on WooCommerce 8.x

"CURCY – Multi Currency for WooCommerce is an exceptional free currency exchange plugin, optimized for smooth operation on WooCommerce 8.x. Efficiently manage multiple currencies on your WooCommerce store."

★★★★✩ (218) Active Installs: 20000+ Tested with: 6.3.2 PHP Version: 7.0
Included Shortcodes:
  • [fee]
  • [woo_multi_currency]
  • [woo_multi_currency_exchange]
  • [woo_multi_currency_rates]
  • [woo_multi_currency_product_price_switcher]

Woo Multi Currency [fee] Shortcode

The Woo-Multi-Currency plugin shortcode ‘fee’ calculates a specific fee based on a given percentage. It also adjusts the fee within a minimum and maximum range. If the calculated fee is less than the minimum or more than the maximum, it’s set to the respective limit.

Shortcode: [fee]

Parameters

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

  • percent – The percentage of the fee cost to be calculated.
  • min_fee – The minimum fee amount to be charged.
  • max_fee – The maximum fee amount to be charged.

Examples and Usage

Basic example – The shortcode calculates a fee based on the percentage provided.

[fee percent=10 /]

Advanced examples

1. Calculating a fee with a minimum limit. If the fee calculated is less than the minimum fee, the minimum fee will be used.

[fee percent=10 min_fee=50 /]

2. Calculating a fee with a maximum limit. If the fee calculated is more than the maximum fee, the maximum fee will be used.

[fee percent=10 max_fee=200 /]

3. Calculating a fee with both a minimum and maximum limit. The fee will be constrained within these limits.

[fee percent=10 min_fee=50 max_fee=200 /]

4. Using the shortcode without any parameters. It will return 0 as there’s no percentage, minimum, or maximum fee defined.

[fee /]

PHP Function Code

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

Shortcode line:

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

Shortcode PHP function:

function fee( $atts ) {
		$atts = shortcode_atts(
			array(
				'percent' => '',
				'min_fee' => '',
				'max_fee' => '',
			), $atts, 'fee'
		);

		$calculated_fee = 0;

		if ( $atts['percent'] ) {
			$calculated_fee = $this->fee_cost * ( floatval( $atts['percent'] ) / 100 );
		}

		if ( $atts['min_fee'] && $calculated_fee < $atts['min_fee'] ) {
			$calculated_fee = $atts['min_fee'];
		}

		if ( $atts['max_fee'] && $calculated_fee > $atts['max_fee'] ) {
			$calculated_fee = $atts['max_fee'];
		}

		return $calculated_fee;
	}

Code file location:

woo-multi-currency/woo-multi-currency/frontend/shipping.php

Woo Multi Currency [woo_multi_currency] Shortcode

The Woo Multi Currency shortcode is a powerful tool for e-commerce sites. It allows the display of product prices in multiple currencies, enhancing user experience. This shortcode fetches currency data using ‘WOOMULTI_CURRENCY_F_Data’ settings. It then renders a currency selector template, ‘woo-multi-currency-selector.php’, allowing users to switch currencies.

Shortcode: [woo_multi_currency]

Examples and Usage

Basic Example – A straightforward usage of the ‘woo_multi_currency’ shortcode without any additional parameters. This will display the currency selector with the default settings.

[woo_multi_currency]

PHP Function Code

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

Shortcode line:

add_shortcode( 'woo_multi_currency', array( $this, 'shortcode_woo_multi_currency' ) );

Shortcode PHP function:

function shortcode_woo_multi_currency() {
		$args = array( 'settings' => WOOMULTI_CURRENCY_F_Data::get_ins(), 'shortcode' => 'default' );
		ob_start();
		wmc_get_template( 'woo-multi-currency-selector.php', $args );

		return ob_get_clean();
	}

Code file location:

woo-multi-currency/woo-multi-currency/frontend/shortcode.php

Woo Multi Currency [woo_multi_currency_exchange] Shortcode

The Woo Multi-Currency shortcode enables dynamic currency conversion for WooCommerce products. It uses product ID and currency parameters to display prices. The shortcode extracts attributes like product ID and currency, retrieves product data, and calculates the price in the selected currency. If the product is on sale, it shows the sale price. The shortcode also includes formatting options.

Shortcode: [woo_multi_currency_exchange]

Parameters

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

  • price – The price of the product.
  • original_price – The original price before sale.
  • currency – The currency in which the price is displayed.
  • product_id – The unique identifier of the product.
  • keep_format – Determines if the original price format is maintained.

Examples and Usage

Basic example – Display the price of the product in a specific currency.

[woo_multi_currency_exchange product_id="123" currency="USD"]

Advanced examples

Display the price of the product in a specific currency and keep the original format.

[woo_multi_currency_exchange product_id="123" currency="USD" keep_format="1"]

Display the price of the product in a specific currency, keep the original format, and show the original price.

[woo_multi_currency_exchange product_id="123" currency="USD" keep_format="1" original_price="100"]

Display the price of the product in a specific currency and show the original price.

[woo_multi_currency_exchange product_id="123" currency="USD" original_price="100"]

Note: Replace “123” with your product ID, “USD” with your desired currency, and “100” with the original price of the product. The keep_format parameter is optional and can be set to “1” to keep the original format of the price.

PHP Function Code

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

Shortcode line:

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

Shortcode PHP function:

function woo_multi_currency_exchange( $atts ) {
		global $product;
		extract(
			shortcode_atts(
				array(
					'price'          => '',
					'original_price' => '',
					'currency'       => '',
					'product_id'     => '',
					'keep_format'    => 1,
				), $atts
			)
		);
		if ( $product_id ) {
			$product_obj = wc_get_product( $product_id );
		} elseif ( ! $price ) {
			$product_obj = $product;
		}
		if ( isset( $product_obj ) && is_a( $product_obj, 'WC_Product' ) ) {
			if ( $keep_format && ! $currency || $currency === $this->settings->get_current_currency() ) {
				$price = $product_obj->get_price_html();

				return $price;
			} else {
				$price = $product_obj->get_price( 'edit' );
				if ( $product_obj->is_on_sale() ) {
					$original_price = $product_obj->get_regular_price( 'edit' );
				}
			}
		}
		if ( $price ) {
			$selected_currencies = $this->settings->get_list_currencies();
			if ( $currency && isset( $selected_currencies[ $currency ] ) && is_array( $selected_currencies[ $currency ] ) ) {
				$data   = $selected_currencies[ $currency ];
				$format = self::get_price_format( $data['pos'] );
				$args   = array(
					'currency'     => $currency,
					'price_format' => $format
				);
				if ( $data['decimals'] ) {
					$args['decimals'] = $data['decimals'];
				}

				if ( $original_price && $original_price > $price ) {
					$this->price_args = $args;
					add_filter( 'wc_price_args', array(
						$this,
						'change_price_format_by_specific_currency'
					), PHP_INT_MAX );
					$price_html = wc_format_sale_price( wmc_get_price( $original_price, $currency ), wmc_get_price( $price, $currency ) );
					remove_filter( 'wc_price_args', array(
						$this,
						'change_price_format_by_specific_currency'
					), PHP_INT_MAX );
					$this->price_args = array();

					return "<span class='wmc-cache-value' data-product_id='{$product_id}' data-keep_format='{$keep_format}' data-price='{$price}' data-original_price='{$original_price}' data-currency='{$currency}' >" . $price_html . '</span>';
				} else {
					return "<span class='wmc-cache-value' data-product_id='{$product_id}' data-keep_format='{$keep_format}' data-price='{$price}' data-original_price='{$original_price}' data-currency='{$currency}' >" . wc_price( wmc_get_price( $price, $currency ), $args ) . '</span>';
				}

			} else {
				if ( $original_price && $original_price > $price ) {
					return "<span class='wmc-cache-value' data-product_id='{$product_id}' data-keep_format='{$keep_format}' data-price='{$price}' data-original_price='{$original_price}' data-currency='{$currency}' >" . wc_format_sale_price( wmc_get_price( $original_price ), wmc_get_price( $price ) ) . '</span>';
				} else {
					return "<span class='wmc-cache-value' data-product_id='{$product_id}' data-keep_format='{$keep_format}' data-price='{$price}' data-original_price='{$original_price}' data-currency='{$currency}' >" . wc_price( wmc_get_price( $price ) ) . '</span>';
				}
			}
		} else {
			return '';
		}
	}

Code file location:

woo-multi-currency/woo-multi-currency/frontend/shortcode.php

Woo Multi Currency [woo_multi_currency_rates] Shortcode

The Woo Multi Currency Rates shortcode is designed to display current exchange rates on your website. It allows you to specify which currencies to display or show all by default. The shortcode fetches the list of currencies and the default currency from the plugin settings. If specific currencies are provided, it filters the list accordingly. The exchange rates are then displayed in a clean, organized format.

Shortcode: [woo_multi_currency_rates]

Parameters

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

  • currencies – specifies the currencies to be displayed in the currency rates list

Examples and Usage

Basic example – Display the exchange rates for all currencies configured in the Woo Multi Currency plugin settings.

[woo_multi_currency_rates]

Advanced examples

Display the exchange rates for specific currencies. In this case, we are specifying the currencies as USD and EUR.

[woo_multi_currency_rates currencies="USD,EUR"]

Display the exchange rates for multiple currencies. Here, we are specifying the currencies as USD, EUR, and GBP.

[woo_multi_currency_rates currencies="USD,EUR,GBP"]

Note: The ‘currencies’ attribute in the shortcode accepts a comma-separated list of currency codes. The currency codes should be in uppercase and match the currency codes configured in the Woo Multi Currency plugin settings. If no ‘currencies’ attribute is provided, the shortcode will display the exchange rates for all currencies configured in the plugin settings.

PHP Function Code

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

Shortcode line:

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

Shortcode PHP function:

function woo_multi_currency_rates( $atts, $content = null ) {
		extract(
			shortcode_atts(
				array(
					'currencies' => '',
				), $atts
			)
		);
		if ( $currencies ) {
			$currencies = array_map( 'strtoupper', array_map( 'trim', array_filter( explode( ',', $currencies ) ) ) );
		} else {
			$currencies = array();
		}
		$list_currencies  = $this->settings->get_list_currencies();
		$currency_default = $this->settings->get_default_currency();
		ob_start(); ?>
        <div class="woo-multi-currency wmc-shortcode wmc-list-currency-rates">
			<?php
			if ( count( $currencies ) ) {
				foreach ( $currencies as $currency ) {
					if ( array_key_exists( $currency, $list_currencies ) ) {
						if ( $currency == $currency_default ) {
							continue;
						} ?>
                        <div class="wmc-currency-rate">
							<?php echo esc_html( $currency_default . '/' . $currency ) ?> = <?php

							echo esc_html( $list_currencies[ $currency ]['rate'] );
							?>
                        </div>
					<?php }
				}
			} else {
				foreach ( $list_currencies as $key => $currency ) {
					if ( $key == $currency_default ) {
						continue;
					} ?>
                    <div class="wmc-currency-rate">
						<?php echo esc_html( $currency_default . '/' . $key ) ?> = <?php
						echo esc_html( $currency['rate'] );
						?>
                    </div>
				<?php }
			} ?>
        </div>
		<?php
		return ob_get_clean();
	}

Code file location:

woo-multi-currency/woo-multi-currency/frontend/shortcode.php

Woo Multi Currency [woo_multi_currency_product_price_switcher] Shortcode

The Woo Multi Currency shortcode is used to create a product price switcher. It allows users to switch between different currencies on a WooCommerce product page. This feature enhances the user experience by providing prices in the visitor’s local currency.

Shortcode: [woo_multi_currency_product_price_switcher]

Examples and Usage

Basic example – Display the Woo Multi Currency product price switcher for a specific product by referencing its ID.

[woo_multi_currency_product_price_switcher product_id=99 /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'woo_multi_currency_product_price_switcher', array( $this, 'product_price_switcher' ) );

Shortcode PHP function:

function product_price_switcher( $atts ) {
		$args = shortcode_atts( array(
			'product_id' => '',
		), $atts );
		global $post;
		$product_id = ! empty( $args['product_id'] ) ? $args['product_id'] : '';
		if ( ! $product_id ) {
			if ( is_object( $post ) && $post->ID && $post->post_type == 'product' && $post->post_status == 'publish' ) {
				$product_id = $post->ID;
			}
		}
		$price_switcher = '';
		if ( $product_id ) {
			$product          = wc_get_product( $product_id );
			$links            = $this->settings->get_links();
			$current_currency = $this->settings->get_current_currency();
			$country          = $this->settings->get_country_data( $current_currency );
			$list_currencies  = $this->settings->get_list_currencies();
			$class            = array( 'wmc-price-switcher' );
			wp_enqueue_style( 'wmc-flags', WOOMULTI_CURRENCY_F_CSS . 'flags-64.min.css' );
			ob_start();
			?>
            <div class="woo-multi-currency <?php echo implode( ' ', $class ) ?>"
                 id="<?php echo esc_attr( self::get_shortcode_id() ) ?>"
                 title="<?php esc_attr_e( 'Please select your currency', 'woo-multi-currency' ) ?>">
                <div class="wmc-currency-wrapper">
                        <span class="wmc-current-currency">
                          <i style="transform: scale(0.8);"
                             class="vi-flag-64 flag-<?php echo strtolower( $country['code'] ) ?> "></i>
                        </span>
                    <div class="wmc-sub-currency">
						<?php
						foreach ( $links as $k => $link ) {
							$sub_class = array( 'wmc-currency' );
							if ( $k === $current_currency ) {
								$sub_class[] = 'wmc-sub-currency-current';
							}
							$country = $this->settings->get_country_data( $k );
							?>
                            <div class="<?php echo esc_attr( implode( ' ', $sub_class ) ) ?>"
                                 data-currency="<?php echo esc_attr( $k ) ?>">
                                <a <?php echo esc_attr( WOOMULTI_CURRENCY_F_Data::get_rel_nofollow() ); ?>
                                        title="<?php echo esc_attr( $country['name'] ) ?>"
                                        href="<?php echo esc_url( $link ) ?>"
                                        class="wmc-currency-redirect" data-currency="<?php echo esc_attr( $k ) ?>">
                                    <i style="transform: scale(0.8);"
                                       class="vi-flag-64 flag-<?php echo strtolower( $country['code'] ) ?> "></i>
									<?php
									switch ( $this->settings->get_price_switcher() ) {
										case 2:
											echo '<span class="wmc-price-switcher-code">' . esc_html( $k ) . '</span>';
											break;
										case 3:
											$decimals           = (int) $list_currencies[ $k ]['decimals'];
											$decimal_separator  = wc_get_price_decimal_separator();
											$thousand_separator = wc_get_price_thousand_separator();
											$symbol             = $list_currencies[ $k ]['custom'];
											$symbol             = $symbol ? $symbol : get_woocommerce_currency_symbol( $k );
											$format             = self::get_price_format( $list_currencies[ $k ]['pos'] );
											$price              = 0;
											$max_price          = '';
											$custom_symbol      = strpos( $symbol, '#PRICE#' );
											if ( $product->get_type() === 'variable' ) {
												$price     = WOOMULTI_CURRENCY_F_Frontend_Price::get_variation_min_price( $product, $k );
												$price_max = WOOMULTI_CURRENCY_F_Frontend_Price::get_variation_max_price( $product, $k );
												if ( $price_max > $price ) {
													$price_max = number_format( wc_get_price_to_display( $product, array(
														'qty'   => 1,
														'price' => $price_max
													) ), $decimals, $decimal_separator, $thousand_separator );
													if ( $custom_symbol === false ) {
														$max_price = ' - ' . sprintf( $format, $symbol, $price_max );
													} else {
														$max_price = ' - ' . str_replace( '#PRICE#', $price_max, $symbol );
													}
												}
											} else {
												if ( $this->settings->check_fixed_price() ) {
													$product_id    = $product->get_id();
													$product_price = wmc_adjust_fixed_price( json_decode( $product->get_meta('_regular_price_wmcp', true ), true ) );
													$sale_price    = wmc_adjust_fixed_price( json_decode( $product->get_meta('_sale_price_wmcp', true ), true ) );
													if ( isset( $product_price[ $k ] ) && ! $product->is_on_sale( 'edit' ) && $product_price[ $k ] > 0 ) {
														$price = $product_price[ $k ];
													} elseif ( isset( $sale_price[ $k ] ) && $sale_price[ $k ] > 0 ) {
														$price = $sale_price[ $k ];
													}
												}
											}
											if ( ! $price && $product->get_price( 'edit' ) ) {
												$price = $product->get_price( 'edit' );
												$price = number_format( wmc_get_price( wc_get_price_to_display( $product, array(
													'qty'   => 1,
													'price' => $price
												) ), $k ), $decimals, $decimal_separator, $thousand_separator );
											} else {
												$price = number_format( wc_get_price_to_display( $product, array(
													'qty'   => 1,
													'price' => $price
												) ), $decimals, $decimal_separator, $thousand_separator );
											}

											if ( $custom_symbol === false ) {
												$formatted_price = sprintf( $format, $symbol, $price );
											} else {
												$formatted_price = str_replace( '#PRICE#', $price, $symbol );
											}
											echo '<span class="wmc-price-switcher-price">' . wp_kses_post( $formatted_price ) . wp_kses_post( $max_price ) . '</span>';
									}
									?>
                                </a>
                            </div>
							<?php
						}
						?>
                    </div>
                </div>
            </div>
			<?php
			$price_switcher = ob_get_clean();
		}

		return $price_switcher;
	}

Code file location:

woo-multi-currency/woo-multi-currency/frontend/shortcode.php

Conclusion

Now that you’ve learned how to embed the Woo Multi Currency 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 *