mohamed adel
mohamed adel

Reputation: 715

WooCommerce edit order privilledge

Hello i made a new role using Ultimate Member named stock_user i want all users in the stock_user role only thing they can do is to change the order status in wooCommerce admin page from processing to shipped only

i tried plugins but nothing worked im sorry i don't have any code but i don't know where to start

can anyone tell me where to start to make this work ?

enter image description here i want to limit this dropdown menu options to processing & shipped only when a user with privildge stock_user is logged in

EDIT 2: Its now working as it should thank to IndexTwo Answer but the problem is that inventory role is a custom role made by Ultimate Member plugin how can i make the status work with it ? here is the code of Indextwo

    function vnm_remove_woo_statuses($statuses) {
    $currentUser = wp_get_current_user();

    $removeStatusesArray = array('pending', 'completed', 'cancelled', 'refunded', 'failed', 'refund-issued', 'shipping-progress', 'on-hold');

    //  If it's inventory user role

    if ( in_array('inventory', $currentUser->roles)) {

        foreach ($removeStatusesArray as $status) {
            if (isset($statuses['wc-' . $status])) {
                unset($statuses['wc-' . $status]);
            }
        }
    }

    return $statuses;
}

add_filter('wc_order_statuses', 'vnm_remove_woo_statuses');

i mean it works when i change inventory to administrator just fine but how to get it working with the custom role (inventory) ? thank you

Upvotes: 0

Views: 64

Answers (1)

indextwo
indextwo

Reputation: 5905

There are a couple of ways to approach the logic on this, but here's the function first:

function vnm_remove_woo_statuses($statuses) {
    $currentUser = wp_get_current_user();

    $removeStatusesArray = array('pending', 'completed', 'cancelled', 'refunded', 'failed', 'refund-issued', 'shipping-progress', 'on-hold');

    //  If it's any role other than administrator, remove the status

    if (!in_array('administrator', $currentUser->roles)) {

        foreach ($removeStatusesArray as $status) {
            if (isset($statuses['wc-' . $status])) {
                unset($statuses['wc-' . $status]);
            }
        }
    }

    //  ALWAYS need to return the status array regardless

    return $statuses;
}

add_filter('wc_order_statuses', 'vnm_remove_woo_statuses', 5, 1);

Note the last line of the function: you always need to return the value of a filter, regardless of the logic therein - otherwise things go wonky. I've also set a high priority of 5 just in case anything else was hooking in.

The conditional logic is currently set to remove statuses if ANY role other than administrator tried to edit an order:

if (!in_array('administrator', $currentUser->roles)) { /* Do stuff */ }

In your example, you were removing the statuses only for administrators. You could switch that around so that it only happens for stock_user users:

if (in_array('stock_user', $currentUser->roles)) { /* Do stuff */ }

...however, be aware that if you have the ability to add multiple roles to a user and add Stock User to an administrator, their access to ordser statuses would also be restricted here.

Upvotes: 1

Related Questions