Meerdere categorieën restrictie
August 7, 2025 · AI Conversations
function restrict_order_access_based_on_category() {
if (!is_admin()) return;
$screen = get_current_screen();
if ($screen->id !== 'shop_order') return;
if (!isset($_GET['post']) || get_post_type($_GET['post']) !== 'shop_order') return;
$order_id = absint($_GET['post']);
if (!$order_id) return;
// 🔒 Category to restrict
$restricted_category_slug = 'bijna-verboden'; // Change this
$allowed_usernames = ['prof_nethues']; // Add your allowed usernames here
$current_user = wp_get_current_user();
$order = wc_get_order($order_id);
if (!$order) return;
foreach ($order->get_items() as $item) {
$product = $item->get_product();
if (!$product) continue;
$parent_id = $product->is_type('variation') ? $product->get_parent_id() : 0;
if (
(has_term($restricted_slug, 'product_cat', $product_id) && !in_array($current_user->user_login, $allowed_usernames)) ||
($parent_id && has_term($restricted_slug, 'product_cat', $parent_id) && !in_array($current_user->user_login, $allowed_usernames))
) {
// 🚫 Redirect unauthorized admin
wp_redirect(admin_url('admin.php?page=unauthorized')); // Change URL
exit;
}
}
}
allow for multiple category
1. Replace the single `$restricted_category_slug` with an **array of slugs**.
2. Use `has_term( array, 'product_cat', $product_id )` to check against multiple categories.
Here is the updated version of your code with support for **multiple restricted categories**:
add_action('current_screen', 'restrict_order_access_based_on_category');
function restrict_order_access_based_on_category() {
if (!is_admin()) return;
$screen = get_current_screen();
if ($screen->id !== 'shop_order') return;
if (!isset($_GET['post']) || get_post_type($_GET['post']) !== 'shop_order') return;
$order_id = absint($_GET['post']);
if (!$order_id) return;
// 🔒 Categories to restrict
$restricted_category_slugs = ['bijna-verboden', 'another-category']; // Add your category slugs
$allowed_usernames = ['prof_nethues']; // Add your allowed usernames here
$current_user = wp_get_current_user();
$order = wc_get_order($order_id);
if (!$order) return;
foreach ($order->get_items() as $item) {
$product = $item->get_product();
if (!$product) continue;
$product_id = $product->get_id();
$parent_id = $product->is_type('variation') ? $product->get_parent_id() : 0;
// Check if product or parent product belongs to restricted categories
if (
(has_term($restricted_category_slugs, 'product_cat', $product_id) && !in_array($current_user->user_login, $allowed_usernames)) ||
($parent_id && has_term($restricted_category_slugs, 'product_cat', $parent_id) && !in_array($current_user->user_login, $allowed_usernames))
) {
// 🚫 Redirect unauthorized admin
wp_redirect(admin_url('admin.php?page=unauthorized')); // Change URL to your custom error/notice page
exit;
}
}
}
### ✅ Notes:
– You can add more slugs to the array: `['slug1', 'slug2', 'slug3']`.
– Make sure your "unauthorized" page (`admin.php?page=unauthorized`) exists or handle the redirect appropriately.
Let me know if you'd like this check to apply on the order list view or restrict editing or viewing order details in other ways too.