From 9cfd58ad157ca88ede2a96c9a60639b0c33dda9b Mon Sep 17 00:00:00 2001 From: mujuonly Date: Fri, 21 Apr 2023 11:52:08 +0530 Subject: [PATCH] Fix: variations are counted as products in import message of number of products imported (#37829) Co-authored-by: mujuonly Co-authored-by: barryhughes <3594411+barryhughes@users.noreply.github.com> Co-authored-by: Nestor Soriano --- .../fix-typo-in-regenerate_report_data-doc | 4 ++++ .../changelog/number_of_variations_imported | 4 ++++ .../client/legacy/js/admin/wc-product-import.js | 4 ++++ .../includes/admin/class-wc-admin-importers.php | 15 ++++++++------- .../class-wc-product-csv-importer-controller.php | 13 +++++++------ .../importers/views/html-csv-import-done.php | 8 ++++++++ .../import/abstract-wc-product-importer.php | 8 +++++--- .../import/class-wc-product-csv-importer.php | 15 ++++++++++----- .../tracks/events/class-wc-importer-tracking.php | 9 +++++---- .../tests/legacy/unit-tests/importer/product.php | 12 ++++++------ 10 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-typo-in-regenerate_report_data-doc create mode 100644 plugins/woocommerce/changelog/number_of_variations_imported diff --git a/plugins/woocommerce/changelog/fix-typo-in-regenerate_report_data-doc b/plugins/woocommerce/changelog/fix-typo-in-regenerate_report_data-doc new file mode 100644 index 00000000000..b52cd46aa6c --- /dev/null +++ b/plugins/woocommerce/changelog/fix-typo-in-regenerate_report_data-doc @@ -0,0 +1,4 @@ +Significance: patch +Type: tweak + +Fix typo in a function comment. \ No newline at end of file diff --git a/plugins/woocommerce/changelog/number_of_variations_imported b/plugins/woocommerce/changelog/number_of_variations_imported new file mode 100644 index 00000000000..58445c9a5f9 --- /dev/null +++ b/plugins/woocommerce/changelog/number_of_variations_imported @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Show the number of variations imported diff --git a/plugins/woocommerce/client/legacy/js/admin/wc-product-import.js b/plugins/woocommerce/client/legacy/js/admin/wc-product-import.js index 3854dc6af3b..624cf50a09a 100644 --- a/plugins/woocommerce/client/legacy/js/admin/wc-product-import.js +++ b/plugins/woocommerce/client/legacy/js/admin/wc-product-import.js @@ -17,6 +17,7 @@ // Number of import successes/failures. this.imported = 0; + this.imported_variations = 0; this.failed = 0; this.updated = 0; this.skipped = 0; @@ -54,6 +55,7 @@ if ( response.success ) { $this.position = response.data.position; $this.imported += response.data.imported; + $this.imported_variations += response.data.imported_variations; $this.failed += response.data.failed; $this.updated += response.data.updated; $this.skipped += response.data.skipped; @@ -64,6 +66,8 @@ window.location = response.data.url + '&products-imported=' + parseInt( $this.imported, 10 ) + + '&products-imported-variations=' + + parseInt( $this.imported_variations, 10 ) + '&products-failed=' + parseInt( $this.failed, 10 ) + '&products-updated=' + diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-importers.php b/plugins/woocommerce/includes/admin/class-wc-admin-importers.php index 11007d8c2c6..2545eba7968 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-importers.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-importers.php @@ -290,13 +290,14 @@ class WC_Admin_Importers { // Send success. wp_send_json_success( array( - 'position' => 'done', - 'percentage' => 100, - 'url' => add_query_arg( array( '_wpnonce' => wp_create_nonce( 'woocommerce-csv-importer' ) ), admin_url( 'edit.php?post_type=product&page=product_importer&step=done' ) ), - 'imported' => count( $results['imported'] ), - 'failed' => count( $results['failed'] ), - 'updated' => count( $results['updated'] ), - 'skipped' => count( $results['skipped'] ), + 'position' => 'done', + 'percentage' => 100, + 'url' => add_query_arg( array( '_wpnonce' => wp_create_nonce( 'woocommerce-csv-importer' ) ), admin_url( 'edit.php?post_type=product&page=product_importer&step=done' ) ), + 'imported' => count( $results['imported'] ), + 'imported_variations' => count( $results['imported_variations'] ), + 'failed' => count( $results['failed'] ), + 'updated' => count( $results['updated'] ), + 'skipped' => count( $results['skipped'] ), ) ); } else { diff --git a/plugins/woocommerce/includes/admin/importers/class-wc-product-csv-importer-controller.php b/plugins/woocommerce/includes/admin/importers/class-wc-product-csv-importer-controller.php index 511103a713f..e5fac1cecb9 100644 --- a/plugins/woocommerce/includes/admin/importers/class-wc-product-csv-importer-controller.php +++ b/plugins/woocommerce/includes/admin/importers/class-wc-product-csv-importer-controller.php @@ -461,12 +461,13 @@ class WC_Product_CSV_Importer_Controller { */ protected function done() { check_admin_referer( 'woocommerce-csv-importer' ); - $imported = isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 0; - $updated = isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0; - $failed = isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0; - $skipped = isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0; - $file_name = isset( $_GET['file-name'] ) ? sanitize_text_field( wp_unslash( $_GET['file-name'] ) ) : ''; - $errors = array_filter( (array) get_user_option( 'product_import_error_log' ) ); + $imported = isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 0; + $imported_variations = isset( $_GET['products-imported-variations'] ) ? absint( $_GET['products-imported-variations'] ) : 0; + $updated = isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0; + $failed = isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0; + $skipped = isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0; + $file_name = isset( $_GET['file-name'] ) ? sanitize_text_field( wp_unslash( $_GET['file-name'] ) ) : ''; + $errors = array_filter( (array) get_user_option( 'product_import_error_log' ) ); include_once dirname( __FILE__ ) . '/views/html-csv-import-done.php'; } diff --git a/plugins/woocommerce/includes/admin/importers/views/html-csv-import-done.php b/plugins/woocommerce/includes/admin/importers/views/html-csv-import-done.php index 3abb51c3a76..70ef0ddd651 100644 --- a/plugins/woocommerce/includes/admin/importers/views/html-csv-import-done.php +++ b/plugins/woocommerce/includes/admin/importers/views/html-csv-import-done.php @@ -30,6 +30,14 @@ if ( ! defined( 'ABSPATH' ) ) { ); } + if ( 0 < $imported_variations ) { + $results[] = sprintf( + /* translators: %d: products count */ + _n( '%s variations imported', '%s variations imported', $imported_variations, 'woocommerce' ), + '' . number_format_i18n( $imported_variations ) . '' + ); + } + if ( 0 < $skipped ) { $results[] = sprintf( /* translators: %d: products count */ diff --git a/plugins/woocommerce/includes/import/abstract-wc-product-importer.php b/plugins/woocommerce/includes/import/abstract-wc-product-importer.php index 56a31d3be00..ad82d60f9cd 100644 --- a/plugins/woocommerce/includes/import/abstract-wc-product-importer.php +++ b/plugins/woocommerce/includes/import/abstract-wc-product-importer.php @@ -250,11 +250,12 @@ abstract class WC_Product_Importer implements WC_Importer_Interface { if ( 'external' === $object->get_type() ) { unset( $data['manage_stock'], $data['stock_status'], $data['backorders'], $data['low_stock_amount'] ); } - + $is_variation = false; if ( 'variation' === $object->get_type() ) { if ( isset( $data['status'] ) && -1 === $data['status'] ) { $data['status'] = 0; // Variations cannot be drafts - set to private. } + $is_variation = true; } if ( 'importing' === $object->get_status() ) { @@ -283,8 +284,9 @@ abstract class WC_Product_Importer implements WC_Importer_Interface { do_action( 'woocommerce_product_import_inserted_product_object', $object, $data ); return array( - 'id' => $object->get_id(), - 'updated' => $updating, + 'id' => $object->get_id(), + 'updated' => $updating, + 'is_variation' => $is_variation, ); } catch ( Exception $e ) { return new WP_Error( 'woocommerce_product_importer_error', $e->getMessage(), array( 'status' => $e->getCode() ) ); 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 66e44a960f2..93445851813 100644 --- a/plugins/woocommerce/includes/import/class-wc-product-csv-importer.php +++ b/plugins/woocommerce/includes/import/class-wc-product-csv-importer.php @@ -1080,10 +1080,11 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { $index = 0; $update_existing = $this->params['update_existing']; $data = array( - 'imported' => array(), - 'failed' => array(), - 'updated' => array(), - 'skipped' => array(), + 'imported' => array(), + 'imported_variations' => array(), + 'failed' => array(), + 'updated' => array(), + 'skipped' => array(), ); foreach ( $this->parsed_data as $parsed_data_key => $parsed_data ) { @@ -1150,7 +1151,11 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { } elseif ( $result['updated'] ) { $data['updated'][] = $result['id']; } else { - $data['imported'][] = $result['id']; + if ( $result['is_variation'] ) { + $data['imported_variations'][] = $result['id']; + } else { + $data['imported'][] = $result['id']; + } } $index ++; diff --git a/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php b/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php index 648424c5a17..fbffe91b4ba 100644 --- a/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php +++ b/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php @@ -71,10 +71,11 @@ class WC_Importer_Tracking { } $properties = array( - 'imported' => isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 0, - 'updated' => isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0, - 'failed' => isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0, - 'skipped' => isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0, + 'imported' => isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 0, + 'imported_variations' => isset( $_GET['products-imported-variations'] ) ? absint( $_GET['products-imported-variations'] ) : 0, + 'updated' => isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0, + 'failed' => isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0, + 'skipped' => isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0, ); // phpcs:enable diff --git a/plugins/woocommerce/tests/legacy/unit-tests/importer/product.php b/plugins/woocommerce/tests/legacy/unit-tests/importer/product.php index 3fbab5add05..8156bb46297 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/importer/product.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/importer/product.php @@ -34,8 +34,8 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case { // Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response. $this->http_responder = array( $this, 'mock_http_responses' ); - $this->csv_file = dirname( __FILE__ ) . '/sample.csv'; - $this->sut = new WC_Product_CSV_Importer( + $this->csv_file = dirname( __FILE__ ) . '/sample.csv'; + $this->sut = new WC_Product_CSV_Importer( $this->csv_file, array( 'mapping' => $this->get_csv_mapped_items(), @@ -108,14 +108,14 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case { public function test_import_for_admin_users() { // In most cases, an admin user will run the import. wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); - $results = $this->sut->import(); + $results = $this->sut->import(); $this->assertEquals( 0, count( $results['failed'] ) ); $this->assertEquals( 0, count( $results['updated'] ) ); $this->assertEquals( 0, count( $results['skipped'] ) ); $this->assertEquals( 7, - count( $results['imported'] ), + count( $results['imported'] ) + count( $results['imported_variations'] ), 'One import item references a downloadable file stored in an unapproved location: if the import is triggered by an admin user, that location will be automatically approved.' ); } @@ -126,11 +126,11 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case { public function test_import_for_shop_managers() { // In some cases, a shop manager may run the import. wp_set_current_user( self::factory()->user->create( array( 'role' => 'shop_manager' ) ) ); - $results = $this->sut->import(); + $results = $this->sut->import(); $this->assertEquals( 0, count( $results['updated'] ) ); $this->assertEquals( 0, count( $results['skipped'] ) ); - $this->assertEquals( 6, count( $results['imported'] ) ); + $this->assertEquals( 6, count( $results['imported'] ) + count( $results['imported_variations'] ) ); $this->assertEquals( 1, count( $results['failed'] ),