Product Category Breadcrumb: Looper Provider Code Snippet

I was a bit surprised that there wasn’t a preset WooCommerce Breadcrumb for product category. I searched the forums and couldn’t find one, so I generated one… and wanted to share with the community to simplify this for others.

This snippet registers a Cornerstone Looper Provider that finds the deepest product category assigned to the current product, walks up its parent categories, and returns the trail from Shop → category ancestors → product. If there are multiple deepest categories, it uses the first one returned by WordPress.

Use this in combination with Pro’s native element: Breadcrumbs (Elements)

In Cornerstone, set Looper Provider → Product Category Breadcrumb on the breadcrumb wrapper and Looper Consumer → All on the repeating crumb. Since each item is an array with name and url fields, use:

  • Crumb label: {{dc:looper:field key="name"}}
  • Crumb link: {{dc:looper:field key="url"}}

image

If you’re not looking for the Shop Name as the first item or the product name as the last item, add a Condition to the Crumb.

Either:

  1. Index is not First
    or
  2. Index is not Last
    or
  3. Index is not First and
    Index is not Last

/**
 * Register a Cornerstone looper for a product breadcrumb:
 * Shop > category ancestors > product.
 * If multiple deepest categories are assigned, the first one returned by
 * WordPress is used.
 */
function register_product_category_breadcrumb_looper() {
	if ( ! function_exists( 'cs_looper_provider_register' ) ) {
		return;
	}

	cs_looper_provider_register( 'product-category-breadcrumb', array(
		'label'  => __( 'Product Category Breadcrumb', 'pro-child' ),
		'values' => array(),
		'filter' => function( $results, $args = array() ) {
			$product_id = get_queried_object_id();
			$product    = $product_id ? wc_get_product( $product_id ) : false;

			if ( ! $product ) {
				return array();
			}

			$terms = get_the_terms( $product_id, 'product_cat' );

			$deepest_term = null;
			$deepest_path = array();

			if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
				foreach ( $terms as $term ) {
					$ancestor_ids = get_ancestors(
						$term->term_id,
						'product_cat',
						'taxonomy'
					);

					if ( ! $deepest_term || count( $ancestor_ids ) > count( $deepest_path ) ) {
						$deepest_term = $term;
						$deepest_path = $ancestor_ids;
					}
				}
			}

			$shop_page_id = wc_get_page_id( 'shop' );
			$shop_name    = $shop_page_id > 0
				? get_the_title( $shop_page_id )
				: __( 'Shop', 'woocommerce' );
			$shop_url     = wc_get_page_permalink( 'shop' );

			$trail = array(
				array(
					'name' => $shop_name,
					'url'  => $shop_url ? $shop_url : home_url( '/' ),
				),
			);

			if ( $deepest_term ) {
				foreach ( array_reverse( $deepest_path ) as $ancestor_id ) {
					$ancestor = get_term( $ancestor_id, 'product_cat' );

					if ( $ancestor && ! is_wp_error( $ancestor ) ) {
						$ancestor_url = get_term_link( $ancestor, 'product_cat' );

						if ( ! is_wp_error( $ancestor_url ) ) {
							$trail[] = array(
								'name' => $ancestor->name,
								'url'  => $ancestor_url,
							);
						}
					}
				}

				$deepest_url = get_term_link( $deepest_term, 'product_cat' );

				if ( ! is_wp_error( $deepest_url ) ) {
					$trail[] = array(
						'name' => $deepest_term->name,
						'url'  => $deepest_url,
					);
				}
			}

			$trail[] = array(
				'name' => get_the_title( $product_id ),
				'url'  => get_permalink( $product_id ),
			);

			return $trail;
		},
	) );
}
add_action( 'after_setup_theme', 'register_product_category_breadcrumb_looper', 21 );

Hey @splaquet,

Thank you for sharing this with the community, @splaquet. A Looper Provider that walks the product category hierarchy is a tidy way to get a WooCommerce category breadcrumb out of the native Breadcrumbs (Elements) element, and the setup notes (Provider on the wrapper, Consumer “All” on the crumb, plus the First/Last index conditions) make it easy to follow.

Quick Check

  • Anyone using this snippet should be on PHP 8.1+ with Pro/X and Cornerstone updated to the latest versions.

Notes for anyone using it

  1. Add the code to a child theme’s functions.php (or a code snippets plugin) – not the parent theme, so it survives updates.
  2. It reads the page’s current product – so it is intended for single product layouts.
  3. Please note that custom code shared on the forum is used as-is. Help adapting or extending custom PHP falls under our Custom Coding tier, available to ONE package subscribers.

Thanks again for contributing.

Best Regards.