WooCommerce结账订单评论自定义:通过functions.php实现持久化修改
WooCommerce 结账/购物车页面持久化自定义方案
直接上可复用代码,将以下内容复制到当前激活主题的functions.php文件末尾即可,所有修改均通过WooCommerce自带钩子实现,不会被插件更新覆盖:
1. 给商品单价添加(exclu. tax)标识
add_filter( 'woocommerce_cart_item_price', 'add_excl_tax_label_to_product_price', 10, 3 ); function add_excl_tax_label_to_product_price( $price, $cart_item, $cart_item_key ) { if ( is_cart() || is_checkout() ) { $price .= ' <small>(exclu. tax)</small>'; } return $price; }
2. 让商品小计显示含税金额
add_filter( 'woocommerce_cart_item_subtotal', 'display_tax_inclusive_subtotal', 10, 3 ); function display_tax_inclusive_subtotal( $subtotal, $cart_item, $cart_item_key ) { if ( is_cart() || is_checkout() ) { $tax_inclusive_subtotal = wc_get_price_including_tax( $cart_item['data'], array( 'qty' => $cart_item['quantity'] ) ); $subtotal = wc_price( $tax_inclusive_subtotal ); } return $subtotal; }
3. 给费用总计添加(exclu. tax)标识
add_filter( 'woocommerce_cart_totals_fee_html', 'add_excl_tax_label_to_fee_total', 10, 2 ); function add_excl_tax_label_to_fee_total( $html, $fee ) { $html = str_replace( wc_price( $fee->amount ), wc_price( $fee->amount ) . ' <small>(exclu. tax)</small>', $html ); return $html; }
4. 调整费用显示位置到购物车小计之后
remove_action( 'woocommerce_cart_totals_before_order_total', 'woocommerce_cart_totals_fee_html', 10 ); add_action( 'woocommerce_cart_totals_after_subtotal', 'woocommerce_cart_totals_fee_html', 10 );
注意事项
- 优先使用子主题的
functions.php存放代码,避免主题更新导致自定义内容丢失; - 粘贴代码前建议备份原
functions.php文件,防止语法错误引发网站异常; - 多费用项场景下,第三个函数会自动给所有费用添加标识,无需额外修改。
内容的提问来源于stack exchange,提问作者damien huvelle
相关产品推荐
相关产品推荐

