1. Documentation /
  2. Code Snippets

Code Snippets

Here are some code snippets listed to make your task easier with the Import export Suite for WooCommerce.

1. Import and export order meta created using WooCommerce Booking plugin

↑ Back to top

Insert the below-mentioned code snippet in the functions.php file of your active child theme before importing or exporting the orders.

<?php
add_filter('wt_iew_alter_export_data', 'wt_iew_alter_export_booking_data', 10, 6);
function wt_iew_alter_export_booking_data($export_data, $offset, $is_last_offset, $file_as, $to_export, $csv_delimiter) {
if ($to_export == 'order') {
if (isset($export_data['head_data'])) {
$export_data['head_data']['meta:booking_items'] = 'meta:booking_items';
}
if (isset($export_data['body_data'])) {
foreach ($export_data['body_data'] as $ord_key => $ord_value) {
global $wpdb;
$order_id = $ord_value['order_id'];
$query = "SELECT * FROM {$wpdb->posts} WHERE post_type = 'wc_booking' AND post_parent = " . $order_id;
$bookings = $wpdb->get_results($query, ARRAY_A);
if (!empty($bookings)) {
foreach ($bookings as $booking) {
$query1 = "SELECT meta_key,meta_value FROM {$wpdb->postmeta} WHERE post_id = " . $booking['ID'];
$booking_meta = $wpdb->get_results($query1, ARRAY_N);
//unset($booking['ID']);
foreach ($booking_meta as $meta) {
if ($meta[0] == '_booking_order_item_id')
continue;
$new_meta[$meta[0]] = $meta[1];
}
$booking_items[] = implode('|', array(
'data:' . serialize($booking),
'meta:' . serialize($new_meta)
));
unset($booking,$new_meta);
}
$export_data['body_data'][$ord_key]['meta:booking_items'] = implode('||', $booking_items);
unset($booking_items);
}
}
}
}
return $export_data;
}
add_filter('wt_iew_importer_skip_from_evaluation', 'wt_iew_importer_skip_from_booking_evaluation');
function wt_iew_importer_skip_from_booking_evaluation($evl_arra) {
$evl_arra[] = 'meta:booking_items';
return $evl_arra;
}
add_filter('wt_woocommerce_order_importer_pre_parse_data','wt_woocommerce_order_importer_pre_parse_booking_data');
function wt_woocommerce_order_importer_pre_parse_booking_data($item){
if(!empty($item['meta_mapping_fields']['meta']['meta:booking_items'])){
$booking_item = explode('||', $item['meta_mapping_fields']['meta']['meta:booking_items']);
$item['meta_mapping_fields']['meta']['meta:booking_items'] = $booking_item;
}
return $item;
}
add_action('wt_woocommerce_order_import_inserted_object', 'wt_woocommerce_booking_order_import_inserted_object', 10, 2);
function wt_woocommerce_booking_order_import_inserted_object($order, $data) {
$order_id = $order->get_id();
global $wpdb;
foreach ($data['meta_data'] as $data_key => $data_value) {
if ($data_value['key'] == 'booking_items') {
if (!empty($data_value['value'])) {
foreach ($data_value['value'] as $booking) {
$_bitem_meta = explode('|', $booking);
$booking_post = array_shift($_bitem_meta);
$booking_post = substr($booking_post, strpos($booking_post, ":") + 1);
$booking_meta = array_shift($_bitem_meta);
$booking_meta = substr($booking_meta, strpos($booking_meta, ":") + 1);
$booking_post_data = unserialize($booking_post);
$query = "SELECT * FROM {$wpdb->posts} WHERE post_type = 'wc_booking' AND ID =" . $booking_post_data['ID'] . " AND post_parent = " . $order_id;
$bookings = $wpdb->get_results($query, ARRAY_A);
if (empty($bookings)) {
unset($booking_post_data['ID']);
$booking_id = wp_insert_post($booking_post_data);
}
$booking_id = $booking_id ? $booking_id : $booking_post_data['ID'];
if (!empty($booking_id)) {
wp_update_post(array('ID' => $booking_id, 'post_parent' => $order_id));
$booking_meta_arr = unserialize($booking_meta);
foreach ($booking_meta_arr as $key => $value) {
update_post_meta($booking_id, $key, maybe_unserialize($value));
}
$order = wc_get_order($order_id);
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item->get_product_id();
if ($product_id == $booking_meta_arr['_booking_product_id']) {
$order_item_id = $item->get_id();
update_post_meta($booking_id, '_booking_order_item_id', $order_item_id);
break;
}
}
}
}
}
}
}
delete_post_meta($order_id, 'booking_items');
}
view raw functions.php hosted with ❤ by GitHub

2. Import and Export membership data created by WooCommerce membership plugin

↑ Back to top

The plugin allows you to import and export the WooCommerce membership plans along with the user membership data. Insert the code snippet in the active theme’s functions.php file to achieve compatibility with the WooCommerce membership plan data.

<?php //Do not copy this line of code
add_filter('hf_csv_customer_post_columns', 'hf_alter_csv_header_for_wc_membeship_export',11);
function hf_alter_csv_header_for_wc_membeship_export($export_columns){
$export_columns['meta:wc_membership_plan_data'] = 'meta:wc_membership_plan_data';
$export_columns['meta:wc_user_membership_data'] = 'meta:wc_user_membership_data';
$export_columns['meta:wc_membership_plan_rule_data'] = 'meta:wc_membership_plan_rule_data';
return $export_columns;
}
add_filter('wt_ier_customer_csv_export_data', 'hf_customer_csv_export_data_for_wc_membeship_export', 11, 2);
function hf_customer_csv_export_data_for_wc_membeship_export($customer_data, $csv_columns){
global $wpdb;
$user_id = $customer_data['ID'];
if( in_array('meta:wc_user_membership_data', $csv_columns )){
$result_wc_user_membership_data = '';
$result_wc_user_membership_meta_data = '';
// get_post with type wc_user_membeship and post_author
$sql_wc_user_membership_data = "SELECT * FROM {$wpdb->posts} WHERE post_author=$user_id AND post_type='wc_user_membership'";
$result_wc_user_membership_data = $wpdb->get_results($sql_wc_user_membership_data,ARRAY_A);
if( !empty($result_wc_user_membership_data)){
$wc_user_membership_id = $result_wc_user_membership_data[0]['ID'];
$sql_wc_user_membership_meta_data = "SELECT meta_key,meta_value FROM {$wpdb->postmeta} WHERE post_id=$wc_user_membership_id";
$result_wc_user_membership_meta_data = $wpdb->get_results($sql_wc_user_membership_meta_data,ARRAY_A);
if(in_array('meta:wc_membership_plan_data', $csv_columns) && $result_wc_user_membership_data[0]['post_parent'] != 0 ){
$sql_wc_membership_plan_data= '';
$result_wc_membership_plan_meta_data= '';
$wc_membership_plan_id = $result_wc_user_membership_data[0]['post_parent'];
$sql_wc_membership_plan_data = "SELECT * FROM {$wpdb->posts} WHERE ID=$wc_membership_plan_id AND post_type='wc_membership_plan'";
$result_wc_membership_plan_data =$wpdb->get_results($sql_wc_membership_plan_data,ARRAY_A);
if( !empty($result_wc_membership_plan_data)){
$sql_wc_membership_plan_meta_data = "SELECT meta_key,meta_value FROM {$wpdb->postmeta} WHERE post_id=$wc_membership_plan_id";
$result_wc_membership_plan_meta_data = $wpdb->get_results($sql_wc_membership_plan_meta_data,ARRAY_A);
}
$wc_membership_plan_data['data'] = $result_wc_membership_plan_data;
$wc_membership_plan_data['meta_data'] = $result_wc_membership_plan_meta_data;
$wc_membership_plan_data = serialize( $wc_membership_plan_data);
}
}
$wc_user_membership_data['data'] = $result_wc_user_membership_data;
$wc_user_membership_data['meta_data'] = $result_wc_user_membership_meta_data;
$wc_user_membership_data = serialize($wc_user_membership_data);
}
$customer_data['wc_user_membership_data'] = $wc_user_membership_data;
$customer_data['wc_membership_plan_data'] = $wc_membership_plan_data;
$wc_membership_plan_rule_data = get_option('wc_memberships_rules');
if(get_option('wt_membership_rule_exported')!== 'YES'){
$customer_data['wc_membership_plan_rule_data'] = serialize($wc_membership_plan_rule_data);
update_option('wt_membership_rule_exported','YES');
}else{
$customer_data['wc_membership_plan_rule_data'] = '';
}
return $customer_data;
}
add_filter('wt_user_impexp_alter_user_meta', 'xa_user_impexp_alter_user_meta',11,3);
function xa_user_impexp_alter_user_meta($found_customer, $user_meta_fields, $meta_array){
global $wpdb;
$wc_membership_plan_inserted_id=false;
if(!empty( $meta_array['wc_membership_plan_rule_data'])){
update_option('wt_membership_plan_rule',$meta_array['wc_membership_plan_rule_data'] );
}
if($meta_array['wc_membership_plan_data']){
$wc_membership_plan_data = unserialize($meta_array['wc_membership_plan_data']);
$wc_membership_plan_data_data = $wc_membership_plan_data['data'][0];
$wc_membership_plan_data_meta_data = $wc_membership_plan_data['meta_data'];
if(!empty($wc_membership_plan_data_data)){
$old_membership_plan_id = $wc_membership_plan_data_data['ID'];
$post_title=$wc_membership_plan_data_data['post_title'];
$sql =$wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type='wc_membership_plan' AND post_title =%s",$post_title) ;
$wc_membership_plan_inserted_id = $wpdb->get_var($sql);
if(empty($wc_membership_plan_inserted_id)){
unset($wc_membership_plan_data_data['ID']);
$table_name = $wpdb->prefix . 'posts';
$wc_membership_plan_inserted_id = wp_insert_post($wc_membership_plan_data_data);
}
if($wc_membership_plan_inserted_id !==0 && ! is_wp_error($wc_membership_plan_inserted_id)){
foreach($wc_membership_plan_data_meta_data as $d_value){
update_post_meta($wc_membership_plan_inserted_id,$d_value['meta_key'], $d_value['meta_value']);
}
$wt_membership_rule = unserialize(get_option('wt_membership_plan_rule'));
if(!empty( $wt_membership_rule)){
foreach($wt_membership_rule as $key=> $rule){
if($rule['membership_plan_id'] == $old_membership_plan_id){
$wt_membership_rule[$key]['membership_plan_id'] = $wc_membership_plan_inserted_id;
}
}
update_option('wt_membership_plan_rule', $wt_membership_rule);
}
}
}
}
if($meta_array['wc_user_membership_data']){
$wc_user_membership_data= unserialize($meta_array['wc_user_membership_data']);
$wc_user_membership_data_data = $wc_user_membership_data['data'][0];
$wc_user_membership_data_meta_data = $wc_user_membership_data['meta_data'];
if( $wc_membership_plan_inserted_id){
$user_membership_check_sql=$wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type='wc_user_membership' AND post_author= %d",$found_customer);
$user_membership_id = $wpdb->get_var($user_membership_check_sql);
unset($wc_user_membership_data_data['ID']);
if(!empty($user_membership_id)){
$wc_user_membership_data_data['ID']= $user_membership_id;
}
$wc_user_membership_data_data['post_parent'] = $wc_membership_plan_inserted_id;
$wc_user_membership_data_data['post_author'] = $found_customer;
$user_membership_id = wp_insert_post($wc_user_membership_data_data);
if($user_membership_id!==0 && ! is_wp_error($user_membership_id)){
foreach($wc_user_membership_data_meta_data as $p_value){
update_post_meta($user_membership_id,$p_value['meta_key'], $p_value['meta_value']);
}
}
}
}
delete_post_meta($found_customer,'wc_user_membership_data');
delete_post_meta($found_customer,'wc_membership_plan_data');
return $found_customer;
}
add_filter('wt_iew_alter_export_data', 'wt_iew_alter_export_data',10,6);
function wt_iew_alter_export_data($export_data, $offset, $is_last_offset, $file_as, $to_export, $csv_delimiter) {
if ($is_last_offset) {
delete_option('wt_membership_rule_exported');
}
return $export_data;
}
add_filter('wt_iew_importer_done_import', 'wt_iew_importer_done_import');
function wt_iew_importer_done_import($to_process){
$wt_membership_rule = get_option('wt_membership_plan_rule');
delete_option('wt_membership_plan_rule');
update_option('wc_memberships_rules',$wt_membership_rule);
return $to_process;
}
view raw function.php hosted with ❤ by GitHub

3. Set email alert on cron import

↑ Back to top

On successful cron import, you can set email alerts in two ways. One without the import log, and another with the import log.

Email alert on successful cron import

↑ Back to top

On setting up the cron job, the import will occur as per the configuration. In order to receive an alert on successful auto import or export, simply insert the below-given code snippet into the function.php of the active child theme.

function wt_pipe_cron_ended(){
$email = 'asd@example.org, ert@example.org';
wp_mail( $email, "Product Auto Import Export", 'Product scheduled import completed.' );
}
add_action('wt_ier_scheduled_action_finished', 'wt_pipe_cron_ended');
view raw functions.php hosted with ❤ by GitHub

In the above snippet, the email subject is ‘Product Auto Import Export’ and the body of the mail is the ‘Product scheduled import completed’. You can replace your personalized email content in the snippet as per your preference.

Email alert with import log

↑ Back to top

After a successful cron import, you can receive an email containing the import log with the below mentioned snippet:

<?php
add_action('wt_ier_scheduled_action_finished', 'wt_scheduled_action_finished', 10,1);
function wt_scheduled_action_finished($out){
ini_set('max_execution_time', -1);
ini_set('memory_limit', -1);
$wt_log_path = WP_CONTENT_DIR . '/webtoffee_iew_log'; // wp content path – fixed WP violation
$files = glob("$wt_log_path/*.*");
$files = array_combine($files, array_map('filectime', $files));
arsort($files);
$destination = key($files);
$email = 'user@mail.com';//Input your email ID
$object= 'Products imported successfully!';//Specify the subject for email
$message = 'OK. Automatic import performed SUCCESSFULLY. : ' .date('l d/m/Y H:i:s', time() ). '';//Enter the message
$mail_attachment = array($destination);
$headers = '';
wp_mail( $email, $object, $message,$headers,$mail_attachment );
}
view raw functions.php hosted with ❤ by GitHub

4. Support for WebP images

↑ Back to top

The plugin imports and exports all the products with images. And, all the basic image formats such as: JPEG, JPG and PNG are supported. However, the plugin does not support WebP images by default!

To get support for WebP images, add this code snippet to your site, and then import the CSV containing WebP images. 

<?php //do not copy this line
function wt_mime_types($mime_types) {
$mime_types['webp'] = 'image/webp'; //Adding webp extension
return $mime_types;
}
add_filter('woocommerce_rest_allowed_image_mime_types', 'wt_mime_types', 1, 1);
view raw functions.php hosted with ❤ by GitHub

5. Filter and export WooCommerce products using custom taxonomy

↑ Back to top

There may be instances where you may want to add some additional categories, tags, and brands, known as taxonomies in your WordPress site. By default, WordPress does not support custom taxonomies, and to make them work, we need third party plugins.  

When using the import export suite plugin, to filter the export product data based on these custom taxonomies, we can use this code snippet.

<?php // Please do not copy this line
// generalize filter by custom taxonomy
$GLOBALS['wt_custom_taxonomy_name'] = 'product_brand';
add_filter('wt_iew_exporter_alter_filter_fields', 'wt_add_additional_exporter_alter_filter_fields', 10, 3);
if(!function_exists('wt_add_additional_exporter_alter_filter_fields')){
function wt_add_additional_exporter_alter_filter_fields($fields, $base, $filter_form_data){
if ('product' != $base) {
return $fields;
}
$fields[$GLOBALS['wt_custom_taxonomy_name']] = array(
'label' => __('Product '. ucwords(str_replace('_', ' ', $GLOBALS['wt_custom_taxonomy_name'])), 'wt-import-export-for-woo'),
'placeholder' => __('All '.ucwords(str_replace('_', ' ', $GLOBALS['wt_custom_taxonomy_name'])), 'wt-import-export-for-woo'),
'field_name' => $GLOBALS['wt_custom_taxonomy_name'],
'sele_vals' => get_custom_taxonomy(),
'help_text' => __('Filter products by their '.ucwords(str_replace('_', ' ', $GLOBALS['wt_custom_taxonomy_name'])).'. You can export multiple together.', 'wt-import-export-for-woo'),
'type' => 'multi_select',
'css_class' => 'wc-enhanced-select',
'validation_rule' => array('type'=>'text_arr')
);
return $fields;
}
}
if(!function_exists('get_custom_taxonomy')){
function get_custom_taxonomy(){
$out = array();
$product_categories = get_terms(array(
'taxonomy' => $GLOBALS['wt_custom_taxonomy_name'],
'hide_empty' => false,
) );
if (!is_wp_error($product_categories)) {
$version = get_bloginfo('version');
foreach ($product_categories as $category) {
$out[$category->slug] = (( $version < '4.8') ? $category->name : get_term_parents_list($category->term_id, $GLOBALS['wt_custom_taxonomy_name'], array('separator' => ' -> ')));
}
}
return $out;
}
}
add_filter('woocommerce_csv_product_export_args','wt_woocommerce_csv_product_export_args');
if(!function_exists('wt_woocommerce_csv_product_export_args')){
function wt_woocommerce_csv_product_export_args($args){
$form_data=(isset($_POST['form_data']) ? Wt_Import_Export_For_Woo_Common_Helper::process_formdata(maybe_unserialize(($_POST['form_data']))) : array());
$prod_brand = !empty($form_data['filter_form_data']['wt_iew_'.$GLOBALS['wt_custom_taxonomy_name']]) ? $form_data['filter_form_data']['wt_iew_'.$GLOBALS['wt_custom_taxonomy_name']] : array();
if(!empty($prod_brand)){
$args['filter_by_custom_taxonomy'] = $prod_brand;
}
return $args;
}
}
if(!function_exists('wt_handle_custom_query_var')){
function wt_handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['filter_by_custom_taxonomy'] ) ) {
$query['tax_query'][] = array(
'taxonomy' => $GLOBALS['wt_custom_taxonomy_name'],
'field' => 'slug',
'terms' => $query_vars['filter_by_custom_taxonomy'],
);
}
return $query;
}
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'wt_handle_custom_query_var', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

To deal with different custom taxonomies, you just have to change the existing taxonomy name: product_brand at line 3 to the required taxonomy name.

6. Export and import BuddyPress data

↑ Back to top

The plugin allows you to import and export the BuddyPress data. Insert the code snippet in the active theme’s functions.php file to achieve compatibility with the BuddyPress data.

<?php //Do not copy this line of code
add_filter('wt_user_alter_csv_header', 'wt_user_alter_csv_header');
add_filter('hf_customer_csv_export_data', 'hf_customer_csv_export_data');
add_action('wt_customer_csv_import_data', 'wt_customer_csv_import_data',10,2);
add_filter('wt_iew_alter_export_data', 'wt_iew_alter_export_data',10,6);
add_filter('wt_user_importer_pre_parse_data', 'wt_user_importer_pre_parse_data');
function wt_user_alter_csv_header($row) {
global $wpdb;
$field_id = $wpdb->get_col('SELECT id FROM ' . $wpdb->prefix . "bp_xprofile_fields where parent_id= 0");
foreach ($field_id as $id) {
$obj_fname = xprofile_get_field($id);
$fname = $obj_fname->name;
$row['bp_xprofile:' . $fname] = 'meta:bp_xprofile:' . $fname;
}
$row['medlemstyp'] = 'tax:medlemstyp';
$row['bp_xprofile_group_data'] = 'meta:bp_xprofilegroup';
$row['bp_group_data'] = 'meta:bp_group_data';
$row['membership'] = 'meta:membership_wt';
return $row;
}
function hf_customer_csv_export_data($customer_data) {
$id = $customer_data['customer_id'];
$membership = bp_get_member_type($id,false);
$user = get_user_by('id', $id);
global $wpdb;
$field_id = $wpdb->get_col('SELECT id FROM ' . $wpdb->prefix . "bp_xprofile_fields where parent_id= 0");
foreach ($field_id as $id) {
$obj_fname = xprofile_get_field($id);
$fname = $obj_fname->name;
// $customer_data['bp_xprofile:' . $fname] = xprofile_get_field_data($id, $user->ID);
if (is_array(xprofile_get_field_data($id, $user->ID))) {
$array_data = xprofile_get_field_data($id, $user->ID);
$seriali_data = serialize($array_data);
$customer_data['bp_xprofile:' . $fname] = $seriali_data;
} else {
$customer_data['bp_xprofile:' . $fname] = xprofile_get_field_data($id, $user->ID);
}
}
$term_name = array();
foreach ($membership as $key => $mvalue) {
$term_data = get_term_by('slug', $mvalue, 'bp_member_type');
if($term_data->term_id){
$term_name[] = get_term_meta( $term_data->term_id, 'bp_type_singular_name', true);
}
}
$customer_data['medlemstyp'] = $term_name ? implode(",",$term_name) : $membership;
$xprof_groups = $wpdb->get_results('SELECT xg.ID,xf.id as act_parent,xg.name as gname,xf.name as fname,xf.parent_id,xf.type FROM ' . $wpdb->prefix . "bp_xprofile_groups xg INNER JOIN " . $wpdb->prefix . "bp_xprofile_fields xf ON xg.id = xf.group_id", ARRAY_A);
$bp_group_data_flag = get_option('Wt_bp_gp_data');
if ($bp_group_data_flag != 'set') {
$customer_data['bp_xprofile_group_data'] = serialize($xprof_groups);
add_option('Wt_bp_gp_data', 'set');
}
$bp_group_data = $wpdb->get_results('SELECT u.group_id,m.name,m.slug,m.description,m.status FROM ' . $wpdb->prefix . "bp_groups_members u INNER JOIN " . $wpdb->prefix . "bp_groups m ON m.id = u.group_id WHERE u.user_id =" . $user->ID, ARRAY_A);
$customer_data['bp_group_data'] = $bp_group_data ? serialize($bp_group_data) : '';
$customer_data['membership'] = implode(",", $membership);
return $customer_data;
}
function wt_customer_csv_import_data($parsed_item, $user_id) {
$data = get_user_meta($user_id, 'membership_wt', true);
if (!empty($data)) {
$membership = explode(",", $data);
foreach ($membership as $key => $mvalue) {
$term_id = term_exists($mvalue);
if ($term_id) {
bp_set_member_type($user_id, $mvalue, false);
//wp_set_post_terms($user_id, array($mvalue), 'bp_member_type', false);
}
}
}
@delete_user_meta($user_id, 'membership_wt');
foreach ($parsed_item['meta_mapping_fields']['meta'] as $xp_key => $xp_value) {
if (strstr($xp_key, 'meta:bp_xprofile:') || $xp_key == 'meta:bp_xprofilegroup' || $xp_key == 'meta:bp_group_data') {
$xp_array[$xp_key] = $xp_value;
}
}
foreach ($xp_array as $item_keys => $item_values) {
if (strstr($item_keys, 'meta:bp_')) {
$bp_data[ltrim($item_keys, 'meta:')] = $item_values;
}
}
foreach ($bp_data as $key => $data) {
if (strstr($key, 'bp_xprofilegroup')) {
$xprofilegroup = unserialize($data);
}
}
global $wpdb;
foreach ($xprofilegroup as $gf_key => $gf_value) {
$gname = $gf_value['gname'];
$xp_group_data = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}bp_xprofile_groups where name=\"$gname\"");
if ($xp_group_data) {
$metakey = 'wt_xprofile_connect_parent';
$fname = $gf_value['fname'];
$ftype = $gf_value['type'];
$act_parent = $gf_value['act_parent'];
$parent_id_xp = $gf_value['parent_id'];
$gid = $xp_group_data[0];
$xp_field_data = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}bp_xprofile_fields where name=\"$fname\" and group_id=$xp_group_data[0]");
if (!$xp_field_data) {
if ($act_parent > 0) {
$xp_field_data_parent = $wpdb->get_col("SELECT object_id FROM {$wpdb->prefix}bp_xprofile_meta where meta_key=\"$metakey\" and meta_value=\"$parent_id_xp\"");
$act_parents = $xp_field_data_parent[0] ? $xp_field_data_parent[0] : 0;
}
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_fields (name,group_id,parent_id,type) VALUES ('$fname',$gid,$act_parents,'$ftype')");
$fid = $wpdb->insert_id;
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_meta (object_id,meta_key,meta_value) VALUES ($fid,'$metakey',$act_parent)");
//create
}
} else {
$id = $wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_groups (name) VALUES ('$gname')");
$lastid = $wpdb->insert_id;
$fname = $gf_value['fname'];
$ftype = $gf_value['type'];
$act_parent = $gf_value['act_parent'];
$parent_id_xp = $gf_value['parent_id'];
$metakey = 'wt_xprofile_connect_parent';
$gid = $lastid;
if ($act_parent > 0) {
$xp_field_data_parent = $wpdb->get_col("SELECT object_id FROM {$wpdb->prefix}bp_xprofile_meta where meta_key=\"$metakey\" and meta_value=\"$parent_id_xp\"");
$act_parents = $xp_field_data_parent[0] ? $xp_field_data_parent[0] : 0;
}
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_fields (name,group_id,parent_id,type) VALUES ('$fname',$gid,$act_parents,'$ftype')");
$fid = $wpdb->insert_id;
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_meta (object_id,meta_key,meta_value) VALUES ($fid,'$metakey',$act_parent)");
//create
}
}
$wpdb->query("DELETE FROM {$wpdb->prefix}bp_xprofile_meta WHERE meta_key=\"$metakey\" ");
foreach ($bp_data as $xpro_key => $xpro_value) {
if (strstr($xpro_key, 'bp_xprofile:')) {
$dp_xfield = explode(":", $xpro_key);
$key = $dp_xfield[1];
$xp_field_ids = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}bp_xprofile_fields where name=\"$key\"");
$xp_field_id = $xp_field_ids[0];
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_data (field_id,user_id,value) VALUES ($xp_field_id,$user_id,'$xpro_value')");
}
}
return $parsed_item;
}
function wt_iew_alter_export_data($export_data, $offset, $is_last_offset, $file_as, $to_export, $csv_delimiter) {
if ($is_last_offset) {
delete_option('Wt_bp_gp_data');
}
return $export_data;
}
function wt_user_importer_pre_parse_data($data) {
foreach ($data['meta_mapping_fields']['meta'] as $x_key => $x_value) {
if (strstr($x_key, 'meta:bp_xprofile:') || $x_key == 'meta:bp_xprofilegroup' || $x_key == 'meta:bp_group_data') {
$x_array[$x_key] = $x_value;
unset($data['meta_mapping_fields']['meta'][$x_key]);
}
}
return $data;
}
view raw functions.php hosted with ❤ by GitHub

Therefore, additional columns related to BuddyPress data will be appended in the exported CSV. Likewise, you can import BuddyPress data by mapping concerned values.

7. Add Custom Metadata

↑ Back to top

All you need to is add the code in the function.php file of the active child theme and the change the parameters ‘RDI’ and ‘LDI’ with the custom field names that you need to export.

<?php // do not copy this line
add_filter('hf_alter_csv_header', 'hf_csv_order_add_more_columns', 10, 1);
function hf_csv_order_add_more_columns($csv_columns)
{
$new_csv_columns = array();
$lineitem_csv_columns = array();
$temp_order_metadata = array('RDI','LDI'); //Give the custom field required to export
foreach ($csv_columns as $data_key => $data_value) {
if(strstr($data_key, 'line_item_')){
$lineitem_csv_columns[$data_key] = $data_value;
unset($csv_columns[$data_key]);
}
}
$new_csv_columns = array_merge($csv_columns,$temp_order_metadata,$lineitem_csv_columns);
return $new_csv_columns;
}
add_filter('hf_alter_csv_order_data', 'wt_alter_csv_order_data', 10, 1);
function wt_alter_csv_order_data($order_data) {
$new_csv_columns = array();
$lineitem_csv_columns = array();
$temp_order_metadata = array('RDI', 'LDI'); //Give the custom field required to export
foreach ($order_data as $d_key => $d_value) {
if (strstr($d_key, 'line_item_')) {
$lineitem_csv_columns[$d_key] = $d_value;
unset($order_data[$d_key]);
}
}
foreach ($temp_order_metadata as $key => $value) {
$order_data[$value] = get_post_meta($order_data['order_id'], $value, true);
}
$new_csv_columns = array_merge($order_data, $lineitem_csv_columns);
return $new_csv_columns;
}
view raw functions.php hosted with ❤ by GitHub

8. Alter subscription export CSV

↑ Back to top

The code snippet below will allow you to export additional metadata along with subscriptions when using the import export suite plugin. Change the index and the value of the array variable $csv_columns to the name of the data to be exported.

Add this code snippet to the function.php file of the active child theme.

<?php // do not copy this line
add_filter('hf_alter_coupon_csv_header', 'wt_csv_subscription_add_more_columns', 10, 1);
function wt_csv_subscription_add_more_columns($csv_columns) {
$csv_columns['meta:_stripe_source_id'] = 'meta:_stripe_source_id';
$csv_columns['meta:_stripe_customer_id'] = 'meta:_stripe_customer_id';
return $csv_columns;
}
add_filter('hf_alter_subscription_data', 'wt_csv_subscription_add_more_data', 10, 1);
function wt_csv_subscription_add_more_data($order_data) {
$additional_meta['meta:_stripe_source_id'] = '_stripe_source_id';
$additional_meta['meta:_stripe_customer_id'] = '_stripe_customer_id';
foreach ($additional_meta as $key => $val) {
$order_data[$key] = get_post_meta($order_data['subscription_id'], $val, TRUE);
}
return $order_data;
}
view raw functions.php hosted with ❤ by GitHub

9. Import and Export Order Meta Created Using WooCommerce Booking Plugin

↑ Back to top

With the import-export suite plugin, you can easily import or export order metadata created by the WooCommerce Booking plugin.

Insert the code snippet in the functions.php file of your active child theme before importing or exporting the orders.

<?php
add_filter('wt_iew_alter_export_data', 'wt_iew_alter_export_booking_data', 10, 6);
function wt_iew_alter_export_booking_data($export_data, $offset, $is_last_offset, $file_as, $to_export, $csv_delimiter) {
if ($to_export == 'order') {
if (isset($export_data['head_data'])) {
$export_data['head_data']['meta:booking_items'] = 'meta:booking_items';
}
if (isset($export_data['body_data'])) {
foreach ($export_data['body_data'] as $ord_key => $ord_value) {
global $wpdb;
$order_id = $ord_value['order_id'];
$query = "SELECT * FROM {$wpdb->posts} WHERE post_type = 'wc_booking' AND post_parent = " . $order_id;
$bookings = $wpdb->get_results($query, ARRAY_A);
if (!empty($bookings)) {
foreach ($bookings as $booking) {
$query1 = "SELECT meta_key,meta_value FROM {$wpdb->postmeta} WHERE post_id = " . $booking['ID'];
$booking_meta = $wpdb->get_results($query1, ARRAY_N);
//unset($booking['ID']);
foreach ($booking_meta as $meta) {
if ($meta[0] == '_booking_order_item_id')
continue;
$new_meta[$meta[0]] = $meta[1];
}
$booking_items[] = implode('|', array(
'data:' . serialize($booking),
'meta:' . serialize($new_meta)
));
unset($booking,$new_meta);
}
$export_data['body_data'][$ord_key]['meta:booking_items'] = implode('||', $booking_items);
unset($booking_items);
}
}
}
}
return $export_data;
}
add_filter('wt_iew_importer_skip_from_evaluation', 'wt_iew_importer_skip_from_booking_evaluation');
function wt_iew_importer_skip_from_booking_evaluation($evl_arra) {
$evl_arra[] = 'meta:booking_items';
return $evl_arra;
}
add_filter('wt_woocommerce_order_importer_pre_parse_data','wt_woocommerce_order_importer_pre_parse_booking_data');
function wt_woocommerce_order_importer_pre_parse_booking_data($item){
if(!empty($item['meta_mapping_fields']['meta']['meta:booking_items'])){
$booking_item = explode('||', $item['meta_mapping_fields']['meta']['meta:booking_items']);
$item['meta_mapping_fields']['meta']['meta:booking_items'] = $booking_item;
}
return $item;
}
add_action('wt_woocommerce_order_import_inserted_object', 'wt_woocommerce_booking_order_import_inserted_object', 10, 2);
function wt_woocommerce_booking_order_import_inserted_object($order, $data) {
$order_id = $order->get_id();
global $wpdb;
foreach ($data['meta_data'] as $data_key => $data_value) {
if ($data_value['key'] == 'booking_items') {
if (!empty($data_value['value'])) {
foreach ($data_value['value'] as $booking) {
$_bitem_meta = explode('|', $booking);
$booking_post = array_shift($_bitem_meta);
$booking_post = substr($booking_post, strpos($booking_post, ":") + 1);
$booking_meta = array_shift($_bitem_meta);
$booking_meta = substr($booking_meta, strpos($booking_meta, ":") + 1);
$booking_post_data = unserialize($booking_post);
$query = "SELECT * FROM {$wpdb->posts} WHERE post_type = 'wc_booking' AND ID =" . $booking_post_data['ID'] . " AND post_parent = " . $order_id;
$bookings = $wpdb->get_results($query, ARRAY_A);
if (empty($bookings)) {
unset($booking_post_data['ID']);
$booking_id = wp_insert_post($booking_post_data);
}
$booking_id = $booking_id ? $booking_id : $booking_post_data['ID'];
if (!empty($booking_id)) {
wp_update_post(array('ID' => $booking_id, 'post_parent' => $order_id));
$booking_meta_arr = unserialize($booking_meta);
foreach ($booking_meta_arr as $key => $value) {
update_post_meta($booking_id, $key, maybe_unserialize($value));
}
$order = wc_get_order($order_id);
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item->get_product_id();
if ($product_id == $booking_meta_arr['_booking_product_id']) {
$order_item_id = $item->get_id();
update_post_meta($booking_id, '_booking_order_item_id', $order_item_id);
break;
}
}
}
}
}
}
}
delete_post_meta($order_id, 'booking_items');
}
view raw functions.php hosted with ❤ by GitHub

10. Export URL Coupons Meta Data along with Coupon details

↑ Back to top

For exporting URL coupon meta data along with coupon details, paste the below-mentioned code snippet in the child theme’s functions.php file. 

<?php // do not copy this line
add_filter('hf_alter_coupon_csv_header', 'wt_csv_coupon_add_more_columns', 10, 1);
function wt_csv_coupon_add_more_columns($csv_columns) {
$csv_columns['meta:_wc_url_coupons_unique_url'] = 'meta:_wc_url_coupons_unique_url';
$csv_columns['meta:_wc_url_coupons_redirect_page'] = 'meta:_wc_url_coupons_redirect_page';
$csv_columns['meta:_wc_url_coupons_redirect_page_type'] = 'meta:_wc_url_coupons_redirect_page_type';
$csv_columns['meta:_wc_url_coupons_product_ids'] = 'meta:_wc_url_coupons_product_ids';
$csv_columns['meta:_wc_url_coupons_defer_apply'] = 'meta:_wc_url_coupons_defer_apply';
return $csv_columns;
}
add_filter('hf_alter_coupon_csv_data', 'wt_csv_coupon_add_more_data', 10, 1);
function wt_csv_coupon_add_more_data($coupon_data) {
$coupon_id = $coupon_data['ID'];
$coupon_data['meta:_wc_url_coupons_unique_url'] = get_post_meta( $coupon_id, '_wc_url_coupons_unique_url' , TRUE);
$coupon_data['meta:_wc_url_coupons_redirect_page'] = get_post_meta( $coupon_id, '_wc_url_coupons_redirect_page' , TRUE);
$coupon_data['meta:_wc_url_coupons_redirect_page_type'] = get_post_meta( $coupon_id, '_wc_url_coupons_redirect_page_type' , TRUE);
$product_ids = get_post_meta( $coupon_id, '_wc_url_coupons_product_ids' , TRUE);
$coupon_data['meta:_wc_url_coupons_product_ids'] = (!empty(($product_ids))) ? implode(',', $product_ids): '';
$coupon_data['meta:_wc_url_coupons_defer_apply'] = get_post_meta( $coupon_id, '_wc_url_coupons_defer_apply' , TRUE);
return $coupon_data;
}
view raw functions.php hosted with ❤ by GitHub

11. Export Customer Roles and Additional Order Meta Data

↑ Back to top

For exporting customer roles and additional metadata of the orders, paste the below-mentioned code snippet in the child theme’s functions.php

<?php // do not copy this line
/ here 'meta:meta_key_1' is the key used for the internal use.
// meta key 1 is used as the header in csv.
// meta_key_1 is the meta key present in the database
add_filter('hf_alter_csv_header', 'hf_csv_order_add_more_columns', 10, 1);
function hf_csv_order_add_more_columns($csv_columns) {
$csv_columns['meta:meta_key_1'] = 'meta key 1';
$csv_columns['meta:meta_key_2'] = 'meta key 2';
$csv_columns['meta:meta_key_3'] = 'meta key 3';
$csv_columns['role'] = 'role';
return $csv_columns;
}
add_filter('hf_alter_csv_order_data', 'hf_csv_order_add_more_data', 10, 1);
function hf_csv_order_add_more_data($order_data) {
$additional_meta['meta:meta_key_1'] = 'meta_key_1';
$additional_meta['meta:meta_key_2'] = 'meta_key_2';
$additional_meta['meta:meta_key_3'] = 'meta_key_3';
foreach ($additional_meta as $key => $val) {
$order_data[$key] = get_post_meta($order_data['order_id'], $val, TRUE);
}
if ($order_data['customer_id'] === 0) {
$order_data['role'] = 'Guest';
} else {
$user_info = get_userdata($order_data['customer_id']);
$roles = implode(', ', $user_info->roles) . " ";
$order_data['role'] = $roles;
}
return $order_data;
}
view raw functions.php hosted with ❤ by GitHub