最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

taxonomy - Include WooCommerce product to all child categories

programmeradmin5浏览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?

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?

Share Improve this question asked Dec 27, 2016 at 8:20 Demyd GanenkoDemyd Ganenko 371 silver badge9 bronze badges 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? – Sathishkumar Commented Jun 14, 2019 at 10:49
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论