WC_Product_CSV_Importer::import
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{}
Hooks from the method
Returns
Array.
Usage
$WC_Product_CSV_Importer = new WC_Product_CSV_Importer(); $WC_Product_CSV_Importer->import();
WC_Product_CSV_Importer::import() WC Product CSV Importer::import code WC 10.8.1
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 ) {
$data['skipped'][] = new WP_Error(
'woocommerce_product_importer_error',
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;
}