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

WooCommerce结账勾选寄往不同地址时为team/team2角色加收5美元费用

调整后完整实现代码

核心修改是在费用计算环节新增了用户角色校验逻辑,只有team、team2角色的用户勾选「寄往不同地址」时才会加收5美元运费,其余用户所有场景都不会触发该规则:

// 移除结账页「寄往不同地址」字段的optional标识
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
    // 仅在结账页处理对应字段
    if( 'ship_to_different_address' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// 勾选地址切换时触发Ajax更新
add_action( 'wp_footer', 'ship_to_different_address_script' );
function ship_to_different_address_script() {
    // 仅在结账页加载脚本
    if( ( is_checkout() && ! is_wc_endpoint_url() ) ) :
        ?>
        <script type="text/javascript">
            jQuery( function($){
                if (typeof woocommerce_params === 'undefined')
                    return false;

                $('input[name=ship_to_different_address]').click( function(){
                    var fee = $(this).prop('checked') === true ? '1' : '';

                    $.ajax({
                        type: 'POST',
                        url: woocommerce_params.ajax_url,
                        data: {
                            'action': 'ship_to_different_address',
                            'ship_to_different_address': fee,
                        },
                        success: function (result) {
                            $('body').trigger('update_checkout');
                        },
                    });
                });
            });
        </script>
    <?php
    endif;
}

// 处理Ajax请求,将地址切换状态存入WC session
add_action( 'wp_ajax_ship_to_different_address', 'get_ajax_ship_to_different_address' );
add_action( 'wp_ajax_nopriv_ship_to_different_address', 'get_ajax_ship_to_different_address' );
function get_ajax_ship_to_different_address() {
    if ( isset($_POST['ship_to_different_address']) ) {
        WC()->session->set('ship_to_different_address', ($_POST['ship_to_different_address'] ? '1' : '0') );
        echo WC()->session->get('ship_to_different_address');
    }
    die();
}

// 按规则增减自定义运费
add_action( 'woocommerce_cart_calculate_fees', 'add_remove_ship_to_different_address', 10, 1 );
function add_remove_ship_to_different_address( $cart )
{
    // 后台请求/购物车页直接跳过
    if ((is_admin() && !defined('DOING_AJAX')) || is_cart())
        return;

    // 校验用户角色,仅team、team2角色触发规则
    $current_user = wp_get_current_user();
    $allowed_roles = ['team', 'team2'];
    if (empty($current_user->roles) || !array_intersect($allowed_roles, $current_user->roles)) {
        return;
    }

    $fee_amount = 5.00;
    // 勾选寄往不同地址则加费
    if (WC()->session->get('ship_to_different_address')) {
        $cart->add_fee(__('异地配送费', 'woocommerce'), $fee_amount);
    }
}

测试前请清空WooCommerce缓存、用户session以及浏览器缓存,避免旧数据导致规则不生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 03:06:04