自动启用虚拟和可下载产品设置 [英] Automatically enable virtual and downloadable product settings

查看:19
本文介绍了自动启用虚拟和可下载产品设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 WooCommerce,我使用了一个供应商插件,它允许人们上传自己的产品.

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

有没有办法在普通的woocommerce添加产品页面"上删除(甚至隐藏)这些选项并自动检查它们?

不需要在我审核所有提交时不可能规避 - 只是想让提交过程尽可能简单.

谢谢

解决方案

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

这是可能的,但是测试和解释的时间很长而且很复杂......您必须通过两种方式在条件下定位这些用户,特定用户角色或他们用户角色的特定能力.

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

<块引用>

在下面的工作示例中,我启用了 'virtual''downloadable' 设置复选框jQuery 和我几乎完全用不透明 CSS 规则隐藏它们......

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

add_action('woocommerce_product_options_general_product_data', 'hiding_and_set_product_settings');函数 hidden_​​and_set_product_settings(){## ==>在此处设置您的目标用户角色:$targeted_user_role = '管理员';//获取当前用户对象$user = wp_get_current_user();//获取当前用户的角色$user_roles = $user->roles;if ( in_array($targeted_user_role, $user_roles) ){## CSS RULES ##(测试后将不透明度更改为 0)//这里是我们的 CSS 来隐藏虚拟"和可下载"复选框?><风格>标签[for =_virtual"],标签[for =_downloadable"]{不透明度:0.2;/* 不透明度:0;*/}</风格><?php## JQUERY 脚本##//这里我们将虚拟"和可下载"复选框设置为选中状态?><脚本>(功能($){$('input[name=_virtual]').prop('checked', true);$('input[name=_downloadable]').prop('checked', true);})(jQuery);<?php}}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

<块引用>

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

经过测试并有效


添加对于许多用户角色:

  1. 替换这一行:

    $targeted_user_role = '管理员';

...通过这一行:

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

  1. 并替换此行:

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

...通过这一行:

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

<块引用>

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


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

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

add_action('woocommerce_product_options_general_product_data', 'hide_and_enable_virtual_by_default');函数 hide_and_enable_virtual_by_default(){?>## 这里是我们的 CSS 来隐藏 'virtual &可下载'<风格>标签[for =_virtual"],标签[for =_downloadable"]{不透明度:0;}</风格><?php## JQUERY 脚本##//这里我们默认设置为选中虚拟"复选框?><脚本>(功能($){$('input[name=_virtual]').prop('checked', true);})(jQuery);<?php}

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中.经测试有效.

With WooCommerce, I'm using a vendor plugin which allows people to upload their own products.

However I would only like them to upload virtual and downloadable products.

Is there a way to remove (or even hide) these options on the normal woocommerce "add product page" and autocheck them?

Doesn't need to be impossible to circumvent as I moderate all submissions - just want to make the submission process as easy as possible.

Thanks

解决方案

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:

  1. Replace this line:

    $targeted_user_role = 'administrator';

… by this line:

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

  1. 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.

这篇关于自动启用虚拟和可下载产品设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆