42

How to Auto-Check Virtual and Downloadable Product Options by User Role in WooCommerce

Powered by WPCodeBox

Empower site administrators to automatically check “Virtual” and “Downloadable” product options in WooCommerce based on user roles, streamlining product setup.


add_filter( 'product_type_options', 'autocheck_virtual_downloadable');

function autocheck_virtual_downloadable( $args ){

    //Set Your targeted user role(s) here
    $targeted_user_roles = array( 'administrator', 'vendor' );

    // Get the current user object
    $user = wp_get_current_user();
    
    // Get the roles of current user
    $user_roles = $user->roles;

    if ( array_intersect( $targeted_user_roles, $user_roles ) ){

        // Auto-Check Virtual and Downloadable Checkboxes
        $args['virtual']     ['default'] = "yes";
        $args['downloadable']['default'] = "yes";
        
        return $args;
        
    }

}

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.