pgems sri
pgems sri

Reputation: 63

Allow user only read only access woocommerce

I have completed a woocommerce website for a client. As per the client requirement I got an issue. My client wants their agents just need to able to search for the orders but they should not able to edit any of the order or they should not see any other option. Agents should see all the order details like phone number, email and complete billing address but they should not able to edit anything.

I have tried few user role editors none of them giving that option. Note : All the orders are guest orders only!

Please let me know if you have any idea how to achieve this.

Upvotes: 0

Views: 2048

Answers (1)

mujuonly
mujuonly

Reputation: 11861

Try this in your theme functions.php

//mydomain.com/my-account/view-order/xxxx - any order number here

function my_customer_has_capability( $allcaps, $caps, $args ) {

    if ( isset( $caps[0] ) ) {
        switch ( $caps[0] ) {
            case 'view_order' :
                $user_id = $args[1];
                $order   = wc_get_order( $args[2] );

                if ( $order && $user_id == $order->user_id || my_get_current_user_role() == 'editor') {
                    $allcaps['view_order'] = true;
                }
            break;
        }
    }
    return $allcaps;
}
add_filter( 'user_has_cap', 'my_customer_has_capability', 10, 3 );

function my_get_current_user_role() {
    $current_user = wp_get_current_user();
    if ( !($current_user instanceof WP_User) )
       return;

    if( empty($current_user->roles[0]) )
        return false;

    $role = ($current_user->roles[0]);
    return (string)$role;
}

Upvotes: 1

Related Questions