force field in csv import
-
Hi @xnau
Following this topic https://ww.wp.xz.cn/support/topic/generate-member-id-when-importing-csv-data/, i was trying to generate an member_id when importing a csv also.
I did it successfully with this code :add_filter( ‘pdb-before_csv_store_record’, array( $this, ‘manage_new_member_id_csv’ ) );
public function manage_new_member_id_csv($record) {
global $wpdb;
$sql = ‘SELECTidFROM ‘ . Participants_Db::$participants_table . ‘ WHERE ‘ . last_name . ‘ = “%s” AND ‘ . first_name . ‘ = “%s” AND ‘ . email . ‘ = “%s”‘;
$result = $wpdb->get_col( $wpdb->prepare( $sql, $record[“last_name”], $record[“first_name”], $record[“email”] ) );
if ( $wpdb->num_rows === 0 ) {
$record = $this->generate_new_member_id($record);
} else {
$record[$this->id_field] = null;
}
return $record;
}When I import a new csv with the member_id field in the csv file, it works properly. However, when the member_id field is not in the csv file, the member_id is not inserted in the database (even though the member_id is generated successfully )
While trying to debug, I noticed in the plugin code that, the csv fields which were treated were only those in the csv file :
class PDb_CSV_Import extends xnau_CSV_Import
[…]
function store_record( $post ) {
[…]
// add the record data to the database
$id = Participants_Db::process_form( $post, ‘insert’, false, $this->column_names );Is there a way to force the treatment of a field when importing a csv file even if the field name is not in the csv file ? If it is not possible currently, would it be possible to add a filter in the plugin so that we would be able to add extra fields which are not in the csv file ?
I would be needing that because I would like to force the member_id to be generated and inserted in database even if the field name is not in the csv file, so that, even if the users who are importing the csv file forget to include this field in the file, the member_id would still be generated
Thank you
Have a nice day
The topic ‘force field in csv import’ is closed to new replies.