按角色限制woocommerce订单状态
我试图制定一个工作流程,店铺经理可以创建订单并将其标记为“待处理付款”,“处理”但只有管理员可以将订单标记为“完成”,“失败”等。按角色限制woocommerce订单状态
我发现的最接近的是this post:
<?php
if (current_user_can(! 'administrator')) {
$args = array('post_type' => 'post', 'post_status' => 'publish, pending,
draft');
} else {
$args = array('post_type' => 'post', 'post_status' => 'publish');
}
$wp_query = new WP_Query($args); while (have_posts()) : the_post(); ?>
CONTENT
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
这应该定期WP岗位工作(虽然我没有测试过),但我不知道如何应用到Woocommerce。我最好的猜测是:
<?php
if (current_user_can(! 'administrator')) {
$args = array('post_type' => 'shop_order', 'order_status' => 'complete,failed');
} else {
$args = array('post_type' => 'shop_order', 'post_status' => 'pending-payment,processing');
}
$wp_query = new WP_Query($args); while (have_posts()) : the_post(); ?>
CONTENT
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
但我得到各种各样的错误!我也不确定它是否仅适用于修改订单屏幕,而不适用于管理商店订单表批量操作下拉菜单。
任何帮助将非常感谢!
有条件的功能current_user_can()
不推荐使用用户角色:
虽然代替能力的检查,对特定角色的部分支持,这种做法是不鼓励,因为它可能会产生不可靠的结果。
相反,您可以获取当前用户和他的角色(因为用户可以有很多)。 此外,订单发布状态在woocommerce中非常具体(它们全部以wc-
开头,如果有多个,它们应该位于数组中)。
所以正确的代码应该是:
<?php
// get current user roles (if logged in)
if(is_user_logged_in()){
$user = wp_get_current_user();
$user_roles = $user->roles;
} else $user_roles = array();
// GET Orders statuses depending on user roles
if (in_array('shop_manager', $user_roles)) { // For "Shop Managers"
$statuses = array('wc-pending','wc-processing');
} elseif (in_array('administrator', $user_roles)) { // For admins (all statuses)
$statuses = array_keys(wc_get_order_statuses());
} else
$statuses = array();
$loop = new WP_Query(array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => $statuses
));
if ($loop->have_posts()):
while ($loop->have_posts()):
$loop->the_post();
?>
<?php echo $loop->post->ID .', '; // Outputting Orders Ids (just for test) ?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
测试和工程
感谢Loic!我测试了代码减去 post-> ID测试。 它似乎没有对状态产生任何影响 - 我检查的所有角色仍然可以编辑所有状态。不知道我遇到了什么问题,但我不明白代码的哪一部分会限制用户的状态。 谢谢 – charliechina
@charliechina好的我已经做了一个小小的更新,请参阅以下行:'//取决于用户角色的GET订单状态... ...那里是有条件地启用有关用户角色的订单状态的位置。 – LoicTheAztec
你给进入WP管理仪表板,除了管理员角色?或者您正在创建任何前端页面来列出所有订单和状态更新? – Sourav