Automattic\WooCommerce\Internal\ProductAttributes

VisualAttributeTermAdmin::seed_visual_attribute_terms │ public static │ WC 1.0

Seed default color terms for a newly created wc-visual attribute.

Method of the class: VisualAttributeTermAdmin{}

Returns

void. Nothing (null).

Usage

$result = VisualAttributeTermAdmin::seed_visual_attribute_terms( $attribute_id ): void;
$attribute_id(int) (required)
Attribute ID.

VisualAttributeTermAdmin::seed_visual_attribute_terms() code WC 11.1.2

public static function seed_visual_attribute_terms( int $attribute_id ): void {
	if ( 0 >= $attribute_id ) {
		return;
	}

	$attribute = wc_get_attribute( $attribute_id );

	if (
		! $attribute ||
		! isset( $attribute->slug, $attribute->type ) ||
		'wc-visual' !== $attribute->type
	) {
		return;
	}

	$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;
		}

		$result = wp_insert_term( $term['label'], $taxonomy, array( 'slug' => $slug ) );

		if ( is_wp_error( $result ) || empty( $result['term_id'] ) ) {
			continue;
		}

		VisualAttributeTermMeta::save_term_visual( (int) $result['term_id'], $term['color'], 0 );
	}
}