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

如何通过WooCommerce订阅自动续订钩子将所有续订订单运费设为$0?

修改WooCommerce订阅自动续订订单运费为$0的钩子方案

当然有对应的钩子可以实现这个需求,以下是几种可靠的实现方式:

1. 在续订订单创建时直接设置运费

使用woocommerce_subscription_renewal_order_created钩子,这个钩子会在自动续订订单生成后触发,可直接修改订单的运费金额:

add_action( 'woocommerce_subscription_renewal_order_created', 'set_renewal_order_shipping_to_zero', 10, 2 );
function set_renewal_order_shipping_to_zero( $renewal_order, $subscription ) {
    // 确认是自动续订订单
    if ( $renewal_order->get_meta( '_subscription_renewal' ) ) {
        // 设置运费总额为0
        $renewal_order->set_shipping_total( 0 );
        // 设置运费税额为0(按需启用)
        $renewal_order->set_shipping_tax( 0 );
        // 保存订单更改
        $renewal_order->save();
    }
    return $renewal_order;
}

2. 通过过滤钩子动态调整运费计算

如果需要在订单计算阶段就修改运费,可使用woocommerce_order_shipping_total过滤钩子,针对性调整续订订单的运费:

add_filter( 'woocommerce_order_shipping_total', 'filter_renewal_order_shipping_total', 10, 2 );
function filter_renewal_order_shipping_total( $shipping_total, $order ) {
    // 判断当前订单是否为订阅续订订单
    if ( $order->get_meta( '_subscription_renewal' ) ) {
        $shipping_total = 0;
    }
    return $shipping_total;
}

注意事项

  • 代码可添加至主题的functions.php文件或自定义插件中
  • 建议先在测试环境验证效果,避免影响生产订单
  • 若订阅有特殊运费规则,可额外添加产品类型、订阅属性等判断条件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 02:31:16