WC_Product_CSV_Importer::import │ public │ WC 1.0

Process importer.

Do not import products with IDs or SKUs that already exist if option update existing is false, and likewise, if updating products, do not process rows which do not exist if an ID/SKU is provided.

Method of the class: WC_Product_CSV_Importer{}

Returns

array.

Usage

$WC_Product_CSV_Importer = new WC_Product_CSV_Importer();
$WC_Product_CSV_Importer->import();

WC_Product_CSV_Importer::import() code WC 11.1.2

public function import() {
	$this->start_time = time();
	$index            = 0;
	$update_existing  = $this->params['update_existing'];
	$data             = array(
		'imported'            => array(),
		'imported_variations' => array(),
		'failed'              => array(),
		'updated'             => array(),
		'skipped'             => array(),
	);

	foreach ( $this->parsed_data as $parsed_data_key => $parsed_data ) {
		do_action( 'woocommerce_product_import_before_import', $parsed_data );

		$id         = isset( $parsed_data['id'] ) ? absint( $parsed_data['id'] ) : 0;
		$sku        = isset( $parsed_data['sku'] ) ? $parsed_data['sku'] : '';
		$id_exists  = false;
		$sku_exists = false;

		if ( $id ) {
			$product   = wc_get_product( $id );
			$id_exists = $product && 'importing' !== $product->get_status();
		}

		if ( $sku ) {
			$id_from_sku = wc_get_product_id_by_sku( $sku );
			$product     = $id_from_sku ? wc_get_product( $id_from_sku ) : false;
			$sku_exists  = $product && 'importing' !== $product->get_status();
		}

		if ( $sku_exists && ! $update_existing ) {
			$data['skipped'][] = new WP_Error(
				'woocommerce_product_importer_error',
				esc_html__( 'A product with this SKU already exists.', 'woocommerce' ),
				array(
					'sku' => esc_attr( $sku ),
					'row' => $this->get_row_id( $parsed_data ),
				)
			);
			continue;
		}

		if ( $id_exists && ! $update_existing ) {
			$data['skipped'][] = new WP_Error(
				'woocommerce_product_importer_error',
				esc_html__( 'A product with this ID already exists.', 'woocommerce' ),
				array(
					'id'  => $id,
					'row' => $this->get_row_id( $parsed_data ),
				)
			);
			continue;
		}

		if ( $update_existing && ( isset( $parsed_data['id'] ) || isset( $parsed_data['sku'] ) ) && ! $id_exists && ! $sku_exists ) {
			$create_variation = false;
			$refusal          = null;

			if ( ProductType::VARIATION === ( $parsed_data['type'] ?? '' ) ) {
				$can_create_variation = $this->can_create_variation( $parsed_data );

				// Anything other than an explicit pass refuses, so an override still written against
				// the previous boolean contract cannot turn a refusal into a creation.
				if ( true !== $can_create_variation ) {
					$refusal = is_wp_error( $can_create_variation ) ? $can_create_variation : null;
				} else {
					/**
					 * Filters whether a new variation should be created for an existing variable product when updating existing products.
					 *
					 * Only fires for variation rows that passed validation, so it can veto the creation but not force it.
					 *
					 * @since 11.1.0
					 *
					 * @param bool  $create_variation Whether to create the new variation instead of skipping the row.
					 * @param array $parsed_data      Parsed row data.
					 */
					$create_variation = apply_filters( 'woocommerce_product_import_create_variation_of_existing_product', true, $parsed_data );
				}
			}

			if ( ! $create_variation ) {
				// A refused variation row reports why it was refused; anything else is a row whose
				// ID or SKU simply matches nothing on the site.
				$data['skipped'][] = new WP_Error(
					'woocommerce_product_importer_error',
					$refusal ? $refusal->get_error_message() : esc_html__( 'No matching product exists to update.', 'woocommerce' ),
					array(
						'id'  => $id,
						'sku' => esc_attr( $sku ),
						'row' => $this->get_row_id( $parsed_data ),
					)
				);
				continue;
			}
		}

		$result = $this->process_item( $parsed_data );

		if ( is_wp_error( $result ) ) {
			$result->add_data( array( 'row' => $this->get_row_id( $parsed_data ) ) );
			$data['failed'][] = $result;
		} elseif ( $result['updated'] ) {
			$data['updated'][] = $result['id'];
		} elseif ( $result['is_variation'] ) {
				$data['imported_variations'][] = $result['id'];
		} else {
			$data['imported'][] = $result['id'];
		}

		++$index;

		if ( $this->params['prevent_timeouts'] && ( $this->time_exceeded() || $this->memory_exceeded() ) ) {
			$this->file_position = $this->file_positions[ $index ];
			break;
		}
	}

	return $data;
}