Overriding Woocommerce breadcrumbs with Rank Math instead?

#19094
  • Resolved Victoria
    Rank Math free

    Hello, I had the exact same problem as this person in this thread:
    https://support.rankmath.com/ticket/woocommerce-breadcrumb-bug/

    Like him, some of my products have multiple categories, so I selected a “primary category”, so the breadcrumb will show “shop > primary category” instead of “shop > alphabetically first category”.
    The product section under Rank Math Titles & Meta still displays only “none”, “product categories”, and “product tags” beside the Primary Taxonomy field; I have chosen “Product Categories” since Primary Category is still not an option.
    The breadcrumb ux element is currently the woocommerce one and I tried copying the code like Todd did in that other ticket. My functions.php now looks like this:

    if (function_exists('rank_math_the_breadcrumbs')) rank_math_the_breadcrumbs();
    
    add_filter( 'rank_math/frontend/breadcrumb/main_term', function( $current_term, $terms ) {
        if ( function_exists( 'is_product' ) && is_product() ) {
            global $post;
            $terms = get_the_terms( $post, 'product_cat' );
            if ( ! empty( $terms ) ) {
                return $terms[0];
            }
        }
        return $current_term;
    }, 10, 2 );

    I was hoping doing this would override the woocommerce breadcrumb and show my primary category but it didn’t work. When I inspect the product page, the breadcrumb is under
    <div class=”product-breadcrumb-container is-normal”><nav class=”woocommerce-breadcrumb breadcrumbs>

    So it is still using the woocommerce breadcrumb and not the rank math one.
    Could you please advise what I need to change, and where, to make this menu element show my Primary Category instead of Alphabetically First Category.

    Thank you

    • This topic was modified 4 years, 5 months ago by Victoria. Reason: added my site
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    This suggest that your theme is using the default single product templates and thus you might need to override these templates and add the Rank Math breadcrumb function.
    WooCommerce comes with a variety of hooks and filter and you can read up on this here: https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/
    You can also check out the following tutorial on overriding WC templates: https://docs.woocommerce.com/document/template-structure/

    Looking forward to helping you. Thank you.

    ​​​​​​​

    In flatsome/woocommerce/global/breadcrumbs.php the following code is for the Category:

    		if(is_product() || flatsome_option('wc_category_page_title')){
    				$key = $key+1;
    				if ( sizeof( $breadcrumb ) > $key+1) {
    					echo ' <span class="divider">'.$delimiter.'</span> ';
    				}
    		} else{
    			
    		// Category pages
    		if ( sizeof( $breadcrumb ) !== $key + 1 ) {
    				echo ' <span class="divider">'.$delimiter.'</span> ';
    			}
    		}

    I don’t know where to add the code or if it will even work since rank math is kept in a seperate place to woocommerce

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    HI there,

    Thanks for getting back to us.

    Would you mind sharing a copy of the whole page template via a gist or https://pastebin.com/ so that we can advise further?

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

The ticket ‘Overriding Woocommerce breadcrumbs with Rank Math instead?’ is closed to new replies.