2022 Nov BFCM Marketplace Sale

  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. Set email alert on cron import

↑ Back to top

On successful cron import, you can set email alerts in two ways. One without 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

3. 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

4. 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 for WooCommerce, to filter the export product data on the basis of 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.

5. 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.

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['bp_xprofile_group_data'] = 'meta:bp_xprofilegroup';
$row['bp_group_data'] = 'meta:bp_group_data';
return $row;
}
function hf_customer_csv_export_data($customer_data) {
$id = $customer_data['customer_id'];
$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);
}
}
$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) : '';
return $customer_data;
}
function wt_customer_csv_import_data($parsed_item, $user_id) {
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.

6. 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

7. Alter subscription export CSV

↑ Back to top

The below code snippet will allow you to export additional metadata along with subscription using Import export Suite for WooCommerce. 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

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

↑ Back to top

With Import export Suite for WooCommerce plugin, you can easily import or export order meta created by 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

9. Export URL Coupons Meta Data along with Coupon details

↑ Back to top

For exporting URL Coupons Meta Data along with Coupon details using Import export Suite for WooCommerce below code snippet can be used in the child theme’s functions.php. 

<?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

10. Export Customer Roles and Additional Order Meta Data

↑ Back to top

For exporting customer role and additional metadata of the orders using Import export Suite for WooCommerce below code snippet can be used in the child theme’s functions.php

<?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) {
$csv_columns['pdf_invoice_num'] = 'Invoice Number';
$csv_columns['Payer PayPal address'] = 'Payer PayPal address';
$csv_columns['PayPal Transaction Fee'] = 'PayPal Transaction Fee';
$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['pdf_invoice_num'] = '_invoice_number_display';
$additional_meta['Payer PayPal address'] = 'Payer PayPal address';
$additional_meta['PayPal Transaction Fee'] = 'PayPal Transaction Fee';
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

WooCommerce

The most customizable eCommerce platform for building your online business.

  • 30-day money-back guarantee
  • Support teams across the world
  • Safe and secure online payment