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

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

问题描述

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

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.

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

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.

谢谢

推荐答案

仅对于虚拟产品,请参阅最后的更新

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.

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

Then is possible to hide some settings with injected CSS and the use of javascript/jQuery to set this hidden settings...

在下面的工作示例中,我使用jQuery启用了 'virtual' 'downloadable' 设置复选框,而我基本上完全用不透明CSS规则隐藏了它们……

我通过以下方式使用挂在 woocommerce_product_options_general_product_data 操作挂钩中的自定义函数:

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
    }

}

代码会出现在活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

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

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


默认情况下设置虚拟"选项(并将其隐藏):

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

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'
    <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文件或任何插件文件中.经过测试,可以正常工作.

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天全站免登陆