I've been trying to figure out a way to bulk add all the product categories from WooCommerce without having to add them manually through Appearance -> Menu.

I need their hierarchy to follow but I haven't found anything to help me at all. Mega Menu Plugins are a no go, I would like a custom solution.

Even if I would like to add them manually, the menu doesn't even show all the sub-categories.

enter image description here

There are 400+ product categories in this woocommerce site. So way too long to do manually anyway.

I tried using chatgpt to generate code that would help me bulk add all the prod. cat. from functions.php. It worked but the hierarchy didn't follow and the menu order didn't make sense. There is up to 3 level of hierarchy and I want to display them like a mega menu with multiple column (which I will achieve by customizing CSS) but I first need to import them in the wordpress menu.

Hope this is clear enough.

1 Answer 1


Ok, I found a solution.

I used a combination of a plugin that allows me to add shortcode to menus and creating a new shortcode for product categories in functions.php.

Here is the code for the shortcode:

function display_product_categories_hierarchy($parent_id = 0, $first = true) {
    $args = array(
        'taxonomy'     => 'product_cat',
        'hide_empty'   => false,
        'parent'       => $parent_id
    $categories = get_categories($args);
    if ($categories) {
        if ($first){
        echo '<ul class="boutiquemenu">';
        } else{
        echo '<ul>';
        foreach ($categories as $category) {
            echo '<li><a href="' . get_term_link($category) . '">' . $category->name . '</a>';
            display_product_categories_hierarchy($category->term_id, false); // Recursive call
            echo '</li>';
        echo '</ul>';

function product_categories_hierarchy_shortcode() {
    ob_start(); // Start output buffering
    display_product_categories_hierarchy(); // Call the function
    return ob_get_clean(); // Return the output
add_shortcode('product_categories_hierarchy', 'product_categories_hierarchy_shortcode');

I added a if ($first) to be able to add a class to where this shortcode would be added.

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.