Automattic\WooCommerce\Internal\CLI\Migrator\Core

WooCommerceProductImporter::setup_attributesprivateWC 1.0

Sets up product attributes for variable products with global taxonomy creation.

Method of the class: WooCommerceProductImporter{}

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->setup_attributes( $product, $attributes_data ): void;
$product(WC_Product_Variable) (required)
The variable product object.
$attributes_data(array) (required)
Standardized attribute data from mapper.

WooCommerceProductImporter::setup_attributes() code WC 10.7.0

private function setup_attributes( WC_Product_Variable $product, array $attributes_data ): void {
	$woo_attributes                  = array();
	$this->current_attribute_mapping = array();

	foreach ( $attributes_data as $attribute_info ) {
		$attr_name    = $attribute_info['name'] ?? null;
		$attr_options = $attribute_info['options'] ?? array();
		if ( empty( $attr_name ) || empty( $attr_options ) ) {
			continue;
		}

		$taxonomy_slug = sanitize_title( $attr_name );
		$taxonomy_name = 'pa_' . $taxonomy_slug;
		$attribute_id  = 0;

		if ( ! taxonomy_exists( $taxonomy_name ) ) {
			$attribute_id = wc_create_attribute(
				array(
					'name'         => $attr_name,
					'slug'         => $taxonomy_slug,
					'type'         => 'select',
					'order_by'     => 'menu_order',
					'has_archives' => false,
				)
			);
			if ( is_wp_error( $attribute_id ) ) {
				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 );
		}

		$term_ids   = array();
		$term_slugs = array();
		foreach ( $attr_options as $value ) {
			$term_slug = sanitize_title( $value );
			$term      = get_term_by( 'slug', $term_slug, $taxonomy_name );
			if ( ! $term ) {
				$term_result = wp_insert_term( $value, $taxonomy_name, array( 'slug' => $term_slug ) );
				if ( is_wp_error( $term_result ) ) {
					wc_get_logger()->warning( "Failed to insert term '{$value}' (slug: {$term_slug}) into {$taxonomy_name}: " . $term_result->get_error_message(), array( 'source' => 'wc-migrator' ) );
					continue;
				}
				$term_ids[]   = $term_result['term_id'];
				$term_slugs[] = $term_slug;
			} else {
				$term_ids[]   = $term->term_id;
				$term_slugs[] = $term->slug;
			}
		}

		$woo_attribute = new \WC_Product_Attribute();
		$woo_attribute->set_name( $taxonomy_name );
		$woo_attribute->set_id( $attribute_id );
		$woo_attribute->set_options( $term_ids );
		$woo_attribute->set_position( $attribute_info['position'] ?? 0 );
		$woo_attribute->set_visible( $attribute_info['is_visible'] ?? true );
		$woo_attribute->set_variation( $attribute_info['is_variation'] ?? true );
		$woo_attributes[] = $woo_attribute;

		$this->current_attribute_mapping[ $attr_name ] = $taxonomy_name;
	}

	$product->set_attributes( $woo_attributes );
}