基于用户角色和小计金额的WooCommerce运输方式

6
我需要根据购买者的角色添加两种不同的运费。我有一个名为'wholesale_customer'的角色,不需要支付运费。然而,如果购物车的小计等于或小于90欧元,必须阻止购买并在结账时添加一条消息。(请记住,您必须达到90欧元的金额)
另一种选择是其他所有WordPress角色,如果他们的购物车小计为40欧元或更少,则需要支付5欧元的运费。
我已经在WooCommerce中配置了运费,并创建了两种最低订单金额的免费运费方式,分别为40欧元和90欧元。
问题是,当具有'wholesale_customer'角色的用户达到40欧元时,他也会启用该免费运费方式,这是不应该发生的,因为对于这个用户角色('wholesale_customer'),只有当购买金额超过90欧元时才免费。
我已经按照以下方式在WooCommerce中配置了运费,创建了2种运费方式: - 一种的“最低所需数量”为90欧元 - 另一种的“最低所需数量”为40欧元 - 运费为0欧元。
我尝试添加以下功能以获得所需结果,但所有的运输方式都一直处于启用状态,因此“最低40欧元免费运输”的零售运输方式也对具有批发客户角色的用户生效。这是不应该发生的,因为具有这个角色的用户应该享受到属于他们的特权。
我展示了一些我用来尝试实现目标的代码,因为我进行了很多测试。只是我还没有找到添加我在演示中提到的文本的方法,以适用于“批发客户”角色。
显示WooCommerce设置的图片。
function custom_shipping_methods_visibility( $available_methods ) {
     
        $subtotal = WC()->cart->subtotal;
        
        $user = wp_get_current_user();
        $user_roles = $user->roles;
        
        // Check user role and cart subtotal to show/hide shipping methods
        if ( in_array( 'wholesale_customer', $user_roles ) && $subtotal <= 70 ) {
            unset( $available_methods['flat_rate:13'] );
        } elseif ( $subtotal <= 30 ) {
            unset( $available_methods['flat_rate:9'] );
        }
        
        return $available_methods;
    }
    add_filter( 'woocommerce_package_rates', 'custom_shipping_methods_visibility', 10, 1 );

////////////////////

// Adds a custom function to filter shipping methods based on cart role and subtotal
    function custom_shipping_methods( $available_methods ) {
        // Get cart subtotal
        $subtotal = WC()->cart->subtotal;
        
        // Gets the role of the current user
        $user = wp_get_current_user();
        $user_roles = $user->roles;
        
        //Check user role and cart subtotal to adjust shipping methods
        if ( in_array( 'wholesale_customer', $user_roles ) && $subtotal < 70 ) {
            foreach ( $available_methods as $method_id => $method ) {
                // Hide the shipping methods if the user is a wholesaler and the subtotal is less than €70
                unset( $available_methods[ $method_id ] );
            }
            
            // Show a warning message
            wc_add_notice( 'Debes alcanzar un mínimo de 70€ en tu carrito para realizar el pedido.', 'error' );
        }
        
        return $available_methods;
    }
    add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 10, 1 );

配置-1

enter image description here

配置-2:

enter image description here

配置-3:

enter image description here

配置-4: 在这里输入图像描述


1
我在快速搜索中找到了这个插件,也许审查一下它会帮助你找到一些重新定位的技巧:https://wordpress.org/plugins/user-role-based-shipping-method/ - admcfajn
1
我能够通过快速搜索找到这个插件,也许对它进行审查会帮助你找到一些重新利用的技巧:https://wordpress.org/plugins/user-role-based-shipping-method/ - admcfajn
1
我能够通过快速搜索找到这个插件,也许审查一下它会帮助你找到一些重新利用的技巧:https://wordpress.org/plugins/user-role-based-shipping-method/ - undefined
谢谢,我现在已经尝试了那个插件,但对我来说并没有起作用。由于某种原因,在设置中它没有显示我在商店中拥有的用户角色。我会继续寻找其他解决方案。 - gemita
谢谢,我已经尝试了那个插件,但对我来说没有起作用。出于某种原因,在设置中没有显示我在商店中的用户角色。我会继续寻找。 - gemita
1个回答

4

更新

首先,为了防止“wholesale_customer”用户在购物车小计低于90欧元时下订单,以下代码将会执行以下操作:

  • 在购物车和结账页面上显示相关提醒通知,
  • 在结账验证过程中显示错误通知,避免任何购买。
// Conditional function: Is user a Wholesale Customer
function is_a_wholesale_customer() {
    if ( ! is_user_logged_in() ) return false;

    return (bool) in_array( 'wholesale_customer', wp_get_current_user()->roles );
}

// Conditional function: Is user a (normal) Customer
function is_a_normal_customer() {
    if ( ! is_user_logged_in() ) return false;

    return (bool) in_array( 'customer', wp_get_current_user()->roles );
}

// Conditional function: Is "wholesale_customer" allowed to order
function is_wholesale_customer_allowed_to_order() {
    // Check "wholesale_customer" user role and required subtotal
    return is_a_wholesale_customer() && WC()->cart->subtotal < 90 ? false : true;
}

// The notice text (for "wholesale_customer")
function wholesale_customer_text_error_notice(){
    return __('Please remember that you must reach an amount of €90', 'woocommerce');
}

// Notice reminder based on required subtotal (for "wholesale_customer")
add_action( 'woocommerce_before_cart', 'check_cart_subtotal_wholesale_customer' ); // cart
add_action( 'woocommerce_before_checkout_form', 'check_cart_subtotal_wholesale_customer' ); // checkout
function check_cart_subtotal_wholesale_customer() {
    if ( ! is_wholesale_customer_allowed_to_order() ) {
        wc_print_notice( wholesale_customer_text_error_notice() );
    }
}
// Checkout validation based on required subtotal (for "wholesale_customer")
add_action( 'woocommerce_checkout_process', 'wholesale_customer_checkout_validation' );
function wholesale_customer_checkout_validation() {
    if ( ! is_wholesale_customer_allowed_to_order() ) {
        wc_add_notice( wholesale_customer_text_error_notice(), 'error' ); // Displays an error notice
    }
}

关于运输方式,您的设置是正确的。
以下代码将根据您的用户角色("wholesale_customer"和"customer"(还包括未登录用户))以及免费运输的最低购物车小计金额来显示/隐藏运输方式。
更新:该代码现在可以与未登录用户一起使用(就像"customer"用户角色一样)。
对于"wholesale_customer",在购物车小计达到90欧元之前,所有运输方式都将被禁用。
当免费运输可用时,所有其他运输方式将被隐藏。
对于您的两种免费运输方式,您需要获取正确的运输费率ID。 在结账页面的运输部分,检查每个显示的免费运输选项的单选按钮(请参见下面的屏幕截图),如果购物车小计超过90欧元。

enter image description here

现在您可以在下面的代码中设置您的两个免费送货方式的费率ID:
// showing / Hidding shipping methods
add_filter( 'woocommerce_package_rates', 'filter_shipping_package_rates', 10, 2 );
function filter_shipping_package_rates( $rates, $package ) {
    // Settings
    $free_rate_ids  = array( 
        'wholesale_customer'   => 'free_shipping:2', // Here set the free shipping rate ID for wholesale user
        'customer_or_unlogged' => 'free_shipping:3', // Here set the free shipping rate ID for customer or unlogged user
    );

    $error_data['all_rates'] = array_keys($rates);
    
    // "Wholesale" user role
    if ( is_a_wholesale_customer() ) {
        $key = 'wholesale_customer';
        // show only "Wholesale" free shipping when available
        if( isset($rates[$free_rate_ids[$key]]) ) {
            return array( $free_rate_ids[$key] => $rates[$free_rate_ids[$key]] );
        } 
        // Hide all shipping methods (no free shipping available)
        else {
            return array();
        }
    } 
    // "Customer" user role or unlogged users
    else {
        $key = 'customer_or_unlogged';
        // show only "Normal" free shipping when available
        if( isset($rates[$free_rate_ids[$key]]) ) {
            return array( $free_rate_ids[$key] => $rates[$free_rate_ids[$key]] );
        } 
    } 
    return $rates;
}

代码放在活动子主题(或活动主题)的functions.php文件中。已测试并有效。

保存代码后,为了刷新运输方式的缓存数据,别忘了清空购物车(或在Woocommerce运输设置中禁用、保存和启用与当前运输区域相关的运输方式)


经过几天的时间,我收到了批发客户的消息,他们说他们的订单没有送达,并且他们操作没有错误信息。但是这些订单并没有出现在WooCommerce中。 - gemita
1
也许你们公司应该雇佣一位有技能的人来审计所有内容,并彻底整理一次。 - LoicTheAztec
1
也许你们公司应该雇佣一位有经验的人来审计一切,并彻底整顿一次。 - LoicTheAztec
你说得对,感谢你的专注和评论 @LoicTheAztec - gemita
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接