我如何使用 woocommerce show/hide 管理产品页面中的某些类别?

how can I show/hide some categories from the admin product page using woocommerce?

我已经尝试这样做一段时间了,不幸的是,我只找到了商店(前端)在类别为空等情况下的解决方案。

我基本上想做的是,当我在产品类别部分创建新产品时

我想show/hide一些类别。

到目前为止我还没有找到任何关于这个的东西,知道怎么做吗?

谢谢。

试试下面的代码:

function is_edit_page($new_edit = null){
    global $pagenow;
    //make sure we are on the backend
    if (!is_admin()) return false;

    if($new_edit == "edit")
        return in_array( $pagenow, array( 'post.php',  ) );
    elseif($new_edit == "new") //check for new post page
        return in_array( $pagenow, array( 'post-new.php' ) );
    else //check for either new or edit
        return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
}


function so_28055266_filterGetTermArgs($args, $taxonomies) {
    global $typenow;

    if ($typenow == 'product') { 
        // check whether we're currently filtering selected taxonomy
        if (implode('', $taxonomies) == 'product_cat' && is_edit_page()) {
            //Add categories term ID that you want to show
            $cats = array(9,10,11,12); // List of category(term ID) that you want to add as an array

            if (empty($cats))
                $args['include'] = array(99999999); // no available categories
            else
                $args['include'] = $cats; //It will only show the category that you mentioned in above array
        }
    }

    return $args;
}

if (is_admin()) {
    add_filter('get_terms_args', 'so_28055266_filterGetTermArgs', 10, 2);
}

让我知道输出。