Automattic\WooCommerce\Blocks
BlockTypesController::set_product_breadcrumbs_preferred_taxonomy
Set the preferred taxonomy and term for the breadcrumbs block on the product post type.
This method mimics the behavior of WC_Breadcrumb::add_crumbs_single() to ensure consistent breadcrumb term selection between WooCommerce's legacy breadcrumbs and the Core breadcrumbs block.
Method of the class: BlockTypesController{}
Hooks from the method
Returns
Array. The settings for the breadcrumbs block.
Usage
$BlockTypesController = new BlockTypesController(); $BlockTypesController->set_product_breadcrumbs_preferred_taxonomy( $settings, $post_type, $post_id );
- $settings(array) (required)
- The settings for the breadcrumbs block.
- $post_type(string) (required)
- The post type.
- $post_id(int)
- The current post ID.
BlockTypesController::set_product_breadcrumbs_preferred_taxonomy() BlockTypesController::set product breadcrumbs preferred taxonomy code WC 10.9.4
public function set_product_breadcrumbs_preferred_taxonomy( $settings, $post_type, $post_id = 0 ) {
if ( ! is_array( $settings ) || 'product' !== $post_type ) {
return $settings;
}
$settings['taxonomy'] = 'product_cat';
// If we have a post ID, determine the specific term using WooCommerce's logic.
if ( ! empty( $post_id ) ) {
$terms = wc_get_product_terms(
$post_id,
'product_cat',
/**
* Filters the arguments used to fetch product terms for breadcrumbs.
*
* @since 9.5.0
*
* @param array $args Array of arguments for `wc_get_product_terms()`.
*/
apply_filters(
'woocommerce_breadcrumb_product_terms_args',
array(
'orderby' => 'parent',
'order' => 'DESC',
)
)
);
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
/**
* Filters the main term used in product breadcrumbs.
*
* @since 9.5.0
*
* @param \WP_Term $main_term The main term to be used in breadcrumbs.
* @param \WP_Term[] $terms Array of all product category terms.
*/
$main_term = apply_filters( 'woocommerce_breadcrumb_main_term', $terms[0], $terms );
if ( $main_term instanceof \WP_Term ) {
$settings['term'] = $main_term->slug;
}
}
}
return $settings;
}