Last active
June 22, 2021 09:36
-
-
Save webtoffee-git/4f8d8876de5f7ac887e80acf3729fb0e to your computer and use it in GitHub Desktop.
Export and Import order meta created using WooCommerce Booking Plugin using - WebToffee Order Import Export plugin for WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment