Hide specific shipping methods for specific custom

2019-08-18 00:23发布

问题:

I use the Wholesale Suite Premium Prices plugin with WooCommerce. I have 6 specific wholesale roles out of 15 that I wish to hide two specific shipping methods from being selected for the 6 exceptions. I'm just trying this on my staging server at this time using code example that I found and modified to my specific conditions. Would the following work for this purpose? Any insights as to how to accomplish this would be appreciated. Lyse

/* Hide specific shipping methods for specific wholesale roles */
add_filter( 'woocommerce_package_rates', function( $shipping_rates ) {
// User role and shipping method ID to hide for the user role
    $role_shipping_method_arr = array(
    'ws_silvia_silver' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_pst_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_tax_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_silver' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_pst_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_tax_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_gold' =>    array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_pst_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_tax_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_gold' =>    array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_pst_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_tax_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    );

    // Getting the current user role
    $curr_user = wp_get_current_user(); 
    $curr_user_data = get_userdata($current_user->ID); 

    // Wholesale Suite Roles 
    if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
        $wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
        $wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole(); 

// Loop through the user role and shipping method pair
    foreach( $role_shipping_method_arr as $role => $shipping_methods_to_hide ) {

// Check if defined role exist in current user role or not
    if( in_array( $role, $current_user->roles) ) {

// Loop through all the shipping rates
    foreach( $shipping_rates as $shipping_method_key => $shipping_method ) {
$shipping_id = $shipping_method->get_id();

// Unset the shipping method if found
    if( in_array( $shipping_id, $shipping_methods_to_hide) ) {
unset($shipping_rates[$shipping_method_key]);
}
}
}
}
}
    return $shipping_rates;
});