WooCommerce 5.4.1 - 直观地格式化价格 $product->get_price()

WooCommerce 5.4.1 - Formatting price visually $product->get_price()

我需要直观地从价格中删除小数位 (00),并只保留第一个整数后的逗号或点。

例如:$10,

例如:$7,

这是代码:

$retorno .= "<div class='cc_btn_checkout_container d-none' data-price='".$product->get_price()."'><div 

对象接收价格:.$product->get_price()。 = 10.00 美元

这里里面显示价格:

 <p id='cc_btn_amount'>$ 10.00</p>

我正在尝试使用此功能....但直到现在什么也没有发生。

$retorno .= "<div class='cc_btn_checkout_container d-none' data-price='".number_format($product->get_price(), 0)."'><div 

数字需要这样显示:$10,

我该怎么做?在这种情况下使用的是正确的函数吗?

Obs:我必须保留价格后的逗号,在这种情况下我不能像其他 Woo 函数那样删除逗号或点。

你可以使用这个过滤器 http://hookr.io/filters/woocommerce_price_format/ 或者这个:

add_filter( 'woocommerce_price_trim_zeros', '__return_true' );

Tyche Softwares

查看本文中的函数

如果不需要,只需从 return 和 <sup> 中删除 $decimal。

“如果你想要上标中的小数点分隔符,你也可以很容易地做到这一点。如果你看一下我们的过滤器,它有一个名为 $decimal_separator 的参数。这与我们的分隔符相同在 WooCommerce 设置中设置。我们只需要将其包含在我们的最终结果中。这是一个示例 –

add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
    $decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
    return $unit . '<sup>' . $decimal_separator. $decimal . '</sup>';
}