From 2c77be6444b349d88a172edd67710340e3c6941a Mon Sep 17 00:00:00 2001 From: David Remer Date: Mon, 28 Oct 2024 20:55:08 +0200 Subject: [PATCH] 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 Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com> --- .../46505-wrong-error-message-in-importer | 4 +++ .../import/class-wc-product-csv-importer.php | 24 +++++++-------- .../class-wc-product-csv-importer-test.php | 29 +++++++++++++++++++ ...-skipping-existing-products-46505-data.csv | 2 ++ 4 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 plugins/woocommerce/changelog/46505-wrong-error-message-in-importer create mode 100644 plugins/woocommerce/tests/php/includes/importer/import-skipping-existing-products-46505-data.csv diff --git a/plugins/woocommerce/changelog/46505-wrong-error-message-in-importer b/plugins/woocommerce/changelog/46505-wrong-error-message-in-importer new file mode 100644 index 00000000000..63021e96dc8 --- /dev/null +++ b/plugins/woocommerce/changelog/46505-wrong-error-message-in-importer @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fixes wrong error message when importing products with existing SKU. diff --git a/plugins/woocommerce/includes/import/class-wc-product-csv-importer.php b/plugins/woocommerce/includes/import/class-wc-product-csv-importer.php index 804426ed9aa..4c681b846fb 100644 --- a/plugins/woocommerce/includes/import/class-wc-product-csv-importer.php +++ b/plugins/woocommerce/includes/import/class-wc-product-csv-importer.php @@ -1135,18 +1135,6 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { $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 ) { $data['skipped'][] = new WP_Error( 'woocommerce_product_importer_error', @@ -1159,6 +1147,18 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { 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', diff --git a/plugins/woocommerce/tests/php/includes/importer/class-wc-product-csv-importer-test.php b/plugins/woocommerce/tests/php/includes/importer/class-wc-product-csv-importer-test.php index 35a234c48ca..f530a80962c 100644 --- a/plugins/woocommerce/tests/php/includes/importer/class-wc-product-csv-importer-test.php +++ b/plugins/woocommerce/tests/php/includes/importer/class-wc-product-csv-importer-test.php @@ -88,4 +88,33 @@ class WC_Product_CSV_Importer_Test extends \WC_Unit_Test_Case { $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() ); + } } diff --git a/plugins/woocommerce/tests/php/includes/importer/import-skipping-existing-products-46505-data.csv b/plugins/woocommerce/tests/php/includes/importer/import-skipping-existing-products-46505-data.csv new file mode 100644 index 00000000000..a76b71a5565 --- /dev/null +++ b/plugins/woocommerce/tests/php/includes/importer/import-skipping-existing-products-46505-data.csv @@ -0,0 +1,2 @@ +"ID","SKU" +9001,"46505-sku"