Merge pull request #18811 from woocommerce/fix/18792

Fix import placeholder lookup
This commit is contained in:
Claudio Sanches 2018-02-05 13:01:00 -02:00 committed by GitHub
commit a74f95c638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 7 deletions

View File

@ -22,6 +22,13 @@ if ( ! class_exists( 'WC_Product_Importer', false ) ) {
*/ */
class WC_Product_CSV_Importer extends WC_Product_Importer { class WC_Product_CSV_Importer extends WC_Product_Importer {
/**
* Tracks current row being parsed.
*
* @var integer
*/
protected $parsing_raw_data_index = 0;
/** /**
* Initialize importer. * Initialize importer.
* *
@ -222,11 +229,14 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
// Not updating? Make sure we have a new placeholder for this ID. // Not updating? Make sure we have a new placeholder for this ID.
if ( ! $this->params['update_existing'] ) { if ( ! $this->params['update_existing'] ) {
$id = isset( $this->raw_data['sku'] ) ? wc_get_product_id_by_sku( $this->raw_data['sku'] ) : ''; $mapped_keys = $this->get_mapped_keys();
$sku_column_index = absint( array_search( 'sku', $mapped_keys, true ) );
$row_sku = isset( $this->raw_data[ $this->parsing_raw_data_index ][ $sku_column_index ] ) ? $this->raw_data[ $this->parsing_raw_data_index ][ $sku_column_index ] : '';
$id_from_sku = $row_sku ? wc_get_product_id_by_sku( $row_sku ) : '';
// If row has a SKU, make sure placeholder was not made already. // If row has a SKU, make sure placeholder was not made already.
if ( $id ) { if ( $id_from_sku ) {
return $id; return $id_from_sku;
} }
$product = new WC_Product_Simple(); $product = new WC_Product_Simple();
@ -235,8 +245,8 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$product->add_meta_data( '_original_id', $id, true ); $product->add_meta_data( '_original_id', $id, true );
// If row has a SKU, make sure placeholder has it too. // If row has a SKU, make sure placeholder has it too.
if ( isset( $this->raw_data['sku'] ) ) { if ( $row_sku ) {
$product->set_sku( $this->raw_data['sku'] ); $product->set_sku( $row_sku );
} }
$id = $product->save(); $id = $product->save();
} }
@ -777,11 +787,14 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
$use_mb = function_exists( 'mb_convert_encoding' ); $use_mb = function_exists( 'mb_convert_encoding' );
// Parse the data. // Parse the data.
foreach ( $this->raw_data as $row ) { foreach ( $this->raw_data as $row_index => $row ) {
// Skip empty rows. // Skip empty rows.
if ( ! count( array_filter( $row ) ) ) { if ( ! count( array_filter( $row ) ) ) {
continue; continue;
} }
$this->parsing_raw_data_index = $row_index;
$data = array(); $data = array();
do_action( 'woocommerce_product_importer_before_set_parsed_data', $row, $mapped_keys ); do_action( 'woocommerce_product_importer_before_set_parsed_data', $row, $mapped_keys );
@ -869,7 +882,9 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
if ( $id ) { if ( $id ) {
$product = wc_get_product( $id ); $product = wc_get_product( $id );
$id_exists = $product && 'importing' !== $product->get_status(); $id_exists = $product && 'importing' !== $product->get_status();
} elseif ( $sku ) { }
if ( $sku ) {
$id_from_sku = wc_get_product_id_by_sku( $sku ); $id_from_sku = wc_get_product_id_by_sku( $sku );
$product = $id_from_sku ? wc_get_product( $id_from_sku ) : false; $product = $id_from_sku ? wc_get_product( $id_from_sku ) : false;
$sku_exists = $product && 'importing' !== $product->get_status(); $sku_exists = $product && 'importing' !== $product->get_status();