按角色限制woocommerce订单状态 [英] Restrict woocommerce order status by role

查看:67
本文介绍了按角色限制woocommerce订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个工作流程,商店经理可以在其中创建订单并将其标记为待付款"、处理中",但只有管理员才能将订单标记为完成"、失败"等.

I'm trying to make a workflow where shop managers can create orders and mark them as "pending payment", "processing" but only admins can mark orders as "complete", "failed" etc.

我发现的最接近的是这篇文章:

<?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.我最好的猜测是:

This should work for regular WP posts (although I haven't tested it) but I'm not sure how to apply to Woocommerce. My best guess is:

<?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(); ?>

但是我遇到了各种各样的错误!我也不确定它是否仅适用于编辑订单屏幕,而不适用于管理车间订单表批量操作下拉菜单.

But I'm getting all sorts of errors with this! I'm also not sure if it would only apply to the edit order screen and not the admin shop order table bulk actions dropdown.

任何帮助将不胜感激!

推荐答案

条件函数 current_user_can() 不推荐用于用户角色:

The conditional function current_user_can() is not recommended with user roles:

虽然部分支持检查特定角色代替能力,但不鼓励这种做法,因为它可能会产生不可靠的结果.

While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

相反,您可以获得当前用户及其角色(因为一个用户可以有多个).此外,woocommerce 中的订单发布状态非常具体(它们都以 wc- 开头,如果很多,它们应该在一个数组中).

Instead you can get the current user and his roles (as a user can have many). Also the orders post status are very specific in woocommerce (they all begin by wc- and they should be in an array if many).

所以正确的代码应该是:

So the correct code should be:

<?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();
?>

经过测试并有效

这篇关于按角色限制woocommerce订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆