WC_Product_CSV_Importer::parse_id_field( string $value )
Parse the ID field.
Description Description
If we’re not doing an update, create a placeholder product so mapping works for rows following this one.
Parameters Parameters
- $value
-
(Required) Field value.
Return Return
(int)
Source Source
File: includes/import/class-wc-product-csv-importer.php
public function parse_id_field( $value ) {
global $wpdb;
$id = absint( $value );
if ( ! $id ) {
return 0;
}
// See if this maps to an ID placeholder already.
$original_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_original_id' AND meta_value = %s;", $id ) ); // WPCS: db call ok, cache ok.
if ( $original_id ) {
return absint( $original_id );
}
// Not updating? Make sure we have a new placeholder for this ID.
if ( ! $this->params['update_existing'] ) {
$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 ( $id_from_sku ) {
return $id_from_sku;
}
$product = wc_get_product_object( 'simple' );
$product->set_name( 'Import placeholder for ' . $id );
$product->set_status( 'importing' );
$product->add_meta_data( '_original_id', $id, true );
// If row has a SKU, make sure placeholder has it too.
if ( $row_sku ) {
$product->set_sku( $row_sku );
}
$id = $product->save();
}
return $id && ! is_wp_error( $id ) ? $id : 0;
}