Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @gusakdmitry ,

    It looks like there is some issue with that specific line on that code. I have replaced the problematic en dash with actual “-” minus and the code seems to be working fine. Here is the corrected code again –

    /**
     * Show product categories in Woorramework breadcrumbs
     */
    // 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 = get_the_title($post->ID);
     
    			// Add the new links, and the original trail's end, back into the trail.
    
    			print_r(count( $trail ));
    			array_splice( $trail, 2, count( $trail ) - 1, $links );
    			
    			$trail['trail_end'] = $trail_end;
    		}
    	}
     
    	return $trail;
    }
     
    /**
     * 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;
    	}
    }

    I hope this helps.

    Thank you ??

    Thread Starter gusakdmitry

    (@gusakdmitry)

    I already guessed to replace “–” myself, the error disappeared, but breadcrumbs did not appear on the shop pages: https://exileiuy.beget.tech/product/first-product/
    Оn other pages breadcrumbs are displayed: https://exileiuy.beget.tech/sample-page/
    What am I doing wrong?

    Hello @gusakdmitry ,

    I am afraid I do not have the exact information regarding this particular snippet.

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    If you need a paid solution, you can consider taking help from one of our partner’s website.

    Thank you ??

    Hi there,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Thank you ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Error when I add code to show product categories in breadcrumbs’ is closed to new replies.