1

I'm trying to restrict products from a certain category from editing for a specific user role. Any idea how to get the catgories IDs of a product in ...wp-admin/post.php?post=1702&action=edit ?

Here's my code so far:

function restrict_edit_if_in_manufacturing_cat($post){
    global $pagenow, $post_type;

    // Targeting admin edit single product screen
    if ($pagenow === 'post.php' && $post_type === 'product') {

        // Get current user
        $user = wp_get_current_user();

        // Roles
        $roles = (array) $user->roles;

        // Roles to check
        $roles_to_check = array('shop_manager'); // several can be added, separated by a comma

        // Compare
        $compare = array_diff($roles, $roles_to_check);

        // Result is empty
        if (empty($compare)) {

            // $category_ids = ...Get Category IDs here...

            // If 458 is in the category id list die
            if (strpos($category_ids, '458') !== false) {

                wp_die('cheatn');
            } else {

                // do something if needed

            }
        }
    }
}
add_action('pre_get_posts', 'restrict_edit_if_in_manufacturing_cat', 10, 1);
Ruvee
  • 8,611
  • 4
  • 18
  • 44
evavienna
  • 1,059
  • 11
  • 29

1 Answers1

3

You could use get_the_terms function to get the categories for the current post and then, get their ids, like this:

add_action('pre_get_posts', 'restrict_edit_if_in_manufacturing_cat');

function restrict_edit_if_in_manufacturing_cat($post)
{

    global $pagenow, $post_type;

    if ($pagenow === 'post.php' && $post_type === 'product') 
    {

        $current_user = wp_get_current_user();

        $user_roles = $current_user->roles;

        if (in_array("shop_manager", $user_roles)) 
        {

            $categories_for_the_current_post = get_the_terms($_GET['post'], 'product_cat');

            $categories_ids = array();
            foreach ($categories_for_the_current_post as $category_object) {
                array_push($categories_ids, $category_object->term_id);
            }

            if (in_array('458', $categories_ids)) 
            {

                wp_die('cheatn');

            } else {

                // do something if needed
            }
        }
    }
}

This snippet has been tested on the standard woo installation 5.7.1 and works fine.

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • This is not what i asked for - maybe i was not clear enough - if the current product belongs to category 458... not if the product id is 458. And the get_the_category retunrs nothing... thx – evavienna Dec 17 '21 at 18:11
  • Just updated my answer, please give it another shot! – Ruvee Dec 17 '21 at 18:21
  • doesnt work - any way to echo the category ids so i can check them – evavienna Dec 17 '21 at 18:28
  • Well, I just tested on my woo installation and it works fine on my end. Yes you could check the ids by using `die(print_r($categories_ids));` right above this line: `if (in_array('458', $categories_ids))` – Ruvee Dec 17 '21 at 18:30
  • If you're checking it with your admin account then, you would need to change `if (in_array("shop_manager", $user_roles))` to `if (in_array("administrator", $user_roles))` – Ruvee Dec 17 '21 at 18:33
  • found the problem - maybe there is a way to fix it. Im hiding the categories with https://stackoverflow.com/a/70390069/9036446 - so the array prints 'Array ( ) 1' instead of 'Array ( [0] => 459 [1] => 460 [2] => 458 ) 1' – evavienna Dec 17 '21 at 19:07
  • your code works fine if it gets the category ids - but i am hiding them too with the code from the link. There should be another way to get the ids... any idea? thx – evavienna Dec 17 '21 at 19:15