Commit 7e78c66934 for woocommerce
commit 7e78c66934955c84c4a62f5005bed6e2ba2e446b
Author: Karol Manijak <20098064+kmanijak@users.noreply.github.com>
Date: Mon Jan 12 13:40:05 2026 +0100
Set preferred term in core Breadrcumbs block to keep compatibility with Woo's block (#62746)
* Set preferred term for core breadrcumbs
* Pass term slug instead of id
* Add filters docblocks
* Add changelog
* Fix the docblock placement
* Satisfy PHPStan complaints
* Another PHPStan compaint
* Fix typo in changelog
diff --git a/plugins/woocommerce/changelog/add-breadcrumbs-extensibility-points b/plugins/woocommerce/changelog/add-breadcrumbs-extensibility-points
new file mode 100644
index 0000000000..779350629c
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-breadcrumbs-extensibility-points
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Breadcrumbs: Set preferred term in core Breadcrumbs block to keep compatibility with Woo's block
diff --git a/plugins/woocommerce/src/Blocks/BlockTypesController.php b/plugins/woocommerce/src/Blocks/BlockTypesController.php
index cc20975783..c7750bd2fa 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypesController.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypesController.php
@@ -63,7 +63,7 @@ final class BlockTypesController {
add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_legacy_widgets_with_block_equivalent' ) );
add_action( 'woocommerce_delete_product_transients', array( $this, 'delete_product_transients' ) );
add_filter( 'register_block_type_args', array( $this, 'enqueue_block_style_for_classic_themes' ), 10, 2 );
- add_filter( 'block_core_breadcrumbs_post_type_settings', array( $this, 'set_product_breadcrumbs_preferred_taxonomy' ), 10, 2 );
+ add_filter( 'block_core_breadcrumbs_post_type_settings', array( $this, 'set_product_breadcrumbs_preferred_taxonomy' ), 10, 3 );
}
/**
@@ -671,20 +671,64 @@ final class BlockTypesController {
}
/**
- * Change the preferred taxonomy for the breadcrumbs block on the product post type.
+ * 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.
*
* @param array $settings The settings for the breadcrumbs block.
* @param string $post_type The post type.
+ * @param int $post_id The current post ID.
* @return array The settings for the breadcrumbs block.
*
* @internal
*/
- public function set_product_breadcrumbs_preferred_taxonomy( $settings, $post_type ) {
+ 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;
}
}