Wrong error message in CSV importer (#51966)

* Test that sku skipping works

#46505

* Switch SKU and ID error detection

If we find an product with the same SKU we overwrite the imported product id with the existing one. When we check for the existing ID first, we would get now a match although the imported ID did not match. Therefore we first check if the SKU is already present before checking for the ID

#46505

* Add doc

* Add changelog

* Add changefile(s) from automation for the following project(s): woocommerce

* Remove unneeded changelog entry pulled in during rebase.

* Remove mistakenly pushed debug code.

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>
This commit is contained in:
David Remer 2024-10-28 20:55:08 +02:00 committed by GitHub
parent b7ecd5e294
commit 2c77be6444
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 12 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fixes wrong error message when importing products with existing SKU.

View File

@ -1135,18 +1135,6 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$sku_exists = $product && 'importing' !== $product->get_status(); $sku_exists = $product && 'importing' !== $product->get_status();
} }
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 ( $sku_exists && ! $update_existing ) { if ( $sku_exists && ! $update_existing ) {
$data['skipped'][] = new WP_Error( $data['skipped'][] = new WP_Error(
'woocommerce_product_importer_error', 'woocommerce_product_importer_error',
@ -1159,6 +1147,18 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
continue; 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 ) { if ( $update_existing && ( isset( $parsed_data['id'] ) || isset( $parsed_data['sku'] ) ) && ! $id_exists && ! $sku_exists ) {
$data['skipped'][] = new WP_Error( $data['skipped'][] = new WP_Error(
'woocommerce_product_importer_error', 'woocommerce_product_importer_error',

View File

@ -88,4 +88,33 @@ class WC_Product_CSV_Importer_Test extends \WC_Unit_Test_Case {
$this->assertEquals( 100, $importer->get_percent_complete() ); $this->assertEquals( 100, $importer->get_percent_complete() );
} }
/**
* @testdox Test that the importer skips updating products with the same SKU.
*/
public function test_import_skipping_existing_product_sku_46505() {
$product = WC_Helper_Product::create_simple_product();
$product->set_sku( '46505-sku' );
$product->save();
$csv_file = __DIR__ . '/import-skipping-existing-products-46505-data.csv';
$args = array(
'parse' => true,
'mapping' => array(
'ID' => 'id',
'SKU' => 'sku',
),
);
$importer = new WC_Product_CSV_Importer( $csv_file, $args );
$data = $importer->import();
WC_Helper_Product::delete_product( $product->get_id() );
$this->assertEmpty( $data['updated'], 'Expected 0 updated products, got ' . count( $data['updated'] ) );
$this->assertEmpty( $data['imported'], 'Expected 0 imported products, got ' . count( $data['imported'] ) );
$this->assertEmpty( $data['failed'], 'Expected 0 failed products, got ' . count( $data['failed'] ) );
$this->assertEquals( 1, count( $data['skipped'] ), 'Expected 1 skipped product, got ' . count( $data['skipped'] ) );
$error = $data['skipped'][0];
$this->assertInstanceOf( WP_Error::class, $error );
$this->assertEquals( 'A product with this SKU already exists.', $error->get_error_message() );
}
} }

View File

@ -0,0 +1,2 @@
"ID","SKU"
9001,"46505-sku"
1 ID SKU
2 9001 46505-sku