custom function for woocommerce price format code example

Example: custom function for woocommerce price format

/**
  * Format price to WooCommerce standards
  * 
  * @access public
  * @param float $price
  * @return string
  */
 public function format_price($price)
 {
     $num_decimals = absint(get_option('woocommerce_price_num_decimals'));
     $currency_symbol = get_woocommerce_currency_symbol();
     $decimal_sep = wp_specialchars_decode(stripslashes(get_option('woocommerce_price_decimal_sep')), ENT_QUOTES);
     $thousands_sep = wp_specialchars_decode(stripslashes(get_option('woocommerce_price_thousand_sep')), ENT_QUOTES);
     $price = number_format($price, $num_decimals, $decimal_sep, $thousands_sep);
     if (apply_filters('woocommerce_price_trim_zeros', false) && $num_decimals > 0) {
         $price = preg_replace('/' . preg_quote(get_option('woocommerce_price_decimal_sep'), '/') . '0++$/', '', $price);
     }
     return sprintf(get_woocommerce_price_format(), $currency_symbol, $price);
 }

Tags:

Php Example