WC_Product_CSV_Importer::parse_id_field
Parse the ID field.
If we're not doing an update, create a placeholder product so mapping works for rows following this one.
Method of the class: WC_Product_CSV_Importer{}
No Hooks.
Returns
Int.
Usage
$WC_Product_CSV_Importer = new WC_Product_CSV_Importer(); $WC_Product_CSV_Importer->parse_id_field( $value );
- $value(string) (required)
- Field value.
WC_Product_CSV_Importer::parse_id_field() WC Product CSV Importer::parse id field code WC 10.5.0
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( ProductType::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;
}