WooCommerce购物车及结账页移除配送标签仅留金额问题求助
解决WooCommerce购物车/结账页配送金额前冒号问题
问题说明
需要在WooCommerce购物车和结账页面实现:当配送金额完成赋值后,移除配送标签文本,仅保留金额(例如将“flat rate 100.00$”改为“100.00$”)。目前已通过shipping_package_rates_filter_callback函数设置$rates[$targeted_rate_id]->label = '';,但金额前仍会显示多余的冒号(如:100.00$)。
原函数代码如下:
add_filter( 'woocommerce_package_rates', 'shipping_package_rates_filter_callback', 100, 2 ); function shipping_package_rates_filter_callback( $rates, $package ) { // HERE define your targeted rate id $targeted_rate_id = 'flat_rate:4'; if ( ! array_key_exists($targeted_rate_id, $rates) ) return $rates; $new_cost = WC()->session->get('shipping_cost'); // Get new cost from WC sessions if ( ! empty($new_cost) ) { // Set shipping label empty $rates[$targeted_rate_id]->label = ''; $rate = $rates[$targeted_rate_id]; $cost = $rate->cost; // Set the initial cost in a variable for taxes calculations $rates[$targeted_rate_id]->cost = $new_cost; // Set the new cost $has_taxes = false; // Initializing $taxes = array(); // Initializing foreach ($rate->taxes as $key => $tax_cost){ if( $tax_cost > 0 ) { $tax_rate = $tax_cost / $cost; // Get the tax rate conversion $taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array $has_taxes = true; // There are taxes (set it to true) } } if( $has_taxes ) { $rates[$targeted_rate_id]->taxes = $taxes; // Set taxes costs array } } else { unset($rates[$targeted_rate_id]); } return $rates; }
解决方案
出现冒号的原因是WooCommerce默认模板会在配送标签和金额之间固定输出冒号,即使标签为空也会保留。可以通过添加woocommerce_cart_shipping_method_full_label过滤器,直接修改完整的配送显示文本,跳过冒号的输出。
修改后的完整代码
add_filter( 'woocommerce_package_rates', 'shipping_package_rates_filter_callback', 100, 2 ); function shipping_package_rates_filter_callback( $rates, $package ) { // HERE define your targeted rate id $targeted_rate_id = 'flat_rate:4'; if ( ! array_key_exists($targeted_rate_id, $rates) ) return $rates; $new_cost = WC()->session->get('shipping_cost'); // Get new cost from WC sessions if ( ! empty($new_cost) ) { // Set shipping label empty $rates[$targeted_rate_id]->label = ''; $rate = $rates[$targeted_rate_id]; $cost = $rate->cost; // Set the initial cost in a variable for taxes calculations $rates[$targeted_rate_id]->cost = $new_cost; // Set the new cost $has_taxes = false; // Initializing $taxes = array(); // Initializing foreach ($rate->taxes as $key => $tax_cost){ if( $tax_cost > 0 ) { $tax_rate = $tax_cost / $cost; // Get the tax rate conversion $taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array $has_taxes = true; // There are taxes (set it to true) } } if( $has_taxes ) { $rates[$targeted_rate_id]->taxes = $taxes; // Set taxes costs array } } else { unset($rates[$targeted_rate_id]); } return $rates; } // 移除配送金额前的冒号,仅显示金额 add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_shipping_label_colon', 100, 2 ); function remove_shipping_label_colon( $label, $method ) { $targeted_rate_id = 'flat_rate:4'; // 匹配目标配送方式,且标签为空时,直接返回格式化后的金额 if ( $method->id === $targeted_rate_id && empty($method->label) ) { return wc_price( $method->cost + array_sum( $method->taxes ) ); } return $label; }
代码说明
- 保留原有的
shipping_package_rates_filter_callback函数,负责设置配送金额和清空标签 - 添加
remove_shipping_label_colon回调函数,通过woocommerce_cart_shipping_method_full_label过滤器拦截完整的配送显示文本:- 判断当前配送方式是否为目标ID,且标签为空
- 直接返回包含税费的格式化金额,跳过默认的“标签: 金额”结构
- 优先级设为100,确保覆盖WooCommerce的默认输出逻辑
内容的提问来源于stack exchange,提问作者udd_ish
相关产品推荐
相关产品推荐

