问题
I am recently working with WooCommerce WordPress project for my client and I am editing a plugin for WooCommerce WordPress. I am trying to display the product price to include the currency code use this function
* @param mixed $item
* @param bool $inc_tax (default: false).
* @param bool $round (default: true).
* @return float
*/
public function get_item_total( $item, $inc_tax = false, $round = true ) {
$qty = ( ! empty( $item['qty'] ) ) ? $item['qty'] : 1;
if ( $inc_tax ) {
$price = ( $item['line_total'] + $item['line_tax'] ) / max( 1, $qty );
} else {
$price = $item['line_total'] / max( 1, $qty );
}
$price = '<span class="price">' . $round ? round( $price, wc_get_price_decimals() ) . '</span>' : '<span class="price">' . $price . '</span>' ;
return apply_filters( 'woocommerce_order_amount_item_total', $price, $this, $item, $inc_tax, $round );
}
But it spits out a number like 55 with out the currency symbol. is there anyway get the number with currency symbol like $55 ?
Thank you for all your help
回答1:
ty rgdesign
This was the fix it works now
public function get_item_total( $item, $inc_tax = false, $round = true ) {
$qty = ( ! empty( $item['qty'] ) ) ? $item['qty'] : 1;
if ( $inc_tax ) {
$price = ( $item['line_total'] + $item['line_tax'] ) / max( 1, $qty );
} else {
$price = $item['line_total'] / max( 1, $qty );
}
$currency_symbol = get_woocommerce_currency_symbol();
$price = $round ? round($price, wc_get_price_decimals() ): $price;
$price = '<span class="price">' .$currency_symbol . $price .'</span>';
return apply_filters( 'woocommerce_order_amount_item_total',$price, $this, $item, $inc_tax, $round );
}
回答2:
I found it to be very easy. Just insert the following code block to functions.php file under theme folder.
//Change the symbol of an existing currency
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
switch( $currency ) {
case 'USD': $currency_symbol = 'USD$'; break;
}
return $currency_symbol;
}
来源:https://stackoverflow.com/questions/41901765/woocommerce-currency-symbol