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.
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=' +

View File

@ -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 {

View File

@ -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';
}

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 ) {
$results[] = sprintf(
/* translators: %d: products count */

View File

@ -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() ) );

View File

@ -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 ++;

View File

@ -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

View File

@ -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'] ),