For Virtual products only see the update at the end
This is possible but quiet long and complicated to test and explain... You will have to target this users in a condition by 2 ways, a specific user role or a specific capability of their user role.
Then is possible to hide some settings with injected CSS and the use of javascript/jQuery to set this hidden settings...
In the working example below, I enable the 'virtual'
and 'downloadable'
settings cheeckboxes with jQuery and I hide them mostly totally with opacity CSS rule…
I use a custom function hooked in woocommerce_product_options_general_product_data
action hook, this way:
add_action( 'woocommerce_product_options_general_product_data', 'hiding_and_set_product_settings' );
function hiding_and_set_product_settings(){
## ==> Set HERE your targeted user role:
$targeted_user_role = 'administrator';
// Getting the current user object
$user = wp_get_current_user();
// getting the roles of current user
$user_roles = $user->roles;
if ( in_array($targeted_user_role, $user_roles) ){
## CSS RULES ## (change the opacity to 0 after testing)
// HERE Goes OUR CSS To hide 'virtual' and 'downloadable' checkboxes
?>
<style>
label[for="_virtual"], label[for="_downloadable"]{ opacity: 0.2; /* opacity: 0; */ }
</style>
<?php
## JQUERY SCRIPT ##
// Here we set as selected the 'virtual' and 'downloadable' checkboxes
?>
<script>
(function($){
$('input[name=_virtual]').prop('checked', true);
$('input[name=_downloadable]').prop('checked', true);
})(jQuery);
</script>
<?php
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
You will have to replace the 'administrator' user role by your specific targeted user role.
You will have to set the opacity to 0, to completely hide that checkboxes.
Tested and works
Addition For many user roles:
Replace this line:
$targeted_user_role = 'administrator';
… by this line:
$targeted_user_roles = array( 'administrator', 'shop_manager' );
And replace also this line:
if ( in_array($targeted_user_role, $user_roles) ){
… by this line:
if ( array_intersect( $targeted_user_roles, $user_roles ) ){
Now the code will work for many user defined user roles
Set VIRTUAL option by default (and hide it):
To hide and set by default virtual option you will use:
add_action( 'woocommerce_product_options_general_product_data', 'hide_and_enable_virtual_by_default' );
function hide_and_enable_virtual_by_default(){
?>
## HERE Goes OUR CSS To hide 'virtual & downloadable'
<style>
label[for="_virtual"], label[for="_downloadable"]{ opacity: 0; }
</style>
<?php
## JQUERY SCRIPT ##
// Here we set as selected the 'virtual' checkboxes by default
?>
<script>
(function($){
$('input[name=_virtual]').prop('checked', true);
})(jQuery);
</script>
<?php
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file. Tested and works.