Html 自定义字段值

Html 自定义字段值,html,wordpress,function,custom-fields,Html,Wordpress,Function,Custom Fields,我尝试使用自定义字段(单价)格式化某些产品的价格。我的值是3.2(3,2),但代码不识别逗号或点,它只显示3 有什么方法可以让我展示全部价值吗? 感谢您的帮助 function cw_change_product_html( $price_html, $product ) { $unit_price = get_post_meta( $product->id, 'unit_price', true ); if ( ! empty( $unit_price ) ) { $price

我尝试使用自定义字段(单价)格式化某些产品的价格。我的值是3.2(3,2),但代码不识别逗号或点,它只显示3 有什么方法可以让我展示全部价值吗? 感谢您的帮助

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 );`
函数cw\u change\u product\u html($price\u html,$product)
{
$unit\u price=get\u post\u meta($product->id,'unit\u price',true);
如果(!空($单价)){
$price_html=''.wc_price($unit_price)。'per kg';
}
返回$price_html;
}    
添加过滤器('woocommerce\u get\u price\u html','cw\u change\u product\u html',10,2);
功能cw\u更改\u产品\u价格\u购物车($price、$cart\u item、$cart\u item\u key)
{
$unit_price=get_post_meta($cart_item['product_id','unit_price',true);
如果(!空($单价))
{
$price=wc_价格(“单价”),“每千克”;
}
返回$price;}
添加过滤器('woocommerce\u cart\u item\u price','cw\u change\u product\u price\u cart',10,3)`

请尝试从
$unit\u price=get\u post\u meta($product->id,'unit\u price',true)转储输出(只是为了更好地理解变量)作为

var_dump(单价)

这将返回字符串。然后将字符串转换为float

$unit_price=floatval($unit_price)

然后您可以使用
wc\u price($unit\u price)


检查文档,因为$price
floatvalue

您能更具体地说明代码的外观吗?不太专业。非常感谢。