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"