0

I need to include some of my products in all subcategories of category Auto. For example - product Wheel must be in all subcategories of Auto, e.g. Audi, BMW, Mercedes, etc. How to do this without manual choosing of all subcategories?

1
  • When i select( check on checkbox ) parent category i need to check all the subcategories belongs to that parent category. I can’t able to find this option in Woocommerce product filter plugin. I tried the above solution but it doesn't seems to work.. can you please tell me what is 'categorychecklist' ID? Jun 14, 2019 at 10:49

1 Answer 1

2

(1) Paste below code to your functions.php file

function my_enqueue() {
    wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/js/myscript.js' );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

(2) Create myscript.js file and paste below code in it and place the file in 'yourtheme/js/' folder.

jQuery(document).ready(function () {

    jQuery('#categorychecklist').prepend('<input type="checkbox" id="selectall"><label for="selectall">Select all subcategories.</label><hr>');

    jQuery('#categorychecklist [type="checkbox"]:not(#selectall)').on('change', function () {
        if(jQuery('#selectall').is(':checked')) {
            jQuery(this).parent().parent().find('ul.children [type="checkbox"]').prop('checked', true);
        } else {
            jQuery(this).parent().parent().find('ul.children [type="checkbox"]').removeAttr('checked');
        }
    });
});

(3) Done - now selecting your parent category will automatically select child categories if Select all subcategories checkbox is selected on product edit page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.