Woocommerce强制免费送货(如果您是管理员)

我目前对75美元以上的商品免费送货,但是如果管理员登录并下订单。我希望管理员可以免费送货,无论购物车的运费不超过$ 75。我一直在尝试使用以下代码,但并未触发它。我在做错什么吗?

add_filter( 'woocommerce_package_rates','show_free_shipping',10,2 );
function show_free_shipping( $rates,$package ) {
    global $current_user;
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            if ($current_user->ID) {
                $user_roles = $current_user->roles;
                $user_role = array_shift($user_roles);

                if ($user_role == 'administrator') {
                    $free[ $rate_id ] = $rate;
                    break;
                }
            }
        }
    }
    return ! empty( $free ) ? $free : $rates;
    //return $rates;
}
lyx_hongli 回答:Woocommerce强制免费送货(如果您是管理员)

在将免费送货设置为“不可用”后使用了过滤器woocommerce_package_rates,因为未达到最小值。

要使免费送货再次可用,您可以使用过滤器woocommerce_shipping_free_shipping_is_available
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Shipping_Free_Shipping.html#200-202

尝试:

add_filter("woocommerce_shipping_free_shipping_is_available",function ($is_available,$package,$method)
{


    $current_user = wp_get_current_user();

    if (in_array("administrator",$current_user->roles)) {
        $is_available = TRUE;
    }


    return $is_available;

},10,3);
本文链接:https://www.f2er.com/3111605.html

大家都在问