akk*_*ker 2 css php wordpress jquery woocommerce
在 WooCommerce 中,我使用了一个供应商插件,它允许人们上传自己的产品。
但是,我只希望他们上传虚拟和可下载的产品。
有没有办法在普通的woocommerce“添加产品页面”上删除(甚至隐藏)这些选项并自动检查它们?
当我审核所有提交时,不需要不可能规避 - 只是想让提交过程尽可能简单。
谢谢
对于虚拟产品,只能在最后看到更新
这是可能的,但是测试和解释的时间很长而且很复杂......您必须通过两种方式在条件下定位这些用户,特定用户角色或他们用户角色的特定能力。
然后可以使用注入的 CSS 隐藏一些设置,并使用 javascript/jQuery 来设置此隐藏设置...
在下面的工作示例中,我使用 jQuery启用
'virtual'和'downloadable'设置复选框,并使用不透明 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
?>
<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
}
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
您必须将“管理员”用户角色替换为您的特定目标用户角色。
您必须将不透明度设置为 0,以完全隐藏该复选框。
测试和工作
添加对于许多用户角色:
替换这一行:
$targeted_user_role = '管理员';
...通过这一行:
$targeted_user_roles = array( 'administrator', 'shop_manager' );
Run Code Online (Sandbox Code Playgroud)
并替换此行:
if ( in_array($targeted_user_role, $user_roles) ){
...通过这一行:
if ( array_intersect( $targeted_user_roles, $user_roles ) ){
Run Code Online (Sandbox Code Playgroud)
现在该代码适用于许多用户定义的用户角色
默认设置 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 & 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
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。测试和工作。
使用这个简单的解决方案:
add_filter( 'product_type_options', 'autocheck_vd');
function autocheck_vd( $arr ){
$arr['virtual'] ['default'] = "yes";
$arr['downloadable']['default'] = "yes";
return $arr;
}
Run Code Online (Sandbox Code Playgroud)