Quick snippet

Published at David's Tea

I've been working with WooCommerce, a free WordPress plugin to add e-commerce support to a website. I was working on including breadcrumbs to help make the navigation of the store more intuitive. I wanted to include the base path of the store (i.e.: Products) in the breadcrumb path, and I found that the Breadcrumb NavXT plugin treated me with much more respect than WooCommerce's built-in breadcrumb feature.

However, on a single product page, the default breadcrumb path didn't include the WooCommerce Product Category taxonomy path, so instead of seeing something like

Products > Parts > Brakes > Pads > Brake Pad

I saw

Products > Brake Pad

The trouble with categories (in blog software at least) is that a post can be in one or more categories, but the user still expects to be able to see a detailed breadcrumb path telling him how he got to whatever page he's viewing. I came up with a quick fix using Breadcrumb NavXT to look for the first available WooCommerce Product Category and build a breadcrumb path based on that.

function product_cat_breadcrumbs($post, $return = false) {
	$taxonomy_name = 'product_cat';
	$terms = get_the_terms($post->ID, $taxonomy_name);
	$term_count = 0;
	foreach ($terms as $term) {
		$term_count++;
		global $bcn_admin;
		if ($bcn_admin !== null) {
			// Load options
			$bcn_admin->breadcrumb_trail->opt = wp_parse_args(get_option('bcn_options'), $bcn_admin->breadcrumb_trail->opt);
			$bcn_admin->breadcrumb_trail->term_parents($term->term_id, $taxonomy_name);
			return $bcn_admin->breadcrumb_trail->display($return);
		}
		break;
	}
	// If we didn't find any terms, fallback to default behaviour.
	if ($term_count == 0)
		return bcn_display($return);
}

Alternatively...

13 Responses to WooCommerce + Breadcrumb NavXT

  1. You should make this a WooCommerce extension - sell it for a dollar or so and make some cash. Necessary add-on.

    Cheers

  2. C. wrote on May 11, 2012 at 14:29

    How does one install this script? I'm not sure where to place it. Specifically, when I throw the function into the PHP, how do I call to the function to make it work?

    This has been driving me nuts and I know it's going to be an issue with my client, so a response would be greatly appreciated!

  3. @C. - You can place this function in your functions.php file. You can then call it from whatever template file (e.g. archive-product.php) you use to display the breadcrumb trail.

    Hope this helps!

  4. C. wrote on May 14, 2012 at 10:55

    Thanks so much for replying, Daniel! I was able to get it in the right place and show up, however it's having some issues. It works fine on content pages, but on category pages it always shows (going off your example):

    Parts > Brakes > Pads

    Even if I'm only on the 'Parts' or 'Brakes' categories it shows all 3 categories. On other categories that don't have any sub-categories, they're all showing a different category than what I'm on -- strangely, it shows 'Adapters' category on every single one! (I can email you the proof site, but don't want to post it on here.)

    I'm using Breadcrumb NavXT 4.0.2, WooCommerce 1.5.4, and Woo Framwork 5.3.12. Maybe your script is out of date? :(

  5. C. wrote on May 14, 2012 at 11:09

    Nevermind! I was able to get it to work. What I did was:

    1. Enabled WooCommerce's default breadcrumbs on all pages
    2. Added your function to my theme's functions.php file
    3. On the single-product.php (plugins > woocommerce > templates) added 'echo product_cat_breadcrumbs();' at the top
    4. Used CSS to hide WooCommerce's default breadcrumbs so only the one made in #3 appears.

    I also added a 'Home >' link at the beginning of it and a '' at the end so it was the same symmetry as the default crumbs. Works like a charm. :) I only wish I could block out the default crumbs on this page using PHP, but I wasn't able to figure that one out... Oh well, close enough!

  6. Bruce wrote on June 7, 2012 at 22:47

    Thanks for creating this... seems like a few people (like myself) are needing this breadcrumb feature.

    I have added your code in the appropriate functions.php area and have called the function but am getting this error:

    Warning: Missing argument 1 for product_cat_breadcrumbs(), called in /home/***/***/wp-content/plugins/woocommerce/templates/single-product.php on line 2 and defined in /home/***/***/wp-content/themes/wootique/functions.php on line 41

    Have a guess what this might be? Thanks!

  7. Bruce,

    Double check what you're passing as the first argument of the product_cat_breadcrumbs() function. The function is written to take one of the product/post objects in the category you care about, and then go from there.

  8. Hi Daniel,

    I'm actually getting the same error as Bruce up there. If you take a look at this page you'll see what I'm talking about.

    http://rsw.revelationtechs.com/shop/test-shirt-9/

    It would be great if I could get this working asap! I appreciate any help.

    P.S. As a side note, how do I customize the breadcrumbs after I have this working? The plugin NavXT is quite confusing currently.

  9. Well I figured out how to get rid of the error. But it doesn't seem to matter what I put as the argument... I could put anything there and the error goes away.

    I have this now:

    Should I be putting something else?

    And again, how do I customize this breadcrumb, like put "Home" as he has above.

  10. Brandon,

    To get meaningful breadcrumbs, the $post argument needs to be a product object in the category that you care about.

    As far as customizing the breadcrumbs, you'll have to consult the Breadcrumb NavXT documentation.

  11. Awesome, thank you very much Daniel, it works perfectly.

  12. charl wrote on March 27, 2013 at 06:24

    Can someone please tell exactly how to implement the function?

    I did put the script into my function.php but how to i call it?

    What must I type in my single-product.php and where must I type?

  13. @charl: Once you have the function in your function.php file, you can call it from whichever template you want (e.g. single-product.php). Make sure you pass a post object as a parameter to the function.