Breadcrumbs woocommerce wordpress

Wrong Woocommerce Breadcrumb for Products in Multiple Categories

Breadcrumbs woocommerce wordpressWoocommerce Breadcrumb are great for customers looking for products in a specifiek product within a categorie, but when using products in multiple categories this is not working very well. If you have a product in categorie X, Y and Z for instance, and you navigate to the product from within the catogory Z, your breadcrumbs probably will display Category X.
This is very confusing for customers and even can result in loss of a sale!

I’ve been looking for a solution and found this page which did it for me.

You need to edit the woocommerce/global/breadcrumb.php, it’s best to make a copy of this file to your (child) theme.

Replace this code:

Original Woocommerce Breadcrumbs code:

elseif ( is_single() && ! is_attachment() ) {
 
if ( get_post_type() == 'product' ) {
 
echo $prepend;
 
if ( $terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) ) ) {
 
$main_term = $terms[0];
 
$ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
 
$ancestors = array_reverse( $ancestors );
 
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, 'product_cat' );
 
if ( ! is_wp_error( $ancestor ) && $ancestor )
echo $before . '<a href="' . get_term_link( $ancestor->slug, 'product_cat' ) . '">' . $ancestor->name . '</a>' . $after . $delimiter;
}
 
echo $before . '<a href="' . get_term_link( $main_term->slug, 'product_cat' ) . '">' . $main_term->name . '</a>' . $after . $delimiter;
 
}
 
echo $before . get_the_title() . $after;

with this code:

Code for generating correct woocommerce breadcrumbs

elseif ( is_single() && ! is_attachment() ) {
 
if ( get_post_type() == 'product' ) {
 
echo $prepend;
 
if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {
                
                $referer = wp_get_referer();
                foreach( $terms as $term){
                    $referer_slug = (strpos($referer, $term->slug));
 
                    if(!$referer_slug==false){
                        $category_name = $term->name;
                        $ancestors = get_ancestors( $term->term_id, 'product_cat' );
        $ancestors = array_reverse( $ancestors );
                        
                        foreach ( $ancestors as $ancestor ) {
       $ancestor = get_term( $ancestor, 'product_cat' );
 
                            if ( ! is_wp_error( $ancestor ) && $ancestor )
                                echo $before . '' . $ancestor->name . '' . $after . $delimiter;
                        }
                        echo $before . '' . $category_name . '' . $after . $delimiter;
                    }
                }
}
 
echo $before . get_the_title() . $after;
9681 Total Views 1 Views Today

9 thoughts on “Wrong Woocommerce Breadcrumb for Products in Multiple Categories

  1. Same issue here.

    Category 1, 2 and 3 all contain some shared products.

    Customer clicks through to product from Category 3, and is shown breadcrumb with Category 1 in it.

    I tried your code here, didn’t work for me. Do you have an updated link to a solution for this.

    Thanks for sharing!

    1. Hi Robbie, Which version of the breadcrumb.php are you using? I’m still using the breadcrumb.php version 2.2.0. Besides that I’m still on Woocommerce 2.3.13, didn’t update tot latest version (currently 2.4.4) yet.

  2. Hey man,

    Thanks for chiming in so quick!

    So the breadcrumb.php version is 2..3 and I’m still running an older Woocommerce 2.3.13

    But something just occured to me. The shop I’m working on is using Yoast Seo Breadcrumbs.

    I might be on a whole other search now…

    That would explain why I’ve been fiddling with woocommerce breadcrumbs and nothing happens.

    Aye-ya-yaye… 🙂

    Let me know what you think, as I’m just a copy and paste code kinda guy. I have no idea what I’m doing at all.

    Thanks in advance for your input.

    Regards,

    Robbie

    1. I’m not into PHP myself so for me it was also Copy / Paste.. and it (still) works for me. Really weird that Woocommerce has not fixed this themselves.

  3. It’s the best stuff I’ve found so far, but in some cases I’m having duplicate categories. It’s a bit frustrating to be honest…

  4. I just like the valuable info you supply to your articles.
    I’ll bookmark your weblog and check once more right here regularly.
    I am relatively certain I’ll be informed lots of new stuff right here!
    Good luck for the next!

  5. I did not find this code in my breadcrumb.php (2.3.0), it looks very different.
    How can I solve this problem?

  6. The solution works with breadcrumb.php version 2.2.0. For me this version of breadcrumbs.php still works (woocommerce version 2.4.10).

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Spam Blocker