首页 / PHP / php-自动启用虚拟和可下载的产品设置
php-自动启用虚拟和可下载的产品设置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-自动启用虚拟和可下载的产品设置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2839字,纯文字阅读大概需要5分钟。
内容图文
借助WooCommerce,我使用的是供应商插件,允许人们上传自己的产品.
但是,我只希望他们上传虚拟和可下载的产品.
有没有办法在普通的woocommerce“添加产品页面”上删除(甚至隐藏)这些选项并自动检查它们?
在我审核所有提交内容时,不必绕开它-只是希望使提交过程尽可能容易.
谢谢
解决方法:
For Virtual products only see the update at the end
这是可能的,但测试和解释的时间却很长而且很复杂…您将必须通过两种方式(一种特定的用户角色或某种用户角色的特定能力)来针对该用户.
然后可以使用注入的CSS隐藏某些设置,并可以使用javascript / jQuery设置此隐藏设置…
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…
我使用挂钩在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
?>
<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
}
}
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中.
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.
经过测试和工作
对于许多用户角色:
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 ) ){
Now the code will work for many user defined 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'
<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
}
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中.经过测试和工作.
内容总结
以上是互联网集市为您收集整理的php-自动启用虚拟和可下载的产品设置全部内容,希望文章能够帮你解决php-自动启用虚拟和可下载的产品设置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。