使用WooCommerce产品更新WordPress菜单

时间:2016-07-05 作者:Relisora

出于美观的目的,我选择将菜单从我的主题(二一五)中保留下来。

我有一个很大的产品目录,我使用Woocommerce将它们分为类别和产品。

每次我添加新产品时,我也必须手动将产品添加到wordpress菜单中。现在网站越来越大了,我会有很多新产品,我会一直保持更新。

Woocommerce小部件“Woocommerce产品类别”在设置为类别的子级时自动添加新产品。

Pictures 为了更容易理解:

enter image description here

这是我的菜单。如果我在“存储柜”中添加一个产品,我仍然需要在后端手动添加它,使其显示为“存储柜”的子级。

有没有一种方法可以自动将产品添加到正确的类别中,就像我创建产品时设置了“存储柜”类别一样?

希望我能说清楚,谢谢你的回复。不要犹豫,请澄清,因为英语不是我的第一语言。

1 个回复
SO网友:Stephen Afam-Osemene

编辑:我尝试了以下内容,它的主题是“woocommerce店面”(尽管它需要额外的样式)。然而,出于某种原因,它与“215”主题不太匹配。(子菜单不显示。)

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;  
}
编辑:下面的代码是原始代码。不要用这个!!!我把它留在这里是因为有评论。

试试这个。我没有运行代码,所以请尝试一下,如果可行,请发表评论:)

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;  
}