• Resolved juanbro

    (@juanbro)


    Hello,

    If you have a product in multiple categories, when you click from a product category or subcategory page, the breadcrumbs may not be what you expect, i.e., the breadcrumbs display a different category than the one you just came from.

    To duplicate:
    Create four categories:
    Category One > Sub Category One,
    Category Two > Sub Category Two

    Now place Sunglasses (or any product) in those categories.

    Now view the Category One (or Sub Category One) page and click the product.

    On the product page, the breadcrumb will show you are in:
    Home > Category Two > Subcategory Two > Sunglasses

    I suppose the product does not know which category page you came from and just picks a category for breadcrumbs to display. But this makes things confusing for the customer. Any solution for this other than disabling breadcrumbs?

    Thanks in advance for your help!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Joel Williams

    (@joelwills)

    Automattic Happiness Engineer

    Hi there!

    This is the default behavior in WordPress (and WooCommerce) when there are multiple categories.

    If you use a plugin like Yoast SEO they have an option to set a Primary category that will always be sued when multiple are added:

    https://kb.yoast.com/kb/implement-wordpress-seo-breadcrumbs/

    Hope that helps!

    Hi,

    Hello!

    I’ve a similar problem: in my site, most products belong to different categories and sub-categories, I give an example: Product X belongs to Category1 / Sub-category1 + Category2 / Sub-category2.

    Problem: when I’m in the Subcategory2 products list and I click on Product X, the system links to the Product X page with url “… / catalog / category1 / subcategory1 / productX” and also the categories/subcategories list in the sidebar marks as “current” subcategory1.

    Question: How can I make sure that if I am in the Subcategory2 product list and if I click on the productX, I go to the product X page with url “… / catalog / category2 / subcategory2 / productX” ?

    Thanks for help!
    Nuala

    Thread Starter juanbro

    (@juanbro)

    Hi Nuala,

    In my opinion this is a bug.

    Unfortunately it seems like a hard one to fix properly. ??

    juanbro

    Joel Williams

    (@joelwills)

    Automattic Happiness Engineer

    As mentioned that’s the way WordPress works, so it’s how it also works on WooCommerce. There isn’t a way to do what @nualap is suggesting built in, it will go to either the first category that was set up, or if using a plugin the Primary category set.

    It’s likely possible with custom coding but that’s beyond what I can do.

    Hi Joel
    Thank you for your suggestion.
    I think I’ll use the plugin you’ve mentioned.
    But I would try custom coding: can you suggest what’s the correct code?

    Thanks again!
    Nuala

    Joel Williams

    (@joelwills)

    Automattic Happiness Engineer

    I’m not a developer so I don’t know what the correct code would be. Hopefully someone else can point you in the right direction. I can’t find any examples of this online so you may need to find a developer to do this for you:

    https://woocommerce.com/customizations/?

    Or ask on developer focused forums like:

    https://wordpress.stackexchange.com

    Hope that helps!

    Joel Williams

    (@joelwills)

    Automattic Happiness Engineer

    Hi there!

    There hasn’t been a reply for a while so I’m going to close this ticket.

    If you’re still having problems please open a new ticket, thanks!

    Hello I wrote some code which fixes an issue:

    	if( is_product() ) {
    		$permalinks = wc_get_permalink_structure();
    		$current_title = end($breadcrumb)[0];
    		if ( $_SERVER['HTTP_REFERER'] ) {
    			$url_parts = explode( '/', trim( wp_parse_url( $_SERVER['HTTP_REFERER'] )['path'], '/' ) );
    			$url = site_url() . '/' . $permalinks['category_base'];
    			$breadcrumb = array(
    				array( __( 'Homepage', 'woocommerce-multilingual' ), site_url(), ),
    				array( __( 'Catalog', 'woocommerce' ), site_url() . $permalinks['product_base'] . '/', ),
    			);
    			foreach( $url_parts as $url_part ) {
    				if( $term = get_term_by( 'slug', $url_part, 'product_cat' ) ) {
    					$url .= '/' . $url_part;
    					$breadcrumb[] = array( $term->name, $url );
    				}
    			}
    			$breadcrumb[] = array( $current_title );
    		} else {
    			$breadcrumb = array(
    				array( __( 'Homepage', 'woocommerce-multilingual' ), site_url(), ),
    				array( __( 'Products', 'woocommerce-multilingual' ), site_url() . $permalinks['product_base'] . '/', ),
    				array( $current_title ),
    			);
    		}
    	}
    

    You should put it in ‘your_theme/woocommerce/global/breadcrumb.php’ just after ‘if ( ! empty( $breadcrumb ) ) {‘.

    Hope it’ll help.

    Serge, thanks it works!

    Hi Serge,

    I don’t have the file ‘your_theme/woocommerce/global/breadcrumb.php’
    In my functions.php of the theme, are some lines for the breadcrumb.
    Is it then also possible with this lines?

    //breadcrumbs
    
    function the_breadcrumb() {
    		echo '<div id="linkpad">';
    	if (!is_front_page()) {
    		echo '<a href="';
    		echo home_url();
    		echo '">';
    		echo 'Home';
    		echo "</a> &raquo; ";
    		if (is_category() || is_single()) {
    			echo '';
    			the_category(' &raquo; ');
    			if (is_single()) {
    				echo " &raquo; ";
    				the_title();
    				echo '';
    			}
    		} elseif (is_page()) {
    			echo '';
    			echo the_title();
    			echo '';
    		} elseif (is_home()) {
    			echo wp_title('');
    		}
    	}
    	elseif (is_tag()) {single_tag_title();}
    	elseif (is_day()) {echo"Archive for "; the_time('F jS, Y'); echo'';}
    	elseif (is_month()) {echo"Archive for "; the_time('F, Y'); echo'';}
    	elseif (is_year()) {echo"Archive for "; the_time('Y'); echo'';}
    	elseif (is_author()) {echo"Author Archive"; echo'';}
    	elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "Blog Archives"; echo'';}
    	elseif (is_search()) {echo"Search Results"; echo'';}
    	echo '</div>';
    }
    

    Thanks for your reply. Paul

    Looks like your (Serge Sysoev (@sergesysoev)) code fixed it BUT NOT 100% — it only updates category after a manual refresh. Unless i refresh the page, category remains the old and once page refreshed category gets updated to the correct one. You may see screencast here: https://recordit.co/9RZO5wdQcy.

    Any fix to this? Please advise. Thank you!

    This is the weirdest default behavior I can think of. A visitor/customer expects to see and browse the products in the category they choose for. I tried the solution of Serge but it does not work properly.

    It goes wrong right from the beginning. When viewing a category archive (and that is where it almost always starts) and clicking a product brings me to the right Single Product page but most of the times in another category. Of course must the product being added to more than one category, which is often the case.

    Does somebody know how to iron that one out?

    Hey @sergesysoev one question.

    How exactly does your code gets printed to the screen? There seems to be no sprintf or echo.

    I’ve tried to put echo $breadcrumb at the end, but this just echoes “Array” everywhere.

    Any idea?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Breadcrumbs for products in multiple categories’ is closed to new replies.