php - 更改 Woocommerce 显示的购物车小计的小数位数
问题描述
我正在使用“更改 Woocommerce 购物车总数中的小数位数”答案代码以显示2 位小数的总数。该代码适用于购物车和结帐页面中显示的总数。
如果可能的话,我也想为小计做同样的事情。任何帮助表示赞赏。
解决方案
要在购物车和结帐页面中使用 2 位小数格式化显示的小计金额,请使用以下命令:
// Displayed formatted cart subtotal in cart and checkout pages
add_filter( 'woocommerce_cart_subtotal', 'filter_cart_subtotal_html', 10, 3 );
function filter_cart_subtotal_html( $cart_subtotal, $compound, $cart ) {
$decimals = 2; // <== <== Set the number of decimals to be displayed
$args = ['decimals' => strval($decimals)]; // Initializing
if ( $compound ) {
$cart_subtotal = wc_price( $cart->get_cart_contents_total() + $cart->get_shipping_total() + $cart->get_taxes_total( false, false ), $args );
} elseif ( $cart->display_prices_including_tax() ) {
$cart_subtotal = wc_price( $cart->get_subtotal() + $cart->get_subtotal_tax(), $args );
if ( $cart->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$cart_subtotal = wc_price( $cart->get_subtotal(), $args );
if ( $cart->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
return $cart_subtotal;
}
代码位于活动子主题(或活动主题)或插件文件的 functions.php 文件中。测试和工作。
推荐阅读
- python - 在 matplotlib / pie 中对齐组标签
- python-3.x - Pandas:基于每个元素的索引和列名的函数填充数据框的快速方法
- python - 带有硒的递归函数
- haskell - 尝试实现二叉树搜索
- javascript - 使用 jQuery 操作 EasyMDE 字段的内容
- node.js - 如何使用 AWS SDK 解析来自 getThingShadow 的 json 结果
- excel - 如何应用 SearchDirection 来查找第一次出现?
- docker - Hello world with gitlab ce docker 容器在本地 ubuntu 上运行
- python - Python 从文件中读取与直接分配文字
- python - 在回归中使用 gurobi python 的荒谬解决方案