Woocommerce breadcrumbs multiple categories

I have e commerce website in wordpress. There are lot of product in it & many product comes under multiple categories like 600 mah Power bank is comes under automobile, IT, media etc. My problem is when i go to the detail of a product there it by default pick up only one category no matter if go through IT category at the end it shows me automobile like this Home / Shop / industry / Automobile / 600 mah Power Bank. But i went to this product via IT so it should show me like this Home / Shop / industry / IT / 600 mah Power Bank.
How can iget path where i come from previous page?

Related posts

Leave a Reply

4 comments

  1. if you are using Woocommerce you can use the following directly, if not it will need adapting but you get the idea:

    if (get_post_type() == 'product' && is_single() && ! is_attachment()) {
        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 == true) {
                    $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 . '<a href="' . get_term_link($ancestor->slug, 'product_cat') . '">' . $ancestor->name . '</a>' . $after . $delimiter;
                        }
                    }
                    echo $before . '<a href="' . get_term_link($term->slug, 'product_cat') . '">' . $category_name . '</a>' . $after . $delimiter;
                }
            }
        }
    
        echo $before . get_the_title() . $after;
    }
    

    The main bulk of the work here is done by wp_get_referer which gets the referring URL of the product your visitor has navigated to. The rest of the code checks if a valid category is contained within the URL and uses it in the breadcrumb.

    See Jonathon Js post here for more information http://www.cryoutcreations.eu/forums/t/wrong-breadcrumbs-displayed

  2. Updated answer for anyone still encountering this issue on a newer version of WooCommerce, this solution worked for me on WooCommerce 3.5.4.

    This code should be placed inside the following file path (i.e. a child theme)
    /wp-content/themes/your-child-theme/woocommerce/global/breadcrumb.php

    It will override the default WooCommerce breadcrumb code.

    http://pastebin.com/raw/bemM8ZNF

    /**
     * Shop breadcrumb
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any important changes.
     *
     * @see         https://docs.woocommerce.com/document/template-structure/
     * @author      WooThemes
     * @package     WooCommerce/Templates
     * @version     2.3.0
     * @see         woocommerce_breadcrumb()
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    if ( $breadcrumb ) {
    
        echo $wrap_before;
    
        if ( is_single() && get_post_type() == 'product' ) {
    
            echo $prepend;
    
            if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {
    
                $referer = wp_get_referer();
    
                $printed = array();
    
                foreach( $terms as $term){
    
                    if(in_array($term->id, $printed)) continue;
    
                    $referer_slug = (strpos($referer, '/'.$term->slug.'/'));
    
                    if(!$referer_slug==false){
    
                        $printed[] = $term->id;
    
                        $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 . '<a href="' . get_term_link( $ancestor->slug, 'product_cat' ) . '">' . $ancestor->name . '</a>' . $after . $delimiter;
                        }
    
                        echo $before . '<a href="' . get_term_link( $term->slug, 'product_cat' ) . '">' . $category_name . '</a>' . $after . $delimiter;
                    }
                }
    
            }
    
            echo $before . get_the_title() . $after;
    
        } else {
    
            foreach ( $breadcrumb as $key => $crumb ) {
    
                echo $before;
    
                if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
                    echo '<a href="' . esc_url( $crumb[1] ) . '">' . esc_html( $crumb[0] ) . '</a>';
                } else {
                    echo esc_html( $crumb[0] );
                }
    
                echo $after;
    
                if ( sizeof( $breadcrumb ) !== $key + 1 ) {
                    echo $delimiter;
                }
    
            }
        }
    
        echo $wrap_after;
    
    }
    

    C/O: Joris Witteman

  3. I reworked the code a little bit to account for a primary category and a referred one, having infinite parents.

    I used the breadcrumb filter as it is quite clean, directly in the single product template, but in case you need it elsewhere you can use the single conditional.

    function breadcumbs_referred_or_primary ($main, $terms)
    {
      // Our primary term is 520 (hardcoded)
    
      $referer = wp_get_referer();
      $referredTerm = -1;
      $referredTermIndex = -1;
      $primaryTermId = 520; // hardcoded
      $primaryTermIndex = -1;
    
      foreach($terms as $key => $term) {
        if ($referredTerm != -1) break; // we found it in a previous iteration!
    
        $ancestors = get_ancestors( $term->term_id, 'product_cat');
        array_unshift($ancestors, $term->term_id);
        if ($primaryTermIndex == -1 && in_array($primaryTermId, $ancestors)) $primaryTermIndex = $key;
    
        foreach ($ancestors as $ancestor) {
          if($referredTerm != -1) break 2; // we found it in a previous iteration!
          $ancestor = get_term( $ancestor, 'product_cat' );
          $referer_slug = (strpos($referer, '/'.$ancestor->slug.'/'));
    
          if (!$referer_slug==false) { // it's found in the current level
            $referredTerm = $term->term_id;
            $referredTermIndex = $key;
          }
        }
      }
    
      // we return either the browsed terms or the primary term
      if ($referredTermIndex != -1) {
        return $terms[$referredTermIndex];
      } else {
        return $terms[$primaryTermIndex];
      }
    }
    
    add_filter('woocommerce_breadcrumb_main_term', 'breadcumbs_referred_or_primary', 10, 2);
    
  4. My solution is:

    if (!empty($breadcrumb)) {
    
        echo $wrap_before;
    
        if (is_single() && get_post_type() == 'product') {
            $breadcrumb_diff = [];
            $breadcrumb_diff[] = $breadcrumb[0];
            if ($terms = get_the_terms($post->ID, 'product_cat')) {
                $referer = wp_get_referer();
                $site_url = site_url();
                $referer = str_replace($site_url . '/zoomagazin/', '', $referer);
                $referer_array = explode('/', $referer);
                foreach ($referer_array as $term_slug) {
                    $get_term_by_slug = get_term_by('slug', $term_slug, 'product_cat');
                    $breadcrumb_diff[] = [$get_term_by_slug->name, get_term_link($term_slug, 'product_cat')];
                }
                $breadcrumb_diff[]= $breadcrumb[count($breadcrumb) - 1];
    
                foreach ($breadcrumb_diff as $key => $crumb) {
    
                    echo $before;
    
                    if (!empty($crumb[1]) && sizeof($breadcrumb_diff) !== $key + 1) {
                        echo '<a href="' . esc_url($crumb[1]) . '">' . esc_html($crumb[0]) . '</a>';
                    } else {
                        echo esc_html($crumb[0]);
                    }
    
                    echo $after;
    
                    if (sizeof($breadcrumb) !== $key + 1) {
                        echo $delimiter;
                    }
    
                }
            }
        } else {
    
    
            foreach ($breadcrumb as $key => $crumb) {
    
                echo $before;
    
                if (!empty($crumb[1]) && sizeof($breadcrumb) !== $key + 1) {
                    echo '<a href="' . esc_url($crumb[1]) . '">' . esc_html($crumb[0]) . '</a>';
                } else {
                    echo esc_html($crumb[0]);
                }
    
                echo $after;
    
                if (sizeof($breadcrumb) !== $key + 1) {
                    echo $delimiter;
                }
    
            }
        }
    
        echo $wrap_after;
    
    }