Log1x / navi

A developer-friendly alternative to the WordPress NavWalker.
https://github.com/Log1x/navi
MIT License
315 stars 30 forks source link

WooCommerce Shop page is never active #25

Closed thomasjulienalain closed 1 year ago

thomasjulienalain commented 4 years ago

Hello !

It's weird, when adding the WooCommerce's shop page in my menu, the item returned by navi has 'active' set to false when I am on the shop page.

Works fine for any other page.

Log1x commented 3 years ago

I assume WooCommerce has a different name for the current-item class on the shop. Can you possibly paste me the list of classes set on the menu item by default?

qpl88 commented 3 years ago

Hi there, i'm also experiencing the same thing. The menu item for WooCommerce's Shop page just shows "menu-item ".

Reinpal commented 1 year ago

Hey!

Stumbled across the same problem, seems like WooCommerce is adding the current-menu-item class via the wp_nav_menu_objects filter.

Maybe thats why it's not showing up as active? See here: https://github.com/woocommerce/woocommerce/blob/3ded2b46e976e5c11393a6cf70943b4f6ef14cb3/plugins/woocommerce/includes/wc-page-functions.php#L157

I used array_map combined with some WooCommerce Conditionals to get this to work, but I guess it's not the cleanest solution.

In case anyone needs a quick workaround:

    /**
     * Returns the shop navigation.
     *
     * @return array
     */
    public function navigation()
    {
        if (Navi::build('shop_navigation')->isEmpty()) {
            return;
        }

        $shop_page = wc_get_page_id('shop');
        $shopNav = Navi::build('shop_navigation')->toArray();

        if (is_shop() || is_singular('product')) {
            array_map(function ($item) use ($shop_page) {
                $item->active = $shop_page == $item->objectId ? true : false;

                return $item;
            }, $shopNav);
        }

        return $shopNav;
    }
Log1x commented 1 year ago

Hey, not sure if something changed but I can't reproduce this with the latest WooCommerce. I'm getting active items for Shop, Products, etc. without any issues.

If someone can show an example of it being broken, I have an easy fix– otherwise, I'm going to close this for now as I don't want to deploy the fix without knowing it's actually doing something.

davideprevosto commented 6 months ago

Hi there, I am getting the same issue. The /shop page is setting to false the "active" boolean property. We are using the last WooCommerce version 8.7.0