首页 > 解决方案 > 带小数分隔符的 PHP 返回价格

问题描述

您能否帮助我了解如何自定义添加到 WordPress/Woocommerce 的以下代码的输出?我希望在千位之后用小数分隔符(点)分隔返回值(常规价格和销售价格),例如“1.200”。

编码:

function sr_change_variable_price($price, $product){
    $html_tag = '<span id="reg_price">%s</span><span id="sale_price">%s</span>';
    if($product->is_type('variable') AND !is_product()){
        $test = $product->get_variation_regular_price('min') . ' Ft' ;
        $min = $product->get_variation_sale_price('min') . ' Ft';
        return(sprintf($html_tag, $test, $min));
    }elseif($product->is_type('variable') AND is_product()){
        return('');
    }else
    {return $price;  // other cases
    }
}
add_filter('woocommerce_get_price_html', 'sr_change_variable_price', 10, 2);

非常感谢您的帮助

标签: phpwoocommercedecimalseparatorprice

解决方案


您可以简单地使用number_format

例子:

$price = number_format(1200, 0, '', '.');
echo $price; // outputs 1.200

推荐阅读