ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-12 09:39:55  阅读:197  来源: 互联网

标签:php jquery css wordpress woocommerce


借助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,jquery,css,wordpress,woocommerce
来源: https://codeday.me/bug/20191012/1899606.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有