【问题标题】:Automatically enable virtual and downloadable product settings自动启用虚拟和可下载产品设置
【发布时间】:2017-06-29 18:45:21
【问题描述】:

在 WooCommerce 中,我使用了一个供应商插件,允许人们上传自己的产品。

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

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

不需要因为我审核所有提交而无法规避 - 只是想让提交过程尽可能简单。

谢谢

【问题讨论】:

  • 我不知道如何自动检查VirtualDownload复选框,但你可以隐藏选项,检查this link
  • 谢谢,主要问题是实际上自动选择。如果我可以自动选择,隐藏会很棒

标签: php jquery css wordpress woocommerce


【解决方案1】:

使用这个简单的解决方案

add_filter( 'product_type_options', 'autocheck_vd');

function autocheck_vd( $arr ){
    $arr['virtual']     ['default'] = "yes"; 
    $arr['downloadable']['default'] = "yes"; 
    return $arr;
}

【讨论】:

    【解决方案2】:

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

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

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

    在下面的工作示例中,我使用 jQuery 启用了 'virtual''downloadable' 设置复选框,并且我几乎完全使用 opacity CSS 规则隐藏它们…… p>

    我在 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 文件或任何插件文件中。

    您必须将“管理员”用户角色替换为您的特定目标用户角色。
    您必须将不透明度设置为 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 ) ){
    

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


    默认设置 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
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。经过测试并且可以工作。

    【讨论】:

    • 啊解决了-愚蠢的我忘了关闭PHP?>谢谢
    • @LoicTheAztec:以上对我有用,但我尝试让这个也适用于库存选项卡上的单独销售复选框。恕我直言,在一个订单中出售超过 1 个可下载文件的副本是没有意义的。将 _sold_individually 添加到脚本部分不会对其进行检查。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 2019-12-29
    • 2018-02-28
    • 1970-01-01
    • 2013-07-05
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多