WC_Product_CSV_Importer_Controller::import()publicWC 1.0

Import the file if it exists and is valid.

Method of the class: WC_Product_CSV_Importer_Controller{}

No Hooks.

Return

null. Nothing (null).

Usage

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

WC_Product_CSV_Importer_Controller::import() code WC 8.7.0

public function import() {
	// Displaying this page triggers Ajax action to run the import with a valid nonce,
	// therefore this page needs to be nonce protected as well.
	check_admin_referer( 'woocommerce-csv-importer' );

	if ( ! self::is_file_valid_csv( $this->file ) ) {
		$this->add_error( __( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) );
		$this->output_errors();
		return;
	}

	if ( ! is_file( $this->file ) ) {
		$this->add_error( __( 'The file does not exist, please try again.', 'woocommerce' ) );
		$this->output_errors();
		return;
	}

	if ( ! empty( $_POST['map_from'] ) && ! empty( $_POST['map_to'] ) ) {
		$mapping_from = wc_clean( wp_unslash( $_POST['map_from'] ) );
		$mapping_to   = wc_clean( wp_unslash( $_POST['map_to'] ) );

		// Save mapping preferences for future imports.
		update_user_option( get_current_user_id(), 'woocommerce_product_import_mapping', $mapping_to );
	} else {
		wp_redirect( esc_url_raw( $this->get_next_step_link( 'upload' ) ) );
		exit;
	}

	wp_localize_script(
		'wc-product-import',
		'wc_product_import_params',
		array(
			'import_nonce'       => wp_create_nonce( 'wc-product-import' ),
			'mapping'            => array(
				'from' => $mapping_from,
				'to'   => $mapping_to,
			),
			'file'               => $this->file,
			'update_existing'    => $this->update_existing,
			'delimiter'          => $this->delimiter,
			'character_encoding' => $this->character_encoding,
		)
	);
	wp_enqueue_script( 'wc-product-import' );

	include_once dirname( __FILE__ ) . '/views/html-csv-import-progress.php';
}