修改WooCommerce购物车与结账页小计:乘10并移除指定行
解决方案:通过functions.php实现WooCommerce购物车/结账页修改
1. 修改小计金额(乘以10)并替换文本为TOTAL
将以下代码添加到主题的functions.php文件中,代码会同时作用于购物车和结账页面:
// 修改购物车页面小计:金额×10 + 替换文本为TOTAL add_filter( 'woocommerce_cart_totals_subtotal_html', 'custom_cart_subtotal_display' ); function custom_cart_subtotal_display( $html ) { // 获取插件返回的10%定金金额 $subtotal = WC()->cart->get_subtotal(); // 计算全额总价 $full_total = $subtotal * 10; // 按WooCommerce默认格式格式化金额 $formatted_total = wc_price( $full_total ); // 返回替换后的HTML结构 return '<tr class="cart-subtotal"> <th>' . __( 'TOTAL', 'woocommerce' ) . '</th> <td data-title="' . __( 'TOTAL', 'woocommerce' ) . '">' . $formatted_total . '</td> </tr>'; } // 修改结账页面订单小计:金额×10 + 替换文本为TOTAL add_filter( 'woocommerce_get_order_item_totals', 'custom_checkout_subtotal_display', 10, 3 ); function custom_checkout_subtotal_display( $total_rows, $order, $tax_display ) { if ( isset( $total_rows['cart_subtotal'] ) ) { $subtotal = $order->get_subtotal(); $full_total = $subtotal * 10; // 更新行的标签和金额 $total_rows['cart_subtotal']['label'] = __( 'TOTAL', 'woocommerce' ); $total_rows['cart_subtotal']['value'] = wc_price( $full_total ); } return $total_rows; }
2. 移除无用行(以WooCommerce Deposit插件添加的定金行为例)
如果无用行是插件生成的定金相关行(比如“Deposit”“剩余余额”),可通过以下代码移除:
// 移除购物车页面的无用行 add_action( 'wp_footer', 'remove_unnecessary_cart_rows' ); function remove_unnecessary_cart_rows() { if ( is_cart() ) { ?> <script> jQuery(document).ready(function($) { // 根据实际行的文本调整选择器,匹配要移除的行 $('.cart_totals tr:contains("Deposit")').remove(); $('.cart_totals tr:contains("Remaining Balance")').remove(); }); </script> <?php } } // 移除结账页面的无用行 add_filter( 'woocommerce_get_order_item_totals', 'remove_unnecessary_checkout_rows', 20, 3 ); function remove_unnecessary_checkout_rows( $total_rows, $order, $tax_display ) { // 根据实际行的键名调整,移除对应行 unset( $total_rows['deposit'] ); unset( $total_rows['remaining_balance'] ); return $total_rows; }
提示:如果无用行的文本或键名和示例不符,可以通过浏览器开发者工具查看页面HTML结构,调整代码中的选择器或键名。
注意事项
- 修改
functions.php前务必备份文件,避免代码错误导致网站异常。 - 优先使用子主题的
functions.php添加代码,防止主题更新时代码丢失。 - 修改后测试购物车和结账页面,确认金额计算、文本替换、行移除均生效。
内容的提问来源于stack exchange,提问作者Marco Bagiacchi
相关产品推荐
相关产品推荐

