Код: Выделить всё
add_filter('woocommerce_rest_pre_dispatch', 'restrict_api_access_based_on_user_meta', 10, 3);
function restrict_api_access_based_on_user_meta($result, $server, $request) {
// Check if the request is for the WooCommerce products endpoint
if (strpos($request->get_route(), '/wc/v3/products') !== false) {
// Retrieve user ID from the authentication information
$user_id = apply_filters('determine_current_user', null);
// Check if the user is authenticated and retrieve the 'disable_account_api' meta value
if ($user_id) {
$disable_account_api = get_user_meta($user_id, 'disable_account_api', true);
// If 'disable_account_api' is set to "yes", deny access
if ($disable_account_api === 'yes') {
return new WP_Error(
'api_disabled',
__('API access has been disabled for this account.', 'woocommerce'),
array('status' => 403)
);
}
}
}
return $result;
}

Подробнее здесь: https://stackoverflow.com/questions/791 ... -for-users