Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

php - Automatically enable virtual and downloadable product settings

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...