48

I am developing an extension for woocommerce WordPress plugin.

I would like to display the currency symbol outside of the loop in a custom function

I have the following:

function my_function( ) {
   global  $woocommerce;
   echo get_woocommerce_currency_symbol();
}

I am not sure why but this doesn't output the symbol? Am I missing something?

danyo
  • 5,686
  • 20
  • 59
  • 119

8 Answers8

81

Your code should work, which means the issue might be in the database. You can check these 2 functions:
get_woocommerce_currency() and get_woocommerce_currency_symbol()
from the WooCommerce docs that shows that you are using the functions correct.

What is left is for you to start some troubleshooting steps to see what causes the error:

What is get_option('woocommerce_currency') returning? If nothing, then you have no currency set and that is why you get nothing from get_woocommerce_currency_symbol();

What happens if you add a currency as a parameter to get_woocommerce_currency_symbol? it gets displayed? something like echo get_woocommerce_currency_symbol("USD");

You should add to your script some error handling lines, to inform the user that he needs to have the currency set before using your extension.

26

use this to show price with currency

$product->get_price_html();

OR use

echo get_woocommerce_currency_symbol();

to show the currency only

Md.Sukel Ali
  • 2,987
  • 5
  • 22
  • 34
Hritik Pandey
  • 899
  • 7
  • 17
20

You should always price under wc_price function, it will automatically add currency symbol along with the default span of woo commerce

For ex..
$product_price = 60;
echo wc_price($product_price);
MakeWebBetter
  • 463
  • 3
  • 5
  • This doesnt work with multi currency https://woocommerce.com/products/multi-currency/ – fdrv Jul 10 '22 at 20:09
8
get_option('woocommerce_currency')

will help you lot.

Mukii kumar
  • 169
  • 1
  • 2
  • 10
5
  function get_woocommerce_currency_symbol( $currency = '' ) { 
        if ( ! $currency ) { 
            $currency = get_woocommerce_currency(); 
        } 

        $symbols = apply_filters( 'woocommerce_currency_symbols', array( 
            'AED' => 'د.إ',  
            'AFN' => '؋',  
            'ALL' => 'L',  
            'AMD' => 'AMD',  
            'ANG' => 'ƒ',  
            'AOA' => 'Kz',  
            'ARS' => '$',  
            'AUD' => '$',  
            'AWG' => 'ƒ',  
            'AZN' => 'AZN',  
            'BAM' => 'KM',  
            'BBD' => '$',  
            'BDT' => '৳ ',  
            'BGN' => 'лв.',  
            'BHD' => '.د.ب',  
            'BIF' => 'Fr',  
            'BMD' => '$',  
            'BND' => '$',  
            'BOB' => 'Bs.',  
            'BRL' => 'R$',  
            'BSD' => '$',  
            'BTC' => '฿',  
            'BTN' => 'Nu.',  
            'BWP' => 'P',  
            'BYR' => 'Br',  
            'BZD' => '$',  
            'CAD' => '$',  
            'CDF' => 'Fr',  
            'CHF' => 'CHF',  
            'CLP' => '$',  
            'CNY' => '¥',  
            'COP' => '$',  
            'CRC' => '₡',  
            'CUC' => '$',  
            'CUP' => '$',  
            'CVE' => '$',  
            'CZK' => 'Kč',  
            'DJF' => 'Fr',  
            'DKK' => 'DKK',  
            'DOP' => 'RD$',  
            'DZD' => 'د.ج',  
            'EGP' => 'EGP',  
            'ERN' => 'Nfk',  
            'ETB' => 'Br',  
            'EUR' => '€',  
            'FJD' => '$',  
            'FKP' => '£',  
            'GBP' => '£',  
            'GEL' => 'ლ',  
            'GGP' => '£',  
            'GHS' => '₵',  
            'GIP' => '£',  
            'GMD' => 'D',  
            'GNF' => 'Fr',  
            'GTQ' => 'Q',  
            'GYD' => '$',  
            'HKD' => '$',  
            'HNL' => 'L',  
            'HRK' => 'Kn',  
            'HTG' => 'G',  
            'HUF' => 'Ft',  
            'IDR' => 'Rp',  
            'ILS' => '₪',  
            'IMP' => '£',  
            'INR' => '₹',  
            'IQD' => 'ع.د',  
            'IRR' => '﷼',  
            'IRT' => 'تومان',  
            'ISK' => 'kr.',  
            'JEP' => '£',  
            'JMD' => '$',  
            'JOD' => 'د.ا',  
            'JPY' => '¥',  
            'KES' => 'KSh',  
            'KGS' => 'сом',  
            'KHR' => '៛',  
            'KMF' => 'Fr',  
            'KPW' => '₩',  
            'KRW' => '₩',  
            'KWD' => 'د.ك',  
            'KYD' => '$',  
            'KZT' => 'KZT',  
            'LAK' => '₭',  
            'LBP' => 'ل.ل',  
            'LKR' => 'රු',  
            'LRD' => '$',  
            'LSL' => 'L',  
            'LYD' => 'ل.د',  
            'MAD' => 'د.م.',  
            'MDL' => 'MDL',  
            'MGA' => 'Ar',  
            'MKD' => 'ден',  
            'MMK' => 'Ks',  
            'MNT' => '₮',  
            'MOP' => 'P',  
            'MRO' => 'UM',  
            'MUR' => '₨',  
            'MVR' => '.ރ',  
            'MWK' => 'MK',  
            'MXN' => '$',  
            'MYR' => 'RM',  
            'MZN' => 'MT',  
            'NAD' => '$',  
            'NGN' => '₦',  
            'NIO' => 'C$',  
            'NOK' => 'kr',  
            'NPR' => '₨',  
            'NZD' => '$',  
            'OMR' => 'ر.ع.',  
            'PAB' => 'B/.',  
            'PEN' => 'S/.',  
            'PGK' => 'K',  
            'PHP' => '₱',  
            'PKR' => '₨',  
            'PLN' => 'zł',  
            'PRB' => 'р.',  
            'PYG' => '₲',  
            'QAR' => 'ر.ق',  
            'RMB' => '¥',  
            'RON' => 'lei',  
            'RSD' => 'дин.',  
            'RUB' => '₽',  
            'RWF' => 'Fr',  
            'SAR' => 'ر.س',  
            'SBD' => '$',  
            'SCR' => '₨',  
            'SDG' => 'ج.س.',  
            'SEK' => 'kr',  
            'SGD' => '$',  
            'SHP' => '£',  
            'SLL' => 'Le',  
            'SOS' => 'Sh',  
            'SRD' => '$',  
            'SSP' => '£',  
            'STD' => 'Db',  
            'SYP' => 'ل.س',  
            'SZL' => 'L',  
            'THB' => '฿',  
            'TJS' => 'ЅМ',  
            'TMT' => 'm',  
            'TND' => 'د.ت',  
            'TOP' => 'T$',  
            'TRY' => '₺',  
            'TTD' => '$',  
            'TWD' => 'NT$',  
            'TZS' => 'Sh',  
            'UAH' => '₴',  
            'UGX' => 'UGX',  
            'USD' => '$',  
            'UYU' => '$',  
            'UZS' => 'UZS',  
            'VEF' => 'Bs F',  
            'VND' => '₫',  
            'VUV' => 'Vt',  
            'WST' => 'T',  
            'XAF' => 'Fr',  
            'XCD' => '$',  
            'XOF' => 'Fr',  
            'XPF' => 'Fr',  
            'YER' => '﷼',  
            'ZAR' => 'R',  
            'ZMW' => 'ZK',  
     ) ); 

        $currency_symbol = isset( $symbols[ $currency ] ) ? $symbols[ $currency ] : ''; 

        return apply_filters( 'woocommerce_currency_symbol', $currency_symbol, $currency ); 
    } 


  [1]: http://woocommerce.wp-a2z.org/oik_api/get_woocommerce_currency_symbol/
KTrivedi
  • 131
  • 1
  • 5
3

You want to show the WooCommerce curency symbol in your Custom WordPress Theme or Plugin but don’t know how? Worry no more. Here is all you need to know. For Custom WooCommerce WordPress Plugin:

function show_currency_symbol( ) {
   global  $woocommerce;
   echo get_woocommerce_currency_symbol();
}

For Custom WooCommerce WordPress Theme:

<?php global  $woocommerce;   echo get_woocommerce_currency_symbol(); ?>
Tehseen Ahmed
  • 147
  • 3
  • 15
3

This short code uses the woocommerce_price function; it will automatically add currency symbol along with the default span of woo commerce.

/* Custom price */ $price = 100; echo woocommerce_price($price);

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
  • 1
    Hello, just wanted to let you know that the function you are recommending is deprecated you should use wc_price instead and an answer recommending it already exists. Best! – jnhghy - Alexandru Jantea Dec 25 '18 at 22:54
  • both as same working on woo-commerce according to country base. – Vijendra Yadav Jan 01 '19 at 11:36
  • As I said the functions is deprecated, so it works for now but its planed to be removed so it is not recommended. Please check the docs! This is a late answer that recomands a depracated solution to a question that has better answers. I know you are new to the site but please understand this answer will create more trouble then help in the near future for anyome that uses it. And if you use it in your code you should change to the other function as well to avoid issues in the future. – jnhghy - Alexandru Jantea Jan 01 '19 at 17:13
  • This doesnt work with multi currency https://woocommerce.com/products/multi-currency/ – fdrv Jul 10 '22 at 20:10
-2

You can change the symbol in you ftp files by going to WP-Content > Plugins > Woocommerce > Includes > WC.Core.Functions.php it starts on line 662 for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
carlos
  • 1
  • Just don't do this! Ever! Never change plugin or theme code directly in FTP! Any change done directly in the plugin code will be overwritten and lost when you update the plugin so don't do it like that! – jnhghy - Alexandru Jantea Aug 02 '22 at 14:50