public
Last active

Woo Breadcrumbs product categories, by Matt

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
// Add product categories to the "Product" breadcrumb in WooCommerce.
 
// Get breadcrumbs on product pages that read: Home > Shop > Product category > Product Name
add_filter( 'woo_breadcrumbs_trail', 'woo_custom_breadcrumbs_trail_add_product_categories', 20 );
 
function woo_custom_breadcrumbs_trail_add_product_categories ( $trail ) {
if ( ( get_post_type() == 'product' ) && is_singular() ) {
global $post;
$taxonomy = 'product_cat';
$terms = get_the_terms( $post->ID, $taxonomy );
$links = array();
 
if ( $terms && ! is_wp_error( $terms ) ) {
$count = 0;
foreach ( $terms as $c ) {
$count++;
if ( $count > 1 ) { continue; }
$parents = woo_get_term_parents( $c->term_id, $taxonomy, true, ', ', $c->name, array() );
 
if ( $parents != '' && ! is_wp_error( $parents ) ) {
$parents_arr = explode( ', ', $parents );
foreach ( $parents_arr as $p ) {
if ( $p != '' ) { $links[] = $p; }
}
}
}
// Add the trail back on to the end.
// $links[] = $trail['trail_end'];
$trail_end = $trail[count( $trail ) - 1];
 
// Add the new links, and the original trail's end, back into the trail.
array_splice( $trail, 2, count( $trail ) - 1, $links );
$trail['trail_end'] = $trail_end;
}
}
 
return $trail;
} // End woo_custom_breadcrumbs_trail_add_product_categories()
 
/**
* Retrieve term parents with separator.
*
* @param int $id Term ID.
* @param string $taxonomy.
* @param bool $link Optional, default is false. Whether to format with link.
* @param string $separator Optional, default is '/'. How to separate terms.
* @param bool $nicename Optional, default is false. Whether to use nice name for display.
* @param array $visited Optional. Already linked to terms to prevent duplicates.
* @return string
*/
 
if ( ! function_exists( 'woo_get_term_parents' ) ) {
function woo_get_term_parents( $id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
$chain = '';
$parent = &get_term( $id, $taxonomy );
if ( is_wp_error( $parent ) )
return $parent;
 
if ( $nicename ) {
$name = $parent->slug;
} else {
$name = $parent->name;
}
 
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= woo_get_term_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
}
 
if ( $link ) {
$chain .= '<a href="' . get_term_link( $parent, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$parent->name.'</a>' . $separator;
} else {
$chain .= $name.$separator;
}
return $chain;
} // End woo_get_term_parents()
}

Where to place this? Thanks

Got it - Just added as a plugin. Worked a treat.

I can't find the file to add this code...
Where to place this code?

Thanks.

i don't know why but i had to replace line 33 with:

$trail_end = get_the_title($post->ID);

hoe to add as a plugin or where to place code?

Thanks

Thanks for the code!
It works great but I miss a breadcrumb for categories:

Currently:

Home / Products -> click on a product category -> Home / Product Category

Should be:

Home / Products -> click on a product category -> Home / Products / Product Category

How can I achieve that?

For everyone who doesn't know how to add this code: look for the functions.php of your active theme :)

THX for the code !

Any idea how to fix the category issue when you click on a category in the url instead of Shop there is product-category

Thanks! I put this code in my a Mystile child theme functions.php and it worked great. (I had to remove a comma from a category.)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.