woocommerce get_woocommerce_currency_symbol()

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?

Answers


Your code should work, which means the issue might be in the database. You can check this 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') returnning? 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.


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);

get_option('woocommerce_currency')

will help you lot.


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(); ?>

  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/

use this to show price with currency

$product->get_price_html();

OR use

echo get_woocommerce_currency_symbol();

to show the currency only


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);


Need Your Help

DateTimeFormat in TypeScript

typescript

I want to display a time string in 24 hour format and thought it would be easy in TypeScript. But I can't use the Date.toLocaleTimeString() with options for some reason. Any idea why? They are defi...

Ruby strftime: Month without leading zero?

ruby date format strftime

Does Ruby's strftime have a format for the month without a leading zero?