Created
December 2, 2016 19:48
-
-
Save anthonysbrown/049306dc826be107f6768e789f8c9c64 to your computer and use it in GitHub Desktop.
Copy woocommerce orders from one database to another
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 | |
/* | |
Plugin Name: Woocommerce migrate orders | |
Author: Anthony Brown (codeable) | |
Version: 1.0 | |
*/ | |
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
class wc_migrate_orders{ | |
function __construct(){ | |
$this->db_username = ''; | |
$this->db_password = ''; | |
$this->db_database = ''; | |
$this->db_host = ''; | |
$this->db_prefix = 'wp_'; | |
} | |
function insert_row($data,$table){ | |
global $wpdb; | |
foreach($data as $column=>$value){ | |
if($column != 'ID' ){ | |
$insert[$column] = $value; | |
} | |
} | |
$insert_id = $wpdb->insert($table, $insert); | |
return $wpdb->insert_id; | |
} | |
function insert_post_meta($old_id,$post_id){ | |
global $wpdb; | |
$old_db = new wpdb($this->db_username ,$this->db_password ,$this->db_database,$this->db_host ); | |
$query = "SELECT * FROM ".$this->db_prefix."postmeta where post_id = '".$old_id."' "; | |
$r = $old_db->get_results($query, ARRAY_A); | |
for ($i = 0; $i < count($r); $i++) { | |
$data['post_id'] = $post_id; | |
$data['meta_key'] = $r[$i]['meta_key']; | |
$data['meta_value'] = $r[$i]['meta_value']; | |
$this->insert_row($data,''.$wpdb->prefix.'postmeta'); | |
#echo '<p>inserted: '.$r[$i]['meta_key'].''; | |
unset($data); | |
} | |
} | |
function insert_order_items_meta($old_id,$post_id){ | |
global $wpdb; | |
$old_db = new wpdb($this->db_username ,$this->db_password ,$this->db_database,$this->db_host ); | |
$query = "SELECT * FROM ".$this->db_prefix."woocommerce_order_itemmeta where order_item_id = '".$old_id."' "; | |
$r = $old_db->get_results($query, ARRAY_A); | |
for ($i = 0; $i < count($r); $i++) { | |
$data['order_item_id'] = $post_id; | |
$data['meta_key'] = $r[$i]['meta_key']; | |
$data['meta_value'] = $r[$i]['meta_value']; | |
$item_meta_id = $this->insert_row($data,''.$wpdb->prefix.'woocommerce_order_itemmeta'); | |
#echo '<p>inserted: '.$r[$i]['meta_key'].''; | |
unset($data); | |
} | |
} | |
function insert_order_items($old_id,$post_id){ | |
global $wpdb; | |
$old_db = new wpdb($this->db_username ,$this->db_password ,$this->db_database,$this->db_host ); | |
$query = "SELECT * FROM ".$this->db_prefix."woocommerce_order_items where order_id = '".$old_id."' "; | |
$r = $old_db->get_results($query, ARRAY_A); | |
for ($i = 0; $i < count($r); $i++) { | |
$data['order_id'] = $post_id; | |
$data['order_item_name'] = $r[$i]['order_item_name']; | |
$data['order_item_type'] = $r[$i]['order_item_type']; | |
$item_meta_id = $this->insert_row($data,''.$wpdb->prefix.'woocommerce_order_items'); | |
$this->insert_order_items_meta($r[$i]['order_item_id'],$item_meta_id); | |
#echo '<p>inserted: '. $r[$i]['order_item_name'].''; | |
unset($data); | |
} | |
} | |
function migrate(){ | |
global $wpdb; | |
$old_db = new wpdb($this->db_username ,$this->db_password ,$this->db_database,$this->db_host ); | |
$order_id = $_POST['order_id']; | |
# print_r($old_db); | |
$query = "SELECT * FROM ".$this->db_prefix."posts where ID = ".$order_id ." "; | |
$r = $old_db->get_results($query, ARRAY_A); | |
for ($i = 0; $i < count($r); $i++) { | |
$post_id = $this->insert_row($r[$i],''.$wpdb->prefix.'posts'); | |
#echo 'Order: '.$post_id.''; | |
$this->insert_post_meta($r[$i]['ID'], $post_id); | |
$this->insert_order_items($r[$i]['ID'], $post_id); | |
update_post_meta($post_id, '_wc_import_old_order', $r[$i]['ID']); | |
echo 'Inserted New Order: '.$post_id.' from old order '.$order_id .' <br> '; | |
} | |
exit; | |
} | |
function get_ids(){ | |
$old_db = new wpdb($this->db_username ,$this->db_password ,$this->db_database,$this->db_host ); | |
$query = "SELECT * FROM ".$this->db_prefix."posts where post_type = 'shop_order' and post_status = 'wc-completed' "; | |
$r = $old_db->get_results($query, ARRAY_A); | |
for ($i = 0; $i < count($r); $i++) { | |
$id[]= $r[$i]['ID']; | |
} | |
echo json_encode($id); | |
exit; | |
} | |
function view(){ | |
echo '<h1>Woocommerce Migrate Orders</h1>'; | |
echo '<script type="text/javascript"> | |
jQuery( document ).ready(function() { | |
jQuery(".start-migrate-orders").on("click", function(){ | |
var data = { | |
"action": "woocommerce_migrate_orders_get_ids" | |
}; | |
jQuery.post(ajaxurl, data, function(response) { | |
obj = jQuery.parseJSON( response ); | |
console.log(obj); | |
jQuery.each( obj, function( key, value ) { | |
jQuery.post(ajaxurl, { | |
"action": "woocommerce_migrate_orders_process", | |
"order_id": value | |
}, function(entry_response) { | |
jQuery(".wc-migrate-orders-output").prepend(entry_response); | |
}); | |
}); | |
}); | |
return false; | |
}); | |
}); | |
</script> | |
<div style="padding:10px;margin:10px 0px"><a href="#" class="start-migrate-orders">Start Migrating Orders</a></div> | |
<div class="wc-migrate-orders-output" style="padding:10px;margin:20px 0px;background-color:#FFF"></div> | |
'; | |
} | |
function menu(){ | |
add_menu_page( | |
__( 'Woocommerce Migrate Orders', 'textdomain' ), | |
'Woocommece Migrate Orders', | |
'manage_options', | |
'wc-migrate-orders', | |
array($this,'view') | |
); | |
} | |
} | |
$wc_migrate_orders = new wc_migrate_orders; | |
#add_action('init', array( $wc_migrate_orders, 'migrate')); | |
add_action( 'admin_menu', array($wc_migrate_orders, 'menu')); | |
add_action( 'wp_ajax_woocommerce_migrate_orders_get_ids', array($wc_migrate_orders, 'get_ids') ); | |
add_action( 'wp_ajax_woocommerce_migrate_orders_process', array($wc_migrate_orders, 'migrate') ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment