为 "wholesale_customer" 以外的所有人禁用本地递送
Disable Local Delivery for everyone except "wholesale_customer"
好吧,过去几天我一直在努力解决这个问题。
我在 woocommerce 的 wordpress 安装中有一个批发插件。它为用户提供了 "wholesale_customer" 高于其他人的特价。我希望能够仅向 "wholesale_customer" 用户角色提供本地交付,但似乎不知道该怎么做。
我从@mcorkum 那里得到了这段代码,但它仍然无法正常工作。
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
我知道这可以通过插件实现,但我宁愿不使用插件或为此付费。
有人看到我没看到的东西吗?
我不是 wordpress 开发人员,但该代码看起来不像是为用户提供角色 "wholesale_customer" "local_delivery" 选项。相反,实际上,如果用户角色是 "wholesale_customer":
,它似乎正在删除本地交付选项
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
如果我只是从表面上看这段代码(因为我不是 wordpress 开发人员),我会重写这个函数以便更容易理解和阅读:
function wholesale_local_delivery($available_methods)
{
global $woocommerce;
global $current_user;
// Return early if no local delivery option is available
if (!isset($available_methods['local_delivery'])) {
return $available_methods;
}
// Determine if the user has a user role of wholesale customer
$hasRoleWholeSaleCustomer = false;
foreach ($current_user->roles as $role) {
if ($role === 'wholesale_customer') {
$hasRoleWholeSaleCustomer = true;
break;
}
}
// If the user does not have the role wholesale customer
// And for the code here to be being processed the local delivery
// option must be available
if (!$hasRoleWholeSaleCustomer) {
unset($available_methods['local_delivery']);
}
// Return the available methods applicable to the users roles
return $available_methods;
}
希望有 woocomerce 经验的其他人可以给出更好的答案。但与此同时,您可以尝试重新编写,看看它是否适合您。
祝你好运。
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
if ( isset( $available_methods['local_delivery'] ) ) {
if ( !current_user_can( 'wholesale_customer' ) ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
将上面的代码粘贴到主题的 functions.php 文件中试试。如果这对你有用,请告诉我。
好吧,过去几天我一直在努力解决这个问题。
我在 woocommerce 的 wordpress 安装中有一个批发插件。它为用户提供了 "wholesale_customer" 高于其他人的特价。我希望能够仅向 "wholesale_customer" 用户角色提供本地交付,但似乎不知道该怎么做。
我从@mcorkum 那里得到了这段代码,但它仍然无法正常工作。
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
我知道这可以通过插件实现,但我宁愿不使用插件或为此付费。
有人看到我没看到的东西吗?
我不是 wordpress 开发人员,但该代码看起来不像是为用户提供角色 "wholesale_customer" "local_delivery" 选项。相反,实际上,如果用户角色是 "wholesale_customer":
,它似乎正在删除本地交付选项if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
如果我只是从表面上看这段代码(因为我不是 wordpress 开发人员),我会重写这个函数以便更容易理解和阅读:
function wholesale_local_delivery($available_methods)
{
global $woocommerce;
global $current_user;
// Return early if no local delivery option is available
if (!isset($available_methods['local_delivery'])) {
return $available_methods;
}
// Determine if the user has a user role of wholesale customer
$hasRoleWholeSaleCustomer = false;
foreach ($current_user->roles as $role) {
if ($role === 'wholesale_customer') {
$hasRoleWholeSaleCustomer = true;
break;
}
}
// If the user does not have the role wholesale customer
// And for the code here to be being processed the local delivery
// option must be available
if (!$hasRoleWholeSaleCustomer) {
unset($available_methods['local_delivery']);
}
// Return the available methods applicable to the users roles
return $available_methods;
}
希望有 woocomerce 经验的其他人可以给出更好的答案。但与此同时,您可以尝试重新编写,看看它是否适合您。
祝你好运。
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
if ( isset( $available_methods['local_delivery'] ) ) {
if ( !current_user_can( 'wholesale_customer' ) ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
将上面的代码粘贴到主题的 functions.php 文件中试试。如果这对你有用,请告诉我。