Today we will consider a relatively rare situation: the site has WooCommerce installed, the shop page does not coincide with the main page of the site, and there is no link to the shop page in the breadcrumbs. How do we add it?

To make it clearer, here are some examples of links.

Site home page: site.com
Store page: site.com/shop/
Breadcrumbs: Home > Shop
Category page: site.com/category/
Breadcrumbs: Home > Category
Product page: site.com/category/product/
Breadcrumbs: Home > Category> Product

As you can see, the “Shop” item is missing in the breadcrumbs on the taxonomy and product pages (the shop page may have a different title), although in some cases it is important for the site owner. Thus, our task is to create such a structure:

Category page: site.com/category/
Breadcrumbs: Home > Shop > Category
Product page: site.com/category/product/
Breadcrumbs: Home > Shop > Category > Product

The wrong approach to solving this problem is to add an intermediate product category “Shop”. In this case, the “Store” item will indeed appear in bread crumbs, but:

  • it leads not to the shop page, but to the category page
  • on the shop page it will be difficult to display the correct categories and products

Correct solution

Add the following PHP code to the end of your active theme’s functions.php file:

// Adding a link to the WooCommerce store page in the breadcrumbs
add_filter( 'woocommerce_get_breadcrumb', function($crumbs, $breadcrumb){
	$shop_page_id = wc_get_page_id('shop'); // Requesting the shop page ID
	if($shop_page_id > 0 && !is_shop() && (is_product_taxonomy() || is_product())) { // We check the correctness of the received ID and that the current page is not a store page and that the page is a product page or a product category
		$new_breadcrumb = [
			//_x( 'Shop', 'breadcrumb', 'woocommerce' ), // A string for adding your own shop name. To use uncomment it but comment out the following
			get_the_title(wc_get_page_id('shop')), // Requesting the name of the shop page
			get_permalink(wc_get_page_id('shop')) // Requesting the URL of the shop page
		];
		array_splice($crumbs, 1, 0, [$new_breadcrumb]); // Add a new item to the breadcrumbs after the "Home" item
	}
	return $crumbs;
}, 10, 2 );

Leave a comment

Your email address will not be published.