dow5001 2018-10-10 18:34
浏览 46
已采纳

隐藏Woocommerce中特定用户角色的特定送货方式

In Woocommerce I am using WooCommerce Wholesale Pro Suite (from IgniteWoo) and Flat Rate Box Shipping plugins to add B2B to our eshop.

I am trying to disable the Flat Rate Box Shipping for specific user roles, guests and customers. I found this code after searching online:

add_filter( 'woocommerce_package_rates', 'hide_shipping_for_user_role', 10, 2 );
function hide_shipping_for_user_role( $rates, $package ) {
// Role ID to be excluded
$excluded_role = "wholesale_customer";

// Shipping rate to be excluded
$shipping_id = 'table_rate_shipping_free-shipping';

// Get current user's role
$user = wp_get_current_user();
if ( empty( $user ) ) return false;

if( in_array( $excluded_role, (array) $user->roles ) && isset( $rates[ $shipping_id ] ) )
unset( $rates[ $shipping_id ] );

return $rates;
}

What should I use in place of "wholesale_customer" and in place of "table_rate_shipping_free-shipping", so the Flat Rate Box Shipping is not showing, for guests and customers roles?

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dqvzfp6468 2018-10-10 19:04
    关注

    Update 2:

    You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

    For info: The shipping method ID for "Flat rate boxes" is flat_rate_boxes.

    The following code will disable "Flat rate boxes" Shipping Methods For "Guests" (non logged in users) and "customer" user role:

    add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_method_based_on_user_role', 30, 2 );
    function hide_specific_shipping_method_based_on_user_role( $rates, $package ) {
    
        ## --- Your settings --- ##
        $excluded_role = "customer"; // User role to be excluded
        $shipping_id = 'flat_rate_boxes'; // Shipping rate to be removed
    
        foreach( $rates as $rate_key => $rate ){
            if( $rate->method_id === $shipping_id ){
                if( current_user_can( $excluded_role ) || ! is_user_logged_in() ){
                    unset($rates[$rate_key]);
                    break;
                }
            }
        }
        return $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Don't forget to enable back shipping cache.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向