WooCommerce开发:结账页替换运费显示为指定文本的钩子需求
WooCommerce结账页面配送标签修改方案
实现需求的核心代码
不用纠结WC()->cart->add_fee(它本来就只支持数值),直接通过WooCommerce的显示钩子就能完成需求:
1. 修改配送方法标签并隐藏金额
利用woocommerce_cart_shipping_method_full_label钩子替换配送方法的完整显示内容,拼接提示文本并去掉金额部分:
add_filter('woocommerce_cart_shipping_method_full_label', 'add_shipping_note_remove_amount', 10, 2); function add_shipping_note_remove_amount($label, $method) { // 组合提示文本与配送方法名称 return 'Contact for shipping quotes - ' . $method->get_name(); }
2. 隐藏结账总计区域的运费数值
如果购物车/结账页面的总计栏仍显示运费金额,用woocommerce_get_order_item_totals钩子替换金额内容:
add_filter('woocommerce_get_order_item_totals', 'hide_shipping_total_amount', 10, 3); function hide_shipping_total_amount($total_rows, $order, $tax_display) { if (isset($total_rows['shipping'])) { // 将运费金额替换为空或自定义占位符 $total_rows['shipping']['value'] = ''; // 也可以直接修改总计栏的标签为提示文本: // $total_rows['shipping']['label'] = 'Contact for shipping quotes'; } return $total_rows; }
注意事项
- 代码可直接放入你的插件主文件,或当前主题的
functions.php(优先放插件,避免主题更新丢失代码) - 若仅需对特定配送方法生效,可在函数内添加判断:比如
if ($method->id === 'flat_rate') { ... } - 测试前记得清空WooCommerce缓存,避免旧内容残留
内容的提问来源于stack exchange,提问作者Rupinder Singh
相关产品推荐
相关产品推荐

