這裏是一個解決方案。但是,您必須讓自己在woocommerce相關模板中做出所有必要的更改。下面,自定義函數現在
Template Structure + Overriding Templates via a Theme
然後將盡上顯示的HTML格式的價格(價格變化從作業:
所以,第一,如果你不知道如何正確地定製WooCommerce模板,請閱讀本4個小數2位小數,並保持html標記以防萬一),你將能夠做出woocommerce相關的模板進行必要的更改:
function wc_shrink_price($price_html){
// Extract the price (without formatting html code)
$price = floatval(preg_replace('/[^0-9\.,]+/', '', $price_html));
// Round price with 2 decimals precision
$shrink_price = round($price, 2);
// Replace old existing price in the original html structure and return the result
return str_replace($price, $shrink_price, $price_html);
}
代碼放在您的活動子主題(或主題的function.php文件)或者也在任何插件文件。
該代碼測試和工程
用例:
在woocommerce模板cart/cart_totals.php
在行33
你有這樣的原始代碼:
(用於分類彙總顯示價格)
<td data-title="<?php esc_attr_e('Subtotal', 'woocommerce'); ?>"><?php wc_cart_totals_subtotal_html(); ?></td>
如果你搜索wc_cart_totals_subtotal_html()
功能,您將看到使用該WC_Cart
方法:WC()->cart->get_cart_subtotal()
...
所以你可以代替這種方式:
<td data-title="<?php esc_attr_e('Subtotal', 'woocommerce'); ?>">
<?php
// Replacement function by a WC_Cart method
$subtotal_html_price = WC()->cart->get_cart_subtotal();
// Here we use our custom function to get a formated html price with 2 decimals
echo wc_shrink_price($subtotal_html_price);
?>
</td>
所以知道,你可以看到你需要爲所有購物車價格做類似的事情。
的車和結算模板是cart
和checkout
子...
這是你的時間來工作了一下!
謝謝你的回答,這對我有很大幫助!我一回到家就會試一試,如果這對我有用,我會告訴你。但看起來這應該是正確的解決方案! – byaafra
@FrédéricByarelle-Bréard自從你第一個問題以來,我一直在思考,我沒有看到另一個更簡單的方法......所以這會給你一些家庭作品:) – LoicTheAztec
是的,有點功課,它會是一些很好的經驗,我已經學到了一些關於woocommerce的工作原理,現在我將一直在尋找模板,而不是尋找插件:) – byaafra