Fix: variations are counted as products in import message of number of products imported (#37829)

Co-authored-by: mujuonly <muju.only@gmail.com>
Co-authored-by: barryhughes <3594411+barryhughes@users.noreply.github.com>
Co-authored-by: Nestor Soriano <konamiman@konamiman.com>
This commit is contained in:
mujuonly 2023-04-21 11:52:08 +05:30 committed by GitHub
parent c19a42398b
commit 9cfd58ad15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 31 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: tweak
Fix typo in a function comment.

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Show the number of variations imported

View File

@ -17,6 +17,7 @@
// Number of import successes/failures. // Number of import successes/failures.
this.imported = 0; this.imported = 0;
this.imported_variations = 0;
this.failed = 0; this.failed = 0;
this.updated = 0; this.updated = 0;
this.skipped = 0; this.skipped = 0;
@ -54,6 +55,7 @@
if ( response.success ) { if ( response.success ) {
$this.position = response.data.position; $this.position = response.data.position;
$this.imported += response.data.imported; $this.imported += response.data.imported;
$this.imported_variations += response.data.imported_variations;
$this.failed += response.data.failed; $this.failed += response.data.failed;
$this.updated += response.data.updated; $this.updated += response.data.updated;
$this.skipped += response.data.skipped; $this.skipped += response.data.skipped;
@ -64,6 +66,8 @@
window.location = response.data.url + window.location = response.data.url +
'&products-imported=' + '&products-imported=' +
parseInt( $this.imported, 10 ) + parseInt( $this.imported, 10 ) +
'&products-imported-variations=' +
parseInt( $this.imported_variations, 10 ) +
'&products-failed=' + '&products-failed=' +
parseInt( $this.failed, 10 ) + parseInt( $this.failed, 10 ) +
'&products-updated=' + '&products-updated=' +

View File

@ -294,6 +294,7 @@ class WC_Admin_Importers {
'percentage' => 100, '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' ) ), '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' => count( $results['imported'] ),
'imported_variations' => count( $results['imported_variations'] ),
'failed' => count( $results['failed'] ), 'failed' => count( $results['failed'] ),
'updated' => count( $results['updated'] ), 'updated' => count( $results['updated'] ),
'skipped' => count( $results['skipped'] ), 'skipped' => count( $results['skipped'] ),

View File

@ -462,6 +462,7 @@ class WC_Product_CSV_Importer_Controller {
protected function done() { protected function done() {
check_admin_referer( 'woocommerce-csv-importer' ); check_admin_referer( 'woocommerce-csv-importer' );
$imported = isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 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; $updated = isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0;
$failed = isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0; $failed = isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0;
$skipped = isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0; $skipped = isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0;

View File

@ -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' ),
'<strong>' . number_format_i18n( $imported_variations ) . '</strong>'
);
}
if ( 0 < $skipped ) { if ( 0 < $skipped ) {
$results[] = sprintf( $results[] = sprintf(
/* translators: %d: products count */ /* translators: %d: products count */

View File

@ -250,11 +250,12 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
if ( 'external' === $object->get_type() ) { if ( 'external' === $object->get_type() ) {
unset( $data['manage_stock'], $data['stock_status'], $data['backorders'], $data['low_stock_amount'] ); unset( $data['manage_stock'], $data['stock_status'], $data['backorders'], $data['low_stock_amount'] );
} }
$is_variation = false;
if ( 'variation' === $object->get_type() ) { if ( 'variation' === $object->get_type() ) {
if ( isset( $data['status'] ) && -1 === $data['status'] ) { if ( isset( $data['status'] ) && -1 === $data['status'] ) {
$data['status'] = 0; // Variations cannot be drafts - set to private. $data['status'] = 0; // Variations cannot be drafts - set to private.
} }
$is_variation = true;
} }
if ( 'importing' === $object->get_status() ) { if ( 'importing' === $object->get_status() ) {
@ -285,6 +286,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
return array( return array(
'id' => $object->get_id(), 'id' => $object->get_id(),
'updated' => $updating, 'updated' => $updating,
'is_variation' => $is_variation,
); );
} catch ( Exception $e ) { } catch ( Exception $e ) {
return new WP_Error( 'woocommerce_product_importer_error', $e->getMessage(), array( 'status' => $e->getCode() ) ); return new WP_Error( 'woocommerce_product_importer_error', $e->getMessage(), array( 'status' => $e->getCode() ) );

View File

@ -1081,6 +1081,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$update_existing = $this->params['update_existing']; $update_existing = $this->params['update_existing'];
$data = array( $data = array(
'imported' => array(), 'imported' => array(),
'imported_variations' => array(),
'failed' => array(), 'failed' => array(),
'updated' => array(), 'updated' => array(),
'skipped' => array(), 'skipped' => array(),
@ -1149,9 +1150,13 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$data['failed'][] = $result; $data['failed'][] = $result;
} elseif ( $result['updated'] ) { } elseif ( $result['updated'] ) {
$data['updated'][] = $result['id']; $data['updated'][] = $result['id'];
} else {
if ( $result['is_variation'] ) {
$data['imported_variations'][] = $result['id'];
} else { } else {
$data['imported'][] = $result['id']; $data['imported'][] = $result['id'];
} }
}
$index ++; $index ++;

View File

@ -72,6 +72,7 @@ class WC_Importer_Tracking {
$properties = array( $properties = array(
'imported' => isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 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, 'updated' => isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0,
'failed' => isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0, 'failed' => isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0,
'skipped' => isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0, 'skipped' => isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0,

View File

@ -115,7 +115,7 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
$this->assertEquals( 0, count( $results['skipped'] ) ); $this->assertEquals( 0, count( $results['skipped'] ) );
$this->assertEquals( $this->assertEquals(
7, 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.' '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.'
); );
} }
@ -130,7 +130,7 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
$this->assertEquals( 0, count( $results['updated'] ) ); $this->assertEquals( 0, count( $results['updated'] ) );
$this->assertEquals( 0, count( $results['skipped'] ) ); $this->assertEquals( 0, count( $results['skipped'] ) );
$this->assertEquals( 6, count( $results['imported'] ) ); $this->assertEquals( 6, count( $results['imported'] ) + count( $results['imported_variations'] ) );
$this->assertEquals( $this->assertEquals(
1, 1,
count( $results['failed'] ), count( $results['failed'] ),