You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce购物车零运费商品显示Free Shipping问题排查

解决方案

针对Webiwork Shipping Per Product WooCommerce插件,要在购物车中为设置了零运费的商品显示“Free Shipping”提示,你需要直接读取每个商品的单品运费设置,而非通过包裹运费总额判断。原代码的核心问题是它检查的是整个订单包裹的运费总和,而非单个商品的运费配置。

以下是修正后的代码:

add_action('woocommerce_after_cart_item_name', 'display_free_shipping_for_cart_items', 20, 2);
function display_free_shipping_for_cart_items($cart_item, $cart_item_key) {
    // 获取当前购物车项对应的商品对象
    $product = $cart_item['data'];
    
    // 获取Webiwork插件存储的单品运费(元键需根据插件实际情况调整,此处为默认常用键名)
    $shipping_cost = get_post_meta($product->get_id(), '_web_shipping_per_product_cost', true);
    
    // 处理变体商品:若变体未单独设置运费,读取父商品的运费配置
    if (empty($shipping_cost) && $product->is_type('variation')) {
        $shipping_cost = get_post_meta($product->get_parent_id(), '_web_shipping_per_product_cost', true);
    }
    
    // 当运费为0或未设置时,显示免运费提示
    if (empty($shipping_cost) || floatval($shipping_cost) === 0.0) {
        echo '<p class="product-shipping-cost" style="color: #2ecc71; margin: 5px 0 0;">Free Shipping</p>';
    }
}

关键细节说明:

  • 元键调整:如果_web_shipping_per_product_cost不是插件实际使用的元键,可通过以下方式确认:
    • 进入WordPress后台编辑商品,用浏览器开发者工具查看页面隐藏字段的名称
    • 直接在数据库wp_postmeta表中查找对应商品ID的运费存储键名
  • 样式自定义:代码中内置了基础样式,你可以修改style属性内容,或通过主题CSS文件设置.product-shipping-cost类的样式
  • 变体商品兼容:自动处理变体商品,若变体未单独设置运费,会继承父商品的运费配置

测试步骤:

  1. 将代码添加到主题functions.php文件或使用代码片段插件
  2. 添加零运费商品到购物车,确认提示正常显示
  3. 添加非零运费商品,确认无额外提示出现

内容的提问来源于stack exchange,提问作者HGiri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:35:04