WC_Product_CSV_Importer_Controller::auto_map_columns()protectedWC 1.0

Auto map column names.

Method of the class: WC_Product_CSV_Importer_Controller{}

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->auto_map_columns( $raw_headers, $num_indexes );
$raw_headers(array) (required)
Raw header columns.
$num_indexes(true|false)
If should use numbers or raw header columns as indexes.
Default: true

WC_Product_CSV_Importer_Controller::auto_map_columns() code WC 8.7.0

protected function auto_map_columns( $raw_headers, $num_indexes = true ) {
	$weight_unit_label    = I18nUtil::get_weight_unit_label( get_option( 'woocommerce_weight_unit', 'kg' ) );
	$dimension_unit_label = I18nUtil::get_dimensions_unit_label( get_option( 'woocommerce_dimension_unit', 'cm' ) );

	/*
	 * @hooked wc_importer_generic_mappings - 10
	 * @hooked wc_importer_wordpress_mappings - 10
	 * @hooked wc_importer_default_english_mappings - 100
	 */
	$default_columns = $this->normalize_columns_names(
		apply_filters(
			'woocommerce_csv_product_import_mapping_default_columns',
			array(
				__( 'ID', 'woocommerce' )             => 'id',
				__( 'Type', 'woocommerce' )           => 'type',
				__( 'SKU', 'woocommerce' )            => 'sku',
				__( 'Name', 'woocommerce' )           => 'name',
				__( 'Published', 'woocommerce' )      => 'published',
				__( 'Is featured?', 'woocommerce' )   => 'featured',
				__( 'Visibility in catalog', 'woocommerce' ) => 'catalog_visibility',
				__( 'Short description', 'woocommerce' ) => 'short_description',
				__( 'Description', 'woocommerce' )    => 'description',
				__( 'Date sale price starts', 'woocommerce' ) => 'date_on_sale_from',
				__( 'Date sale price ends', 'woocommerce' ) => 'date_on_sale_to',
				__( 'Tax status', 'woocommerce' )     => 'tax_status',
				__( 'Tax class', 'woocommerce' )      => 'tax_class',
				__( 'In stock?', 'woocommerce' )      => 'stock_status',
				__( 'Stock', 'woocommerce' )          => 'stock_quantity',
				__( 'Backorders allowed?', 'woocommerce' ) => 'backorders',
				__( 'Low stock amount', 'woocommerce' ) => 'low_stock_amount',
				__( 'Sold individually?', 'woocommerce' ) => 'sold_individually',
				/* translators: %s: Weight unit */
				sprintf( __( 'Weight (%s)', 'woocommerce' ), $weight_unit_label ) => 'weight',
				/* translators: %s: Length unit */
				sprintf( __( 'Length (%s)', 'woocommerce' ), $dimension_unit_label ) => 'length',
				/* translators: %s: Width unit */
				sprintf( __( 'Width (%s)', 'woocommerce' ), $dimension_unit_label ) => 'width',
				/* translators: %s: Height unit */
				sprintf( __( 'Height (%s)', 'woocommerce' ), $dimension_unit_label ) => 'height',
				__( 'Allow customer reviews?', 'woocommerce' ) => 'reviews_allowed',
				__( 'Purchase note', 'woocommerce' )  => 'purchase_note',
				__( 'Sale price', 'woocommerce' )     => 'sale_price',
				__( 'Regular price', 'woocommerce' )  => 'regular_price',
				__( 'Categories', 'woocommerce' )     => 'category_ids',
				__( 'Tags', 'woocommerce' )           => 'tag_ids',
				__( 'Shipping class', 'woocommerce' ) => 'shipping_class_id',
				__( 'Images', 'woocommerce' )         => 'images',
				__( 'Download limit', 'woocommerce' ) => 'download_limit',
				__( 'Download expiry days', 'woocommerce' ) => 'download_expiry',
				__( 'Parent', 'woocommerce' )         => 'parent_id',
				__( 'Upsells', 'woocommerce' )        => 'upsell_ids',
				__( 'Cross-sells', 'woocommerce' )    => 'cross_sell_ids',
				__( 'Grouped products', 'woocommerce' ) => 'grouped_products',
				__( 'External URL', 'woocommerce' )   => 'product_url',
				__( 'Button text', 'woocommerce' )    => 'button_text',
				__( 'Position', 'woocommerce' )       => 'menu_order',
			),
			$raw_headers
		)
	);

	$special_columns = $this->get_special_columns(
		$this->normalize_columns_names(
			apply_filters(
				'woocommerce_csv_product_import_mapping_special_columns',
				array(
					/* translators: %d: Attribute number */
					__( 'Attribute %d name', 'woocommerce' ) => 'attributes:name',
					/* translators: %d: Attribute number */
					__( 'Attribute %d value(s)', 'woocommerce' ) => 'attributes:value',
					/* translators: %d: Attribute number */
					__( 'Attribute %d visible', 'woocommerce' ) => 'attributes:visible',
					/* translators: %d: Attribute number */
					__( 'Attribute %d global', 'woocommerce' ) => 'attributes:taxonomy',
					/* translators: %d: Attribute number */
					__( 'Attribute %d default', 'woocommerce' ) => 'attributes:default',
					/* translators: %d: Download number */
					__( 'Download %d ID', 'woocommerce' ) => 'downloads:id',
					/* translators: %d: Download number */
					__( 'Download %d name', 'woocommerce' ) => 'downloads:name',
					/* translators: %d: Download number */
					__( 'Download %d URL', 'woocommerce' ) => 'downloads:url',
					/* translators: %d: Meta number */
					__( 'Meta: %s', 'woocommerce' ) => 'meta:',
				),
				$raw_headers
			)
		)
	);

	$headers = array();
	foreach ( $raw_headers as $key => $field ) {
		$normalized_field  = strtolower( $field );
		$index             = $num_indexes ? $key : $field;
		$headers[ $index ] = $normalized_field;

		if ( isset( $default_columns[ $normalized_field ] ) ) {
			$headers[ $index ] = $default_columns[ $normalized_field ];
		} else {
			foreach ( $special_columns as $regex => $special_key ) {
				// Don't use the normalized field in the regex since meta might be case-sensitive.
				if ( preg_match( $regex, $field, $matches ) ) {
					$headers[ $index ] = $special_key . $matches[1];
					break;
				}
			}
		}
	}

	return apply_filters( 'woocommerce_csv_product_import_mapped_columns', $headers, $raw_headers );
}