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

借助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-Woocommerce:显示按属性过滤的产品


下一篇:woocommerce面包屑导航breadcrumb的修改