WC_Product_CSV_Importer::parse_relative_field()
Parse relative field and return product ID.
Handles id:xx and SKUs.
If mapping to an id: and the product ID does not exist, this link is not valid.
If mapping to a SKU and the product ID does not exist, a temporary object will be created so it can be updated later.
Метод класса: WC_Product_CSV_Importer{}
Хуков нет.
Возвращает
int|Строку
.
Использование
$WC_Product_CSV_Importer = new WC_Product_CSV_Importer(); $WC_Product_CSV_Importer->parse_relative_field( $value );
- $value(строка) (обязательный)
- Field value.
Код WC_Product_CSV_Importer::parse_relative_field() WC Product CSV Importer::parse relative field WC 9.3.3
public function parse_relative_field( $value ) { global $wpdb; if ( empty( $value ) ) { return ''; } // IDs are prefixed with id:. if ( preg_match( '/^id:(\d+)$/', $value, $matches ) ) { $id = intval( $matches[1] ); // If original_id is found, use that instead of the given ID since a new placeholder must have been created 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 ); } // See if the given ID maps to a valid product allready. $existing_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation' ) AND ID = %d;", $id ) ); // WPCS: db call ok, cache ok. if ( $existing_id ) { return absint( $existing_id ); } // If we're not updating existing posts, we may need a placeholder product to map to. if ( ! $this->params['update_existing'] ) { $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 ); $id = $product->save(); } return $id; } $id = wc_get_product_id_by_sku( $value ); if ( $id ) { return $id; } try { $product = wc_get_product_object( 'simple' ); $product->set_name( 'Import placeholder for ' . $value ); $product->set_status( 'importing' ); $product->set_sku( $value ); $id = $product->save(); if ( $id && ! is_wp_error( $id ) ) { return $id; } } catch ( Exception $e ) { return ''; } return ''; }