自动启用虚拟和可下载的产品设置

使用WooCommerce,我使用的是供应商插件,允许人们上传自己的产品。

但是我只希望他们上传虚拟和可下载的产品。

有没有办法在正常的woocommerce“添加产品页面”中删除(甚至隐藏)这些选项并自动检查它们?

在调整所有提交内容时,不需要无法规避 – 只是想让提交过程尽可能简单。

谢谢

对于虚拟产品,仅在最后看到更新

这是可能的,但是安静漫长且复杂,无法测试和解释……您必须通过2种方式,特定用户角色或其用户角色的特定function来定位这些用户。

然后可以使用注入的CSS隐藏一些设置并使用javascript / jQuery来设置这个隐藏的设置……

在下面的工作示例中,我使用jQuery启用'virtual''downloadable'设置cheeckboxes,我将它们隐藏起来主要使用不透明度CSS规则…

我使用挂在woocommerce_product_options_general_product_data动作钩子中的自定义函数,这样:

 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 ?>     

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

您必须用特定的目标用户角色替换“管理员”用户角色。
您必须将不透明度设置为0,才能完全隐藏该复选框。

经过测试和工作


添加对于许多用户角色:

1)替换此行:

 $targeted_user_role = 'administrator'; 

......按此行:

 $targeted_user_roles = array( 'administrator', 'shop_manager' ); 

2)并替换这一行:

 if ( in_array($targeted_user_role, $user_roles) ){ 

......按此行:

 if ( array_intersect( $targeted_user_roles, $user_roles ) ){ 

现在,代码将适用于许多用户定义的用户角色


默认设置VIRTUAL选项(并隐藏它):

要默认隐藏和设置虚拟选项,您将使用:

 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'     

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。 经过测试和工作。