在 Woocommerce 中,我使用的是 WooCommerce Wholesale Pro Suite(来自IgniteWoo)以及统一费率盒子运输插件,将 B2B 添加到我们的网上商店。
我正在尝试禁用特定用户角色、客人和客户的统一费率包装盒运输。我在网上搜索后发现了这段代码:
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
”,那么对于客人和顾客的角色来说,统一费率盒子运输没有显示?
任何帮助表示赞赏。
更新2:
你可能必须“启用调试模式“在常规运输设置中”送货选项" tab, 暂时禁用传送缓存。
欲了解信息:“统一费率包装盒”的运输方式 ID 是flat_rate_boxes
.
以下代码将禁用“客人”的“统一费率盒”运送方式(未登录用户)和“客户”用户角色:
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 文件中。经过测试并有效。
不要忘记启用回运缓存。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)