1 回答

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);
免責聲明:代碼未經嘗試,直接寫在答案框中。
- 1 回答
- 0 關注
- 156 瀏覽
添加回答
舉報