If existing category adds in Menu - Woocommerce 3.6.4

Asked

Viewed 21 times

0

I’m using the hierarchy: Samsung>S9 Plus, Category A
The code only works at the following address: https://paginaweb.pt/categoria-produto/s9-plus

The code is this:

// adiciona produto ao menu se categoria existir
add_filter( 'wp_nav_menu_items', 'adiciona_produto_menu', 10, 2 );
function adiciona_produto_menu( $items, $args ) {
    // não funciona com a categoria samsung
    //if( has_term( 'samsung', 'product_cat' ) ) {
    if( has_term( 's9-plus', 'product_cat' ) ) {
        $produto = '<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children">
                        <div class="nav-toggle-subarrow">
                            <i class="fa fa-angle-down"></i>
                        </div>
                        <a href="#">
                            <i class="fa fa-shopping-cart"></i>
                            <span class="fontawesome-text">Produtos</span>
                        </a>
                        <ul class="sub-menu">
                            <li class="menu-item menu-item-type-custom menu-item-object-custom">
                                <a href="#">
                                    <span class="fontawesome-text">Samsung</span>
                                </a>
                            </li>
                        </ul>
                    </li>';
    }
    return $produto.$items;
}

Question: If the Samsung category exists, I want to add to the Main Menu, but I don’t understand how. We will need to use the Menu location?

$args->theme_location == 'primary'

And why it is only presented in the S9 Plus category... I don’t understand! Can help?


Okay, so here’s the deal:

if ( class_exists( 'WooCommerce' ) ) {
    add_filter( 'wp_nav_menu_items', 'adiciona_produto_menu', 10, 2 );
    function adiciona_produto_menu( $items, $args ) {

        if ( term_exists( 'apple', 'product_cat' ) ) :
            $apple = '<li style="border-top:0;border-bottom:0;" class="menu-item menu-item-type-custom menu-item-object-custom"><a href="https://paginaweb.pt/categoria-produto/apple/" target="_self" rel="nofollow noopener"><span class="fontawesome-text">Apple</span></a></li>';
        endif;

        if ( term_exists( 'google', 'product_cat' ) ) :
            $google = '<li style="border-top:0;border-bottom:0;" class="menu-item menu-item-type-custom menu-item-object-custom"><a href="https://paginaweb.pt/categoria-produto/apple/" target="_self" rel="nofollow noopener"><span class="fontawesome-text">Google</span></a></li>';
        endif;

        $categorias = '<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"><div class="nav-toggle-subarrow" target="_self" rel="nofollow noopener"><i class="fa fa-angle-down"></i></div><a href="#"><i class="fa fa-shopping-cart"></i><span class="fontawesome-text">Produtos</span></a><ul class="sub-menu">' . $apple . $google . '</ul></li>';
        return $categorias.$items;
    }
}

1 answer

1


The correct term is: term_exists that is to say, if( term_exists( 'samsung', 'product_cat' ) )

  • Just adding that has_term() is to check if a post has a certain category, and in the case for what you intend to do, the correct is to check if the term exists in the category with term_exists().

  • @Claudio Sanches thank you for your comment! By the way, where can I find more information about terms and other information? I always walk around on spiders I can never find documentation...

  • Documentation that I use is this: https://developer.wordpress.org/, I know it’s not much, but I like to search there and study for it, I don’t know if it helps you, but it can be a start.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.