Update wordpress menu with woocommerce products

EDIT: I tried the following and it works in the theme “Storefront for woocommerce”(Though it needs additional styling).
For some reason though, it does not work well with the twentyfifteen theme.(The submenu does not show.)

add_filter( 'wp_nav_menu_items', 'wc_products_on_nav_menu'); //filter to add the html
add_action( 'save_post_product', 'rebuild_custom_wc_nav_menu'); //action to regenerate the menu array anytime a product is added or updated

function build_wc_extra_menu(){
//get the product categories
$args = array(
     'taxonomy'     => 'product_cat',
     'orderby'      => 'name',
     'show_count'   => false,
     'pad_counts'   => false,
     'hierarchical' => true,
     'title_li'     => '',
     'hide_empty'   => false
);
$product_categories = get_categories( $args );

if(!empty($product_categories)){
    //get the products in each category
    foreach ($product_categories as $category){
        $cat_menu = array();
        $product_arg = array (
                'post_type'  => 'product',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'order'=> 'ASC',
                'product_cat' => $category->slug,
            );
        $products = get_posts($product_arg);
        foreach ($products as $product){
            //store the products as an associtive array with the name as key and link as value(will cause conflicts if two products have the same title)
            $link = get_post_permalink($product->ID);
            $cat_menu[$product->post_title] = $link;
        }
        //store the array of products as the value of an array with the key as the name of the category
        $custom_menu[$category->name] = $cat_menu;
    }
}
//save the entire array in the wp_options table
update_option('wc_cat_menu', $custom_menu);
}

function rebuild_custom_wc_nav_menu(){
//simply rebuild the array 
build_wc_extra_menu();
}

function wc_products_on_nav_menu($menu) { 
//check if the option exists, if not, rebuild the array
if(empty(get_option('wc_cat_menu'))) build_wc_extra_menu();
//retrieve the array from the wp_options table
$custom_menu = get_option('wc_cat_menu');
foreach ($custom_menu as $cat => $product_array){
    //build the html to append to the menu
    $cat_menu = '<li class="page_item">'.$cat.'<ul class="sub-menu">';
    foreach ($product_array as $name => $link){
        $product_submenu = '<li class="page_item"><a href="'.$link.'">'.$name.'</a></li>';
        $cat_menu = $cat_menu.$product_submenu;
    }
    $cat_menu = $cat_menu.'</ul></li>';
    //append the html to the menu
    $menu = $menu.$cat_menu;
}
//return the filtered menu
return $menu;  
}

EDIT: This code below was the original code. Don’t use this!!! I’m leaving it here so because of the comments.

Try this. I did not run the code, so just try it out and comment if it works 🙂

add_filter( 'wp_nav_menu_items', 'wc_products_on_nav_menu');
//the filter hook to edit the nav_menu list

function wc_products_on_nav_menu($menu) {  
//first get all the product categories  
   $product_categories = get_terms( array(
    'taxonomy' => 'product_cat',
    'hide_empty' => true,
) );
if(!empty($product_categories)){
    foreach ($product_categories as $category){
 //add a top level list for the category name.
        $cat_menu = '<li>'.$category->name.'<ul>';
        $product_arg = array (
                'post_type'  => 'product',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'order'=> 'ASC',
                'product_cat' => $category->slug,
            );
        $products = get_posts($product_arg);
        foreach ($products as $product){
//add the products in that category as sub menu items
            $link = get_post_permalink($product->ID);
            $menu_item = '<li><a href="'.$link.'" >'.$product->post_title.'</a></li>';
            $cat_menu = $cat_menu . $menu_item;
        }
        $cat_menu = $cat_menu.'</ul></li>';
        $menu = $menu.$cat_menu;
    }
}
  return $menu;  
}