Commit f3df9b92120 for woocommerce
commit f3df9b92120bcb14bd1aeb53e00fad50490332ef
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date: Thu Dec 3 13:53:04 2026 +0100
Register global product attributes during early init callbacks (#71692)
* Remove redundant attribute taxonomy registration from the create ability
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Register a new attribute's taxonomy from early init callbacks
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Test early attribute terms across taxonomy registration
Replay the priority-4 ability call and the priority-5 registration so the test catches both a failed insert and a term lost during registration.
* Publish the attribute taxonomy filter timing change
Extension developers need the create and update timing in the release notes. A comment-only entry would omit it.
---------
Co-authored-by: aaronfc <aaron.fas@automattic.com>
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/dev-remove-attribute-create-taxonomy-registration b/plugins/woocommerce/changelog/dev-remove-attribute-create-taxonomy-registration
new file mode 100644
index 00000000000..83bbbc2ce75
--- /dev/null
+++ b/plugins/woocommerce/changelog/dev-remove-attribute-create-taxonomy-registration
@@ -0,0 +1,4 @@
+Significance: patch
+Type: tweak
+
+Run product attribute taxonomy filters on create and update, including during early init callbacks.
diff --git a/plugins/woocommerce/includes/class-wc-post-types.php b/plugins/woocommerce/includes/class-wc-post-types.php
index e7846e9cafc..199979aa47a 100644
--- a/plugins/woocommerce/includes/class-wc-post-types.php
+++ b/plugins/woocommerce/includes/class-wc-post-types.php
@@ -368,7 +368,9 @@ class WC_Post_Types {
}
/**
- * Register a saved attribute's taxonomy again in this request. Does nothing before init registers taxonomies.
+ * Register a saved attribute's taxonomy again in this request. Does nothing before init.
+ *
+ * Runs from early init callbacks too, so the attribute can take terms before register_taxonomies() runs at priority 5.
*
* @internal
*
@@ -377,7 +379,7 @@ class WC_Post_Types {
* @return void
*/
public static function refresh_attribute_taxonomy( int $attribute_id ): void {
- if ( ! did_action( 'woocommerce_after_register_taxonomy' ) ) {
+ if ( ! did_action( 'init' ) ) {
return;
}
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributeCreate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributeCreate.php
index 244bb13b85f..880e1226fdd 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributeCreate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductAttributeCreate.php
@@ -102,7 +102,7 @@ class ProductAttributeCreate extends AbstractDomainAbility implements AbilityDef
return new \WP_Error( 'woocommerce_ability_product_attribute_slug_empty', __( 'The attribute needs a slug with letters or numbers. Send one, or a name that has some.', 'woocommerce' ), array( 'status' => 400 ) );
}
$existing_id = wc_attribute_taxonomy_id_by_name( $slug );
- // wc_create_attribute() only checks registered taxonomies, which misses an attribute created earlier in this request.
+ // Check the attribute table, not only registered taxonomies as wc_create_attribute() does, and report the existing ID.
if ( $existing_id > 0 ) {
return new \WP_Error(
self::CREATE_ERRORS['invalid_product_attribute_slug_already_exists'],
@@ -160,8 +160,6 @@ class ProductAttributeCreate extends AbstractDomainAbility implements AbilityDef
return new \WP_Error( 'woocommerce_ability_product_attribute_create_failed', __( 'The attribute was created but could not be read back.', 'woocommerce' ), array( 'status' => 500 ) );
}
- self::register_attribute_taxonomy( $attributes[ 'id:' . $id ] );
-
$attribute = self::format_product_attribute( $attributes[ 'id:' . $id ], 0 );
if ( is_wp_error( $attribute ) ) {
return $attribute;
@@ -170,72 +168,6 @@ class ProductAttributeCreate extends AbstractDomainAbility implements AbilityDef
return array( 'attribute' => $attribute );
}
- /**
- * Register the attribute taxonomy for the rest of this request.
- *
- * WooCommerce registers attribute taxonomies on init only. Without this, a product
- * write later in the same request, such as the next call of a stdio MCP session,
- * saves the product without the attribute's terms and reports no error.
- *
- * @param \stdClass $attribute Row of wc_get_attribute_taxonomies().
- * @return void
- */
- private static function register_attribute_taxonomy( \stdClass $attribute ): void {
- global $wc_product_attributes;
-
- $taxonomy = wc_attribute_taxonomy_name( (string) $attribute->attribute_name );
- if ( taxonomy_exists( $taxonomy ) ) {
- return;
- }
-
- $label = (string) $attribute->attribute_label;
- $public = 1 === (int) $attribute->attribute_public;
- register_taxonomy(
- $taxonomy,
- /**
- * Filters the object types of a global attribute taxonomy.
- *
- * @since 2.3.0
- * @param array $object_types Object types.
- */
- apply_filters( "woocommerce_taxonomy_objects_{$taxonomy}", array( 'product' ) ),
- /**
- * Filters the registration arguments of a global attribute taxonomy.
- *
- * @since 2.3.0
- * @param array $args Taxonomy arguments.
- */
- apply_filters(
- "woocommerce_taxonomy_args_{$taxonomy}",
- array(
- 'hierarchical' => false,
- 'update_count_callback' => '_update_post_term_count',
- 'labels' => array(
- 'name' => $label,
- 'singular_name' => $label,
- ),
- 'show_ui' => true,
- 'show_in_quick_edit' => false,
- 'show_in_menu' => false,
- 'meta_box_cb' => false,
- 'query_var' => $public,
- 'rewrite' => false,
- 'sort' => false,
- 'public' => $public,
- 'show_in_nav_menus' => false,
- 'capabilities' => array(
- 'manage_terms' => 'manage_product_terms',
- 'edit_terms' => 'edit_product_terms',
- 'delete_terms' => 'delete_product_terms',
- 'assign_terms' => 'assign_product_terms',
- ),
- )
- )
- );
-
- $wc_product_attributes[ $taxonomy ] = $attribute;
- }
-
/**
* Get the input schema.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributeCreateTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributeCreateTest.php
index 68e1d05306e..18acdbbfd9c 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributeCreateTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductAttributeCreateTest.php
@@ -94,6 +94,36 @@ class ProductAttributeCreateTest extends WC_Unit_Test_Case {
$this->assertTrue( taxonomy_exists( 'pa_color' ), 'Later writes in this request can assign its terms.' );
}
+ /**
+ * @testdox A term added at init priority 4 survives WooCommerce's taxonomy registration at priority 5.
+ */
+ public function test_early_attribute_term_survives_taxonomy_registration(): void {
+ global $wp_actions;
+
+ // The test suite has already run init. Replay only the callbacks needed for this test.
+ remove_all_actions( 'init' );
+ unset( $wp_actions['init'], $wp_actions['woocommerce_after_register_taxonomy'] );
+ unregister_taxonomy( 'product_type' );
+
+ $term = null;
+ add_action(
+ 'init',
+ function () use ( &$term ) {
+ $this->assertIsArray( $this->sut->execute( array( 'name' => 'Early color' ) ) );
+ $term = wp_insert_term( 'Red', 'pa_early-color' );
+ $this->assertNotWPError( $term );
+ },
+ 4
+ );
+ add_action( 'init', array( \WC_Post_Types::class, 'register_taxonomies' ), 5 );
+
+ do_action( 'init' );
+
+ $this->assertSame( 2, did_action( 'registered_taxonomy_pa_early-color' ) );
+ $this->assertTrue( is_object_in_taxonomy( 'product', 'pa_early-color' ) );
+ $this->assertSame( $term['term_id'], get_term_by( 'slug', 'red', 'pa_early-color' )->term_id );
+ }
+
/**
* @testdox Should store the slug, sort order and archives it is given, without a pa_ prefix on the slug.
*/