I try to format some products price using a custom field (unit_price). My value is 3.2 (3,2) but the code doesn't recognize comma or dot and it shows only 3 Is there any way I could achieve showing the full value? Any help is appreciated.
function cw_change_product_html( $price_html, $product )
{
$unit_price = get_post_meta( $product->id, 'unit_price', true );
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . ' per kg</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
function cw_change_product_price_cart( $price, $cart_item, $cart_item_key )
{
$unit_price = get_post_meta( $cart_item['product_id'], 'unit_price', true );
if ( ! empty( $unit_price ) )
{
$price = wc_price( $unit_price ) . ' per kg';
}
return $price;}
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_cart', 10, 3 );`
Please try dumping the output(just for better understanding the variable) from $unit_price = get_post_meta( $product->id, 'unit_price', true );
as
var_dump($unit_price);
This will return string. Then all you have to convert string into float
$unit_price= floatval($unit_price);
Then you can use wc_price( $unit_price )
.
$price is float value