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) {
		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);
	// If we didn't find any terms, fallback to default behaviour.
	if ($term_count == 0)
		return bcn_display($return);