Marko I.
Marko I.

Reputation: 562

Auto enable virtual option by default for a specific product type in WooCommerce

I have Woocommerce product type called "booking" and what I would like to do is to automatically mark it "virtual" upon creation.

Based on "Automatically enable virtual and downloadable product settings" answer code, the code below sets all products to be virtual, while I want only "booking" product type to be by default virtual as opposed to simple or variable products:

add_action( 'woocommerce_product_options_general_product_data', 'enable_virtual_option' );
function enable_virtual_option(){

?>

        <script>
            (function($){
                $('input[name=_virtual]').prop('checked', true);

            })(jQuery);
        </script>

        <?php

}

Upvotes: 1

Views: 1422

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254363

The following will auto enable virtual checkbox for a specific product type (here "booking" type):

add_action( 'woocommerce_product_options_general_product_data', 'hiding_and_set_product_settings' );
function hiding_and_set_product_settings(){
    global $post, $product_object;

    if ( $product_object->is_type('booking') ){
        ?>
        <script>
            (function($){
                $('input[name=_virtual]').prop('checked', true);
            })(jQuery);
        </script>

        <?php
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 1

Related Questions