隐藏 Woocommerce 中特定用户角色的特定运输方式
在 Woocommerce 中,我使用 WooCommerce Wholesale Pro Suite (来自 IgniteWoo) 和 Flat Rate Box Shipping 插件将 B2B 添加到我们的电子商店.
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;
}
我应该用什么来代替wholesale_customer
"和table_rate_shipping_free-shipping
",这样对于客人和客户角色?
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?
感谢任何帮助.
更新 2:
您可能需要在运输选项"标签下的一般运输设置中启用调试模式",禁用临时运输缓存.
You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.
有关信息:统一费率箱"的送货方式 ID 为 flat_rate_boxes
.
For info: The shipping method ID for "Flat rate boxes" is flat_rate_boxes
.
以下代码将禁用Guests"(非登录用户)和customer"用户角色的Flat rate box"运输方式:
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;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
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.