Commit a8283504d03 for woocommerce
commit a8283504d0345150d0371567a12e86048b628424
Author: Tung Du <dinhtungdu@gmail.com>
Date: Tue Nov 17 10:20:53 2026 +0700
Register a new attribute's taxonomy in the same request (#70704)
* Move attribute taxonomy registration into its own method
Pull the body of the init loop in WC_Post_Types::register_taxonomies()
into a private register_attribute_taxonomy() method. No behavior change.
* Register a new attribute's taxonomy in the same request
wc_create_attribute() and wc_update_attribute() now register the pa_*
taxonomy right away, through the same method init uses, before the
woocommerce_attribute_added and woocommerce_attribute_updated hooks
fire. Code can add terms right after creating an attribute, and a
second create of the same slug in one request is rejected.
On update, object types that other code attached are kept. On a slug
rename, the old taxonomy's object types move to the new one, queued
term counts are flushed, and the old taxonomy is unregistered instead
of being aliased under the new name.
Amp-Thread-ID: https://ampcode.com/threads/T-01a1a9b3-46b8-7626-975f-3b135894acdb
Co-authored-by: Amp <amp@ampcode.com>
* Remove hand-rolled attribute taxonomy registrations
wc_create_attribute() now registers the taxonomy itself, so the CSV
importer, the WXR importer, the deprecated post importer hook, the CLI
migrator, visual attribute seeding and the test helpers no longer
register it with their own arguments. The CLI migrator now builds the
slug with wc_sanitize_taxonomy_name(), as wc_create_attribute() does.
Amp-Thread-ID: https://ampcode.com/threads/T-01a1a9b3-46b8-7626-975f-3b135894acdb
Co-authored-by: Amp <amp@ampcode.com>
---------
Co-authored-by: Amp <amp@ampcode.com>
diff --git a/plugins/woocommerce/changelog/wooplug-8106-register-attribute-taxonomy b/plugins/woocommerce/changelog/wooplug-8106-register-attribute-taxonomy
new file mode 100644
index 00000000000..4a17db7b20b
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooplug-8106-register-attribute-taxonomy
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix new product attributes rejecting values added right after creation, and stop duplicate attributes from being saved.
diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-importers.php b/plugins/woocommerce/includes/admin/class-wc-admin-importers.php
index 8ef1dbc0cfa..da294fb8a70 100644
--- a/plugins/woocommerce/includes/admin/class-wc-admin-importers.php
+++ b/plugins/woocommerce/includes/admin/class-wc-admin-importers.php
@@ -194,7 +194,7 @@ class WC_Admin_Importers {
if ( ! taxonomy_exists( $term['domain'] ) ) {
$attribute_name = wc_attribute_taxonomy_slug( $term['domain'] );
- // Create the taxonomy.
+ // Creating the attribute also registers its taxonomy.
if ( ! in_array( $attribute_name, wc_get_attribute_taxonomies(), true ) ) {
wc_create_attribute(
array(
@@ -206,23 +206,6 @@ class WC_Admin_Importers {
)
);
}
-
- // Register the taxonomy now so that the import works!
- register_taxonomy(
- $term['domain'],
- // phpcs:ignore
- apply_filters( 'woocommerce_taxonomy_objects_' . $term['domain'], array( 'product' ) ),
- // phpcs:ignore
- apply_filters(
- 'woocommerce_taxonomy_args_' . $term['domain'],
- array(
- 'hierarchical' => true,
- 'show_ui' => false,
- 'query_var' => true,
- 'rewrite' => false,
- )
- )
- );
}
}
}
diff --git a/plugins/woocommerce/includes/class-wc-post-types.php b/plugins/woocommerce/includes/class-wc-post-types.php
index d2153e06f26..e7846e9cafc 100644
--- a/plugins/woocommerce/includes/class-wc-post-types.php
+++ b/plugins/woocommerce/includes/class-wc-post-types.php
@@ -256,75 +256,153 @@ class WC_Post_Types {
global $wc_product_attributes;
$wc_product_attributes = array();
- $attribute_taxonomies = wc_get_attribute_taxonomies();
- if ( $attribute_taxonomies ) {
- foreach ( $attribute_taxonomies as $tax ) {
- $name = wc_attribute_taxonomy_name( $tax->attribute_name );
+ foreach ( wc_get_attribute_taxonomies() as $tax ) {
+ self::register_attribute_taxonomy( $tax, $permalinks );
+ }
- if ( $name ) {
- $tax->attribute_public = absint( isset( $tax->attribute_public ) ? $tax->attribute_public : 1 );
- $label = ! empty( $tax->attribute_label ) ? $tax->attribute_label : $tax->attribute_name;
- $wc_product_attributes[ $name ] = $tax;
- $taxonomy_data = array(
- 'hierarchical' => false,
- 'update_count_callback' => '_update_post_term_count',
- 'labels' => array(
- /* translators: %s: attribute name */
- 'name' => sprintf( _x( 'Product %s', 'Product Attribute', 'woocommerce' ), $label ),
- 'singular_name' => $label,
- /* translators: %s: attribute name */
- 'search_items' => sprintf( __( 'Search %s', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'all_items' => sprintf( __( 'All %s', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'parent_item' => sprintf( __( 'Parent %s', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'parent_item_colon' => sprintf( __( 'Parent %s:', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'edit_item' => sprintf( __( 'Edit %s', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'update_item' => sprintf( __( 'Update %s', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'add_new_item' => sprintf( __( 'Add new %s', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'new_item_name' => sprintf( __( 'New %s', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'not_found' => sprintf( __( 'No "%s" found', 'woocommerce' ), $label ),
- /* translators: %s: attribute name */
- 'back_to_items' => sprintf( __( '← Back to "%s" attributes', 'woocommerce' ), $label ),
- ),
- 'show_ui' => true,
- 'show_in_quick_edit' => false,
- 'show_in_menu' => false,
- 'meta_box_cb' => false,
- 'query_var' => 1 === $tax->attribute_public,
- 'rewrite' => false,
- 'sort' => false,
- 'public' => 1 === $tax->attribute_public,
- 'show_in_nav_menus' => 1 === $tax->attribute_public && apply_filters( 'woocommerce_attribute_show_in_nav_menus', false, $name ),
- 'capabilities' => array(
- 'manage_terms' => 'manage_product_terms',
- 'edit_terms' => 'edit_product_terms',
- 'delete_terms' => 'delete_product_terms',
- 'assign_terms' => 'assign_product_terms',
- ),
- );
+ /**
+ * Fires after WooCommerce registers its taxonomies, including product attribute taxonomies.
+ *
+ * @since 2.1.0
+ */
+ do_action( 'woocommerce_after_register_taxonomy' );
+ }
- if ( 1 === $tax->attribute_public && sanitize_title( $tax->attribute_name ) ) {
- $taxonomy_data['rewrite'] = array(
- 'slug' => trailingslashit( $permalinks['attribute_rewrite_slug'] ) . urldecode( sanitize_title( $tax->attribute_name ) ),
- 'with_front' => false,
- 'hierarchical' => true,
- );
- }
+ /**
+ * Register one attribute taxonomy. Init and refresh_attribute_taxonomy() both use this.
+ *
+ * @param stdClass $tax Attribute row from wc_get_attribute_taxonomies().
+ * @param array|null $permalinks Permalink structure, so a loop reads it only once.
+ * @return void
+ */
+ private static function register_attribute_taxonomy( $tax, ?array $permalinks = null ): void {
+ global $wc_product_attributes;
- register_taxonomy( $name, apply_filters( "woocommerce_taxonomy_objects_{$name}", array( 'product' ) ), apply_filters( "woocommerce_taxonomy_args_{$name}", $taxonomy_data ) );
- }
- }
+ $name = wc_attribute_taxonomy_name( $tax->attribute_name );
+
+ if ( ! $name ) {
+ return;
}
- do_action( 'woocommerce_after_register_taxonomy' );
+ $tax->attribute_public = absint( isset( $tax->attribute_public ) ? $tax->attribute_public : 1 );
+ $label = ! empty( $tax->attribute_label ) ? $tax->attribute_label : $tax->attribute_name;
+ $wc_product_attributes[ $name ] = $tax;
+ $taxonomy_data = array(
+ 'hierarchical' => false,
+ 'update_count_callback' => '_update_post_term_count',
+ 'labels' => array(
+ /* translators: %s: attribute name */
+ 'name' => sprintf( _x( 'Product %s', 'Product Attribute', 'woocommerce' ), $label ),
+ 'singular_name' => $label,
+ /* translators: %s: attribute name */
+ 'search_items' => sprintf( __( 'Search %s', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'all_items' => sprintf( __( 'All %s', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'parent_item' => sprintf( __( 'Parent %s', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'parent_item_colon' => sprintf( __( 'Parent %s:', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'edit_item' => sprintf( __( 'Edit %s', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'update_item' => sprintf( __( 'Update %s', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'add_new_item' => sprintf( __( 'Add new %s', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'new_item_name' => sprintf( __( 'New %s', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'not_found' => sprintf( __( 'No "%s" found', 'woocommerce' ), $label ),
+ /* translators: %s: attribute name */
+ 'back_to_items' => sprintf( __( '← Back to "%s" attributes', 'woocommerce' ), $label ),
+ ),
+ 'show_ui' => true,
+ 'show_in_quick_edit' => false,
+ 'show_in_menu' => false,
+ 'meta_box_cb' => false,
+ 'query_var' => 1 === $tax->attribute_public,
+ 'rewrite' => false,
+ 'sort' => false,
+ 'public' => 1 === $tax->attribute_public,
+ /**
+ * Filters whether a public product attribute taxonomy shows in navigation menus.
+ *
+ * @since 2.1.0
+ * @param bool $show_in_nav_menus Whether to show the taxonomy in navigation menus.
+ * @param string $name Attribute taxonomy name, for example pa_color.
+ */
+ 'show_in_nav_menus' => 1 === $tax->attribute_public && apply_filters( 'woocommerce_attribute_show_in_nav_menus', false, $name ),
+ 'capabilities' => array(
+ 'manage_terms' => 'manage_product_terms',
+ 'edit_terms' => 'edit_product_terms',
+ 'delete_terms' => 'delete_product_terms',
+ 'assign_terms' => 'assign_product_terms',
+ ),
+ );
+
+ if ( 1 === $tax->attribute_public && sanitize_title( $tax->attribute_name ) ) {
+ $taxonomy_data['rewrite'] = array(
+ 'slug' => trailingslashit( ( $permalinks ?? wc_get_permalink_structure() )['attribute_rewrite_slug'] ) . urldecode( sanitize_title( $tax->attribute_name ) ),
+ 'with_front' => false,
+ 'hierarchical' => true,
+ );
+ }
+
+ register_taxonomy(
+ $name,
+ /**
+ * Filters the object types of a product attribute taxonomy.
+ *
+ * @since 2.1.0
+ * @param array $object_types Object types. Default array( 'product' ).
+ */
+ apply_filters( "woocommerce_taxonomy_objects_{$name}", array( 'product' ) ),
+ /**
+ * Filters the arguments used to register a product attribute taxonomy.
+ *
+ * @since 2.1.0
+ * @param array $taxonomy_data Taxonomy arguments.
+ */
+ apply_filters( "woocommerce_taxonomy_args_{$name}", $taxonomy_data )
+ );
+ }
+
+ /**
+ * Register a saved attribute's taxonomy again in this request. Does nothing before init registers taxonomies.
+ *
+ * @internal
+ *
+ * @since 11.4.0
+ * @param int $attribute_id Attribute ID.
+ * @return void
+ */
+ public static function refresh_attribute_taxonomy( int $attribute_id ): void {
+ if ( ! did_action( 'woocommerce_after_register_taxonomy' ) ) {
+ return;
+ }
+
+ delete_transient( 'wc_attribute_taxonomies' );
+ WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
+
+ $tax = wc_get_attribute_taxonomies()[ 'id:' . $attribute_id ] ?? null;
+ if ( ! $tax ) {
+ return;
+ }
+
+ // Unregister first, or the old query var and rewrite rules stay.
+ $name = wc_attribute_taxonomy_name( $tax->attribute_name );
+ $existing = get_taxonomy( $name );
+ $object_types = $existing ? $existing->object_type : array();
+ if ( $existing ) {
+ unregister_taxonomy( $name );
+ }
+
+ self::register_attribute_taxonomy( $tax );
+
+ // Keep object types that other code attached during this request.
+ foreach ( $object_types as $object_type ) {
+ register_taxonomy_for_object_type( $name, $object_type );
+ }
}
/**
diff --git a/plugins/woocommerce/includes/import/abstract-wc-product-importer.php b/plugins/woocommerce/includes/import/abstract-wc-product-importer.php
index 11e14657104..eda6a0c869f 100644
--- a/plugins/woocommerce/includes/import/abstract-wc-product-importer.php
+++ b/plugins/woocommerce/includes/import/abstract-wc-product-importer.php
@@ -847,8 +847,6 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
* @throws Exception If taxonomy cannot be loaded.
*/
public function get_attribute_taxonomy_id( $raw_name ) {
- global $wpdb, $wc_product_attributes;
-
$attribute_name = $this->get_attribute_taxonomy_name_from_raw_name( $raw_name );
$attribute_id = $this->get_existing_attribute_taxonomy_id( $raw_name );
@@ -872,32 +870,6 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
throw new Exception( $attribute_id->get_error_message(), 400 );
}
- // Register as taxonomy while importing.
- $taxonomy_name = wc_attribute_taxonomy_name( $attribute_name );
- register_taxonomy(
- $taxonomy_name,
- apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ),
- apply_filters(
- 'woocommerce_taxonomy_args_' . $taxonomy_name,
- array(
- 'labels' => array(
- 'name' => $raw_name,
- ),
- 'hierarchical' => true,
- 'show_ui' => false,
- 'query_var' => true,
- 'rewrite' => false,
- )
- )
- );
-
- // Set product attributes global.
- $wc_product_attributes = array();
-
- foreach ( wc_get_attribute_taxonomies() as $taxonomy ) {
- $wc_product_attributes[ wc_attribute_taxonomy_name( $taxonomy->attribute_name ) ] = $taxonomy;
- }
-
return $attribute_id;
}
diff --git a/plugins/woocommerce/includes/wc-attribute-functions.php b/plugins/woocommerce/includes/wc-attribute-functions.php
index 34a1d6af370..643b6a1717a 100644
--- a/plugins/woocommerce/includes/wc-attribute-functions.php
+++ b/plugins/woocommerce/includes/wc-attribute-functions.php
@@ -614,6 +614,9 @@ function wc_create_attribute( $args ) {
$id = $wpdb->insert_id;
+ // Register now so hook listeners can add terms.
+ WC_Post_Types::refresh_attribute_taxonomy( $id );
+
/**
* Attribute added.
*
@@ -637,6 +640,8 @@ function wc_create_attribute( $args ) {
// Set old slug to check for database changes.
$old_slug = ! empty( $args['old_slug'] ) ? wc_sanitize_taxonomy_name( $args['old_slug'] ) : $slug;
+ WC_Post_Types::refresh_attribute_taxonomy( $id );
+
/**
* Attribute updated.
*
@@ -694,15 +699,20 @@ function wc_create_attribute( $args ) {
array( 'meta_key' => 'attribute_pa_' . sanitize_title( $old_slug ) ) // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This one-time attribute migration matches an indexed postmeta key.
);
- // Update global vars to reflect migration. This ensures any functions dealing with terms later in this request
- // use the correct info.
+ // The terms moved to the new taxonomy, so drop the old one.
global $wc_product_attributes;
- if ( isset( $wc_product_attributes[ $old_taxonomy_name ] ) && ! isset( $wc_product_attributes[ $new_taxonomy_name ] ) ) {
- $wc_product_attributes[ $new_taxonomy_name ] = $wc_product_attributes[ $old_taxonomy_name ];
- }
- global $wp_taxonomies;
- if ( isset( $wp_taxonomies[ $old_taxonomy_name ] ) && ! isset( $wp_taxonomies[ $new_taxonomy_name ] ) ) {
- $wp_taxonomies[ $new_taxonomy_name ] = $wp_taxonomies[ $old_taxonomy_name ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
+ unset( $wc_product_attributes[ $old_taxonomy_name ] );
+ $old_taxonomy = get_taxonomy( $old_taxonomy_name );
+ if ( $old_taxonomy ) {
+ // Keep object types that other code attached to the old taxonomy.
+ foreach ( $old_taxonomy->object_type as $object_type ) {
+ register_taxonomy_for_object_type( $new_taxonomy_name, $object_type );
+ }
+ // Flush queued term counts while the old taxonomy still exists, as wc_delete_attribute() does.
+ if ( wp_defer_term_counting() ) {
+ wp_update_term_count( array(), '', true );
+ }
+ unregister_taxonomy( $old_taxonomy_name );
}
}//end if
}//end if
diff --git a/plugins/woocommerce/src/Internal/CLI/Migrator/Core/WooCommerceProductImporter.php b/plugins/woocommerce/src/Internal/CLI/Migrator/Core/WooCommerceProductImporter.php
index f5f1582b85a..d3cb45d71b2 100644
--- a/plugins/woocommerce/src/Internal/CLI/Migrator/Core/WooCommerceProductImporter.php
+++ b/plugins/woocommerce/src/Internal/CLI/Migrator/Core/WooCommerceProductImporter.php
@@ -638,7 +638,7 @@ class WooCommerceProductImporter {
continue;
}
- $taxonomy_slug = sanitize_title( $attr_name );
+ $taxonomy_slug = wc_sanitize_taxonomy_name( $attr_name );
$taxonomy_name = 'pa_' . $taxonomy_slug;
$attribute_id = 0;
@@ -656,37 +656,6 @@ class WooCommerceProductImporter {
wc_get_logger()->warning( "Failed to create attribute '{$attr_name}': " . $attribute_id->get_error_message(), array( 'source' => 'wc-migrator' ) );
continue;
}
-
- register_taxonomy(
- $taxonomy_name,
- /**
- * Filters the object types associated with the attribute taxonomy.
- *
- * @since 10.2.0
- * @param array $object_types Array of object types.
- */
- apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ),
- /**
- * Filters the arguments for registering the attribute taxonomy.
- *
- * @since 10.2.0
- * @param array $args Array of taxonomy registration arguments.
- */
- apply_filters(
- 'woocommerce_taxonomy_args_' . $taxonomy_name,
- array(
- 'labels' => array(
- 'name' => $attr_name,
- ),
- 'hierarchical' => false,
- 'show_ui' => false,
- 'show_in_rest' => true,
- 'query_var' => true,
- 'rewrite' => false,
- 'public' => false,
- )
- )
- );
} else {
$attribute_id = wc_attribute_taxonomy_id_by_name( $taxonomy_name );
}
diff --git a/plugins/woocommerce/src/Internal/Integrations/WPPostsImporter.php b/plugins/woocommerce/src/Internal/Integrations/WPPostsImporter.php
index 5e33f2de4be..3fc3c5569dc 100644
--- a/plugins/woocommerce/src/Internal/Integrations/WPPostsImporter.php
+++ b/plugins/woocommerce/src/Internal/Integrations/WPPostsImporter.php
@@ -50,7 +50,7 @@ class WPPostsImporter {
$attribute_name = wc_attribute_taxonomy_slug( $term['domain'] );
- // Create the taxonomy.
+ // Creating the attribute also registers its taxonomy.
if ( ! in_array( $attribute_name, wc_get_attribute_taxonomies(), true ) ) {
wc_create_attribute(
array(
@@ -62,23 +62,6 @@ class WPPostsImporter {
)
);
}
-
- // Register the taxonomy so that the import works.
- register_taxonomy(
- $term['domain'],
- // phpcs:ignore
- apply_filters( 'woocommerce_taxonomy_objects_' . $term['domain'], array( 'product' ) ),
- // phpcs:ignore
- apply_filters(
- 'woocommerce_taxonomy_args_' . $term['domain'],
- array(
- 'hierarchical' => true,
- 'show_ui' => false,
- 'query_var' => true,
- 'rewrite' => false,
- )
- )
- );
}
}
diff --git a/plugins/woocommerce/src/Internal/ProductAttributes/VisualAttributeTermAdmin.php b/plugins/woocommerce/src/Internal/ProductAttributes/VisualAttributeTermAdmin.php
index 4031e5aced2..d14b3a546bb 100644
--- a/plugins/woocommerce/src/Internal/ProductAttributes/VisualAttributeTermAdmin.php
+++ b/plugins/woocommerce/src/Internal/ProductAttributes/VisualAttributeTermAdmin.php
@@ -511,37 +511,6 @@ class VisualAttributeTermAdmin implements RegisterHooksInterface {
$taxonomy = $attribute->slug;
- // Taxonomy is registered on init from the cached list but not yet available
- // at process_add_attribute time (cache invalidated after wc_create_attribute).
- // Use the same WC filter seams as WC_Post_Types::register_taxonomies().
- if ( ! taxonomy_exists( $taxonomy ) ) {
- /**
- * Filters the object types for a WooCommerce taxonomy.
- *
- * @param array $objects Object types.
- *
- * @since 11.0.0
- */
- $objects = apply_filters( "woocommerce_taxonomy_objects_{$taxonomy}", array( 'product' ) );
- /**
- * Filters the arguments for a WooCommerce taxonomy.
- *
- * @param array $args Taxonomy arguments.
- *
- * @since 11.0.0
- */
- $args = apply_filters(
- "woocommerce_taxonomy_args_{$taxonomy}",
- array(
- 'hierarchical' => false,
- 'public' => false,
- 'show_ui' => false,
- 'show_in_menu' => false,
- )
- );
- register_taxonomy( $taxonomy, $objects, $args );
- }
-
foreach ( self::get_default_color_terms() as $slug => $term ) {
if ( get_term_by( 'slug', $slug, $taxonomy ) ) {
continue;
diff --git a/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php b/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php
index 94a7adbc8df..d907215cdd3 100644
--- a/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php
+++ b/plugins/woocommerce/tests/legacy/framework/helpers/class-wc-helper-product.php
@@ -412,8 +412,6 @@ class WC_Helper_Product {
* @return array
*/
public static function create_attribute( $raw_name = 'size', $terms = array( 'small' ) ) {
- global $wpdb, $wc_product_attributes;
-
// Make sure caches are clean.
delete_transient( 'wc_attribute_taxonomies' );
WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
@@ -443,31 +441,6 @@ class WC_Helper_Product {
'has_archives' => 0,
)
);
-
- // Register as taxonomy.
- register_taxonomy(
- $taxonomy_name,
- apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ),
- apply_filters(
- 'woocommerce_taxonomy_args_' . $taxonomy_name,
- array(
- 'labels' => array(
- 'name' => $raw_name,
- ),
- 'hierarchical' => false,
- 'show_ui' => false,
- 'query_var' => true,
- 'rewrite' => false,
- )
- )
- );
-
- // Set product attributes global.
- $wc_product_attributes = array();
-
- foreach ( wc_get_attribute_taxonomies() as $taxonomy ) {
- $wc_product_attributes[ wc_attribute_taxonomy_name( $taxonomy->attribute_name ) ] = $taxonomy;
- }
}
$attribute = wc_get_attribute( $attribute_id );
diff --git a/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php
index db9459460b6..931a8b3b6ae 100644
--- a/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/wc-attribute-functions-test.php
@@ -547,6 +547,110 @@ class WC_Attribute_Functions_Test extends \WC_Unit_Test_Case {
$this->assertEquals( 'menu_order', $attribute->order_by, 'Any invalid property changes will be reset to their defaults.' );
}
+ /**
+ * @testdox wc_create_attribute() registers the taxonomy before woocommerce_attribute_added fires.
+ */
+ public function test_wc_create_attribute_registers_the_taxonomy_before_the_added_hook(): void {
+ $slug = $this->get_unique_attribute_slug( 'new' );
+ $taxonomy = wc_attribute_taxonomy_name( $slug );
+ $term = null;
+ add_action(
+ 'woocommerce_attribute_added',
+ function () use ( $taxonomy, &$term ) {
+ $term = wp_insert_term( 'Red', $taxonomy );
+ }
+ );
+
+ wc_create_attribute(
+ array(
+ 'name' => 'Color',
+ 'slug' => $slug,
+ )
+ );
+
+ $this->assertIsArray( $term, 'A hook listener should be able to add a term.' );
+ $this->assertSame( 'manage_product_terms', get_taxonomy( $taxonomy )->cap->manage_terms, 'The taxonomy should have the init arguments.' );
+ }
+
+ /**
+ * @testdox wc_create_attribute() rejects a slug that was already created in the same request.
+ */
+ public function test_wc_create_attribute_rejects_the_same_slug_twice_in_one_request(): void {
+ $slug = $this->get_unique_attribute_slug( 'dup' );
+
+ $first = wc_create_attribute(
+ array(
+ 'name' => 'First',
+ 'slug' => $slug,
+ )
+ );
+ $second = wc_create_attribute(
+ array(
+ 'name' => 'Second',
+ 'slug' => $slug,
+ )
+ );
+
+ $this->assertIsInt( $first );
+ $this->assertWPError( $second );
+ $this->assertSame( 'invalid_product_attribute_slug_already_exists', $second->get_error_code() );
+ }
+
+ /**
+ * @testdox wc_update_attribute() keeps object types that other code attached to the taxonomy.
+ */
+ public function test_wc_update_attribute_keeps_attached_object_types(): void {
+ $slug = $this->get_unique_attribute_slug( 'types' );
+ $taxonomy = wc_attribute_taxonomy_name( $slug );
+
+ $attribute_id = wc_create_attribute(
+ array(
+ 'name' => 'Color',
+ 'slug' => $slug,
+ )
+ );
+ register_taxonomy_for_object_type( $taxonomy, 'post' );
+
+ wc_update_attribute( $attribute_id, array( 'name' => 'Colour' ) );
+
+ $this->assertContains( 'post', get_taxonomy( $taxonomy )->object_type, 'The attached object type should stay.' );
+ $this->assertSame( 'Colour', get_taxonomy( $taxonomy )->labels->singular_name, 'The taxonomy should use the new name.' );
+ }
+
+ /**
+ * @testdox wc_update_attribute() registers the new taxonomy and removes the old one when the slug changes.
+ */
+ public function test_wc_update_attribute_slug_rename_registers_the_new_taxonomy(): void {
+ $old_slug = $this->get_unique_attribute_slug( 'old' );
+ $new_slug = $this->get_unique_attribute_slug( 'new' );
+ $old_taxonomy = wc_attribute_taxonomy_name( $old_slug );
+ $new_taxonomy = wc_attribute_taxonomy_name( $new_slug );
+
+ $attribute_id = wc_create_attribute(
+ array(
+ 'name' => 'Color',
+ 'slug' => $old_slug,
+ )
+ );
+ wp_insert_term( 'Red', $old_taxonomy );
+ register_taxonomy_for_object_type( $old_taxonomy, 'post' );
+
+ wc_update_attribute(
+ $attribute_id,
+ array(
+ 'name' => 'Color',
+ 'slug' => $new_slug,
+ )
+ );
+
+ $this->assertFalse( taxonomy_exists( $old_taxonomy ), 'The old taxonomy should be gone.' );
+ $this->assertTrue( taxonomy_exists( $new_taxonomy ), 'The new taxonomy should be registered.' );
+ // A copy of the old taxonomy object under the new name would keep the old name.
+ $this->assertSame( $new_taxonomy, get_taxonomy( $new_taxonomy )->name, 'The new taxonomy should be its own registration.' );
+ $this->assertNotEmpty( term_exists( 'red', $new_taxonomy ), 'The term should move to the new taxonomy.' );
+ $this->assertContains( 'post', get_taxonomy( $new_taxonomy )->object_type, 'Attached object types should move to the new taxonomy.' );
+ }
+
/**
* Test visual attribute type registration and persistence.
*
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Helpers/FixtureData.php b/plugins/woocommerce/tests/php/src/Blocks/Helpers/FixtureData.php
index 6fdd833b41d..9d7c02f53e9 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Helpers/FixtureData.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Helpers/FixtureData.php
@@ -159,8 +159,6 @@ class FixtureData {
* @return array Attribute data and created terms.
*/
public static function get_product_attribute( $raw_name = 'size', $terms = array( 'small' ) ) {
- global $wpdb, $wc_product_attributes;
-
// Make sure caches are clean.
delete_transient( 'wc_attribute_taxonomies' );
\WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
@@ -190,31 +188,6 @@ class FixtureData {
'has_archives' => 0,
)
);
-
- // Register as taxonomy.
- register_taxonomy(
- $taxonomy_name,
- apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ),
- apply_filters(
- 'woocommerce_taxonomy_args_' . $taxonomy_name,
- array(
- 'labels' => array(
- 'name' => $raw_name,
- ),
- 'hierarchical' => false,
- 'show_ui' => false,
- 'query_var' => true,
- 'rewrite' => false,
- )
- )
- );
-
- // Set product attributes global.
- $wc_product_attributes = array();
-
- foreach ( wc_get_attribute_taxonomies() as $taxonomy ) {
- $wc_product_attributes[ wc_attribute_taxonomy_name( $taxonomy->attribute_name ) ] = $taxonomy;
- }
}
$attribute = wc_get_attribute( $attribute_id );
diff --git a/plugins/woocommerce/tests/php/src/Internal/ProductAttributes/VisualAttributeTermAdminTest.php b/plugins/woocommerce/tests/php/src/Internal/ProductAttributes/VisualAttributeTermAdminTest.php
index c62e4977dfd..a42155ccd1b 100644
--- a/plugins/woocommerce/tests/php/src/Internal/ProductAttributes/VisualAttributeTermAdminTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/ProductAttributes/VisualAttributeTermAdminTest.php
@@ -145,9 +145,9 @@ class VisualAttributeTermAdminTest extends WC_Unit_Test_Case {
}
/**
- * @testdox Should auto-register the taxonomy and seed color terms when called before the taxonomy is registered.
+ * @testdox Should seed color terms right after the attribute is created, in the same request.
*/
- public function test_seeds_when_taxonomy_is_not_registered(): void {
+ public function test_seeds_right_after_the_attribute_is_created(): void {
$suffix = self::get_unique_suffix();
$attribute_data = array(
'name' => 'Seed Visual Unregistered ' . $suffix,
@@ -161,20 +161,11 @@ class VisualAttributeTermAdminTest extends WC_Unit_Test_Case {
$taxonomy = wc_attribute_taxonomy_name( $attribute_data['slug'] );
$term_ids = array();
- // Pre-assert the fallback branch is actually exercised; if WC auto-registers
- // the taxonomy on init the test would silently bypass the code under test.
- $this->assertFalse(
- taxonomy_exists( $taxonomy ),
- 'Taxonomy should not be registered before the seeder runs; otherwise the fallback branch is not exercised.'
- );
-
try {
VisualAttributeTermAdmin::seed_visual_attribute_terms(
$attribute_id
);
- $this->assertTrue( taxonomy_exists( $taxonomy ), 'Seeder should register the taxonomy when missing.' );
-
$terms = get_terms(
array(
'taxonomy' => $taxonomy,
@@ -183,7 +174,7 @@ class VisualAttributeTermAdminTest extends WC_Unit_Test_Case {
);
$this->assertIsArray( $terms, 'Terms should be returned for the taxonomy.' );
- $this->assertCount( 9, $terms, 'Nine default color terms should be seeded after auto-registering the taxonomy.' );
+ $this->assertCount( 9, $terms, 'Nine default color terms should be seeded.' );
foreach ( $terms as $term ) {
$term_ids[] = (int) $term->term_id;