亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WooCommerce 刪除 3 位小數中的多余零

WooCommerce 刪除 3 位小數中的多余零

PHP
慕工程0101907 2023-10-21 19:59:02
我使用 3 位小數,因此我的價格顯示為:"$20.000" 、 "$20.050" 、 "$20.055"我搜索了有關刪除零小數的信息,并找到了這個:    add_filter( 'woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1 );function wc_hide_trailing_zeros( $trim ) {    // set to false to show trailing zeros    return true;}但是,它無法正常工作。因為它只刪除 0.000 位小數。(20.000 美元到 20 美元)此外,我想從最后一個小數段刪除額外的 0 小數。例如; “$20.000”改為“$20.00”“$20.050”改為“$20.05”“$20.055”不會改變
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

您的上述過濾器(返回)會觸發functiontrue的執行,該函數確實僅在價格只有 0 位小數時才刪除零。wc_trim_zeros()

你需要的是使用formatted_woocommerce_price鉤子來代替。

下面的過濾器將刪除所有尾隨零:

add_filter('formatted_woocommerce_price', function($formatted_price, $price, $decimals, $decimal_separator) {

? ? // Need to trim 0s only if we have the decimal separator present.

? ? if (strpos($formatted_price, $decimal_separator) !== false) {

? ? ? ? $formatted_price = rtrim($formatted_price, '0');

? ? ? ? // After trimming trailing 0s, it may happen that the decimal separator will remain there trailing... just get rid of it, if it's the case.

? ? ? ? $formatted_price = rtrim($formatted_price, $decimal_separator);

? ? }

? ? return $formatted_price;

}, 10, 4);

更新:如果是第三個也是最后一個小數,下面的代碼只會刪除尾隨零:


add_filter('formatted_woocommerce_price', function($formatted_price, $price, $decimals, $decimal_separator) {

? ? // Need to trim 0s only if we have the decimal separator present.

? ? if (strpos($formatted_price, $decimal_separator) !== false) {

? ? ? ? $formatted_price = preg_replace('/^(\d+' . preg_quote($decimal_separator, '/' ) . '\d{2})0$/', "$1", $formatted_price);

? ? }

? ? return $formatted_price;

}, 10, 4);

免責聲明:代碼未經嘗試,直接寫在答案框中。


查看完整回答
反對 回復 2023-10-21
  • 1 回答
  • 0 關注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號