Skip to content

Instantly share code, notes, and snippets.

@JasvinderSingh1
Created December 18, 2019 12:15
Show Gist options
  • Save JasvinderSingh1/b3f4f60cbcabbc1a6fb1ca039c4d4c5f to your computer and use it in GitHub Desktop.
Save JasvinderSingh1/b3f4f60cbcabbc1a6fb1ca039c4d4c5f to your computer and use it in GitHub Desktop.
Custom script to import order from a different database to WooCommerce
<?php
/*
* Custom script to import order from a different database to WooCommerce
*/
function custom_import_order_shortcode_fun2(){
ob_start();
global $wpdb;
//
// $old_orders = $wpdb->get_results( "SELECT * FROM `orders` WHERE orderid BETWEEN 68980 AND 69005");
// $order_products_pp = $wpdb->get_results( "SELECT orderorderid FROM `order_products` WHERE ordprodid BETWEEN 44 AND 359", ARRAY_A);
// $order_products_column = array_column($order_products_pp, 'orderorderid');
$old_orders = $wpdb->get_results( "SELECT * FROM `wp_cart66_orders`");
$i = 1;
foreach( $old_orders as $old_orders_each ) {
if ($i == 1) {
$orderid = $old_orders_each->id;
echo '$orderid :: '.$orderid; echo "<br>";
$status = '';
if ($old_orders_each->status == 'completed') {
$status = 'wc-completed';
} else if ($old_orders_each->status == 'completed phone order') {
$status = 'wc-comp-phone-order';
} else if ($old_orders_each->status == 'completed web order') {
$status = 'wc-comp-web-order';
} else if ($old_orders_each->status == 'cancelled') {
$status = 'wc-cancelled';
}
echo "Old Status: ".$old_orders_each->status." New Status: ".$status.'<br>';
// Create post object
$order_data = array(
'post_author' => 1,
'post_date' => $old_orders_each->ordered_on,
'post_date_gmt' => $old_orders_each->ordered_on,
'post_title' => 'Order &ndash; '.date('F d, Y @ H:i A', strtotime($old_orders_each->ordered_on)),
'post_excerpt' => '',
'post_status' => $status,
'post_password' => '',
'post_name' => 'order-'.date('M-d-Y-Hi-A', strtotime($old_orders_each->ordered_on)),
'post_modified' => $old_orders_each->ordered_on,
'post_modified_gmt' => $old_orders_each->ordered_on,
'post_parent' => '0',
'post_type' => 'shop_order'
);
// Insert the post into the database
$post_id = wp_insert_post( $order_data );
echo "New order ID: ".$post_id.'<br><br>';
//postmeta
$new_user_id = get_user_by( 'email', $old_orders_each->email );
//$new_user_id = $wpdb->get_results("SELECT * FROM `wp_cart66_order_items` WHERE `order_id` = '".$old_orders_each->id."'", OBJECT);
update_post_meta( $post_id, '_order_key', '' );
update_post_meta( $post_id, '_customer_user', $new_user_id->ID );
// if ($old_orders_each->orderpaymentmodule == 'checkout_paypal') {
// update_post_meta( $post_id, '_payment_method', 'paypal' );
// update_post_meta( $post_id, '_payment_method_title', 'PayPal' );
// } else if ($old_orders_each->orderpaymentmodule == 'checkout_authorizenet') {
// update_post_meta( $post_id, '_payment_method', 'authorizenet' );
// update_post_meta( $post_id, '_payment_method_title', 'Authorize.net' );
// } else if ($old_orders_each->orderpaymentmodule == 'checkout_paypalexpress') {
// update_post_meta( $post_id, '_payment_method', 'paypalexpress' );
// update_post_meta( $post_id, '_payment_method_title', 'PayPal Express' );
// } else if ($old_orders_each->orderpaymentmodule == 'checkout_paypalpaymentsprous') {
// update_post_meta( $post_id, '_payment_method', 'paypalpaymentsprous' );
// update_post_meta( $post_id, '_payment_method_title', 'PayPal Payment' );
// }
update_post_meta( $post_id, '_customer_ip_address', $old_orders_each->ip );
update_post_meta( $post_id, '_customer_user_agent', '' );
update_post_meta( $post_id, '_created_via', 'checkout' );
update_post_meta( $post_id, '_cart_hash', '' );
update_post_meta( $post_id, '_billing_first_name', $old_orders_each->bill_first_name );
update_post_meta( $post_id, '_billing_last_name', $old_orders_each->bill_last_name );
update_post_meta( $post_id, '_billing_address_1', $old_orders_each->bill_address.' '.$old_orders_each->bill_address2 );
update_post_meta( $post_id, '_billing_city', $old_orders_each->bill_city );
update_post_meta( $post_id, '_billing_state', $old_orders_each->bill_state );
update_post_meta( $post_id, '_billing_postcode', $old_orders_each->bill_zip );
update_post_meta( $post_id, '_billing_country', $old_orders_each->bill_country );
update_post_meta( $post_id, '_billing_email', $old_orders_each->email );
update_post_meta( $post_id, '_shipping_first_name', $old_orders_each->ship_first_name );
update_post_meta( $post_id, '_shipping_last_name', $old_orders_each->ship_last_name );
update_post_meta( $post_id, '_shipping_address_1', $old_orders_each->ship_address.' '.ship_address2 );
update_post_meta( $post_id, '_shipping_city', $old_orders_each->ship_city );
update_post_meta( $post_id, '_shipping_state', $old_orders_each->ship_state );
update_post_meta( $post_id, '_shipping_postcode', $old_orders_each->ship_zip );
update_post_meta( $post_id, '_shipping_country', $old_orders_each->ship_country );
update_post_meta( $post_id, '_order_currency', 'USD' );
update_post_meta( $post_id, '_billing_phone', $old_orders_each->phone );
update_post_meta( $post_id, '_cart_discount_tax', '0' );
// update_post_meta( $post_id, '_order_shipping', $old_orders_each->ordshipcost );
update_post_meta( $post_id, '_order_shipping_tax', $old_orders_each->tax );
//update_post_meta( $post_id, '_order_tax', $old_orders_each->ordtaxtotal );
update_post_meta( $post_id, '_order_total', $old_orders_each->total );
update_post_meta( $post_id, '_order_version', '' );
update_post_meta( $post_id, '_prices_include_tax', '' );
update_post_meta( $post_id, '_billing_address_index', $old_orders_each->bill_first_name.' '.$old_orders_each->bill_last_name.' '.$old_orders_each->bill_address.' '.$old_orders_each->bill_address2.' '.$old_orders_each->bill_city.' '.$old_orders_each->bill_state.' '.$old_orders_each->bill_zip.' '.$old_orders_each->bill_country.' '.$old_orders_each->email );
update_post_meta( $post_id, '_shipping_address_index', $old_orders_each->ship_first_name.' '.$old_orders_each->ship_last_name.' '.$old_orders_each->ship_address.' '.ship_address2.' '.$old_orders_each->ship_city.' '.$old_orders_each->ship_state.' '.$old_orders_each->ship_zip.' '.$old_orders_each->ship_country.' '.$old_orders_each->email );
update_post_meta( $post_id, 'is_vat_exempt', '' );
// if ($old_orders_each->ordstatus == 'captured') {
// update_post_meta( $post_id, '_paypal_status', 'completed' );
// }
update_post_meta( $post_id, '_transaction_id', $old_orders_each->trans_id );
$woo_pp_txnData = array();
$woo_pp_txnData['refundable_txns'][0]['txnID'] = '';
$woo_pp_txnData['refundable_txns'][0]['amount'] = '';
$woo_pp_txnData['refundable_txns'][0]['refunded_amount'] = '';
$woo_pp_txnData['refundable_txns'][0]['status'] = '';
$woo_pp_txnData['txn_type'] = 'sale';
update_post_meta( $post_id, '_woo_pp_txnData', serialize($woo_pp_txnData) );
update_post_meta( $post_id, '_recorded_sales', 'yes' );
update_post_meta( $post_id, '_recorded_coupon_usage_counts', 'yes' );
update_post_meta( $post_id, '_order_stock_reduced', 'yes' );
// update_post_meta( $post_id, 'Payer PayPal address', '' );
// update_post_meta( $post_id, 'Payer first name', '' );
// update_post_meta( $post_id, 'Payer last name', '' );
// update_post_meta( $post_id, 'Payment type', '' );
update_post_meta( $post_id, '_date_paid', $old_orders_each->ordered_on );
update_post_meta( $post_id, '_paid_date', $old_orders_each->ordered_on );
update_post_meta( $post_id, '_download_permissions_granted', '' );
//update_post_meta( $post_id, '_paypal_transaction_fee', '' );
update_post_meta( $post_id, 'old_post_id', $old_orders_each->id );
//postmeta
//Item
$orders_items = $wpdb->get_results( "SELECT * FROM `wp_cart66_order_items` WHERE `order_id` = ".$orderid."");
foreach ($orders_items as $orders_items_each) {
//$new_product_id = $wpdb->get_results("SELECT `post_id` FROM `wp_postmeta` WHERE `meta_key` = '_ci_old_productid' AND `meta_value` = '".$orderid."'", OBJECT);
$prod_id = $wpdb->get_results( "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_key` = '_ci_old_productid' AND `meta_value` ='".$orders_items_each->id."'",OBJECT);
$item_id = wc_add_order_item($post_id, array(
'order_item_name' => get_the_title( $prod_id ),
'order_item_type' => 'line_item',
));
if (!empty($item_id)) {
wc_update_order_item_meta( $item_id, '_product_id', wp_get_post_parent_id($prod_id) );
wc_update_order_item_meta( $item_id, '_variation_id', $prod_id );
wc_update_order_item_meta( $item_id, '_qty', $orders_items_each->quantity );
wc_update_order_item_meta( $item_id, '_tax_class', '' );
wc_update_order_item_meta( $item_id, '_line_subtotal', '' );
wc_update_order_item_meta( $item_id, '_line_subtotal_tax', '' );
wc_update_order_item_meta( $item_id, '_line_total', ($orders_items_each->product_price)*($orders_items_each->quantity) );
wc_update_order_item_meta( $item_id, '_line_tax', '' );
wc_update_order_item_meta( $item_id, '_line_tax_data', '' );
}
//variations
// $ordprodoptions = unserialize($orders_items_each->ordprodoptions);
// if (!empty($ordprodoptions)) {
// foreach ($ordprodoptions as $key => $value) {
// $key = 'pa_'.ord_formatUrl($key);
// wc_update_order_item_meta( $item_id, $key, $value);
// }
// }
//variations
}
//Item
//shipping method
$shipcost = (float)$old_orders_each->shipping;
if (!empty($shipcost)) {
$item_id3 = wc_add_order_item($post_id, array(
'order_item_name' => $old_orders_each->shipping_method,
'order_item_type' => 'shipping',
));
if (!empty($item_id3)) {
wc_add_order_item_meta( $item_id3, 'method_id', '' );
wc_add_order_item_meta( $item_id3, 'instance_id', '0' );
wc_add_order_item_meta( $item_id3, 'cost', $old_orders_each->shipping );
wc_add_order_item_meta( $item_id3, 'total_tax', '0' );
wc_add_order_item_meta( $item_id3, 'taxes', array() );
}
}
//shipping method
//coupon
$coup = $old_orders_each->coupon;
if ($coup != 'none') {
$item_id4 = $wpdb->insert(
'wp_woocommerce_order_items',
array(
'order_item_name' => 'Coupon',
'order_item_type' => 'coupon',
'order_id' => $post_id
)
);
wc_add_order_item_meta( $item_id4, 'discount_amount', $old_orders_each->coupon );
wc_add_order_item_meta( $item_id4, 'discount_amount_tax', '' );
wc_add_order_item_meta( $item_id4, 'coupon_data', '' );
}
//coupon
$i++;
}
}
//
return ob_get_clean();
}
add_shortcode('custom_import_order_shortcode2', 'custom_import_order_shortcode_fun2');
add_shortcode('update_user_id','update_user_id_func');
function update_user_id_func() {
global $wpdb;
$old_users = $wpdb->get_results( "SELECT * FROM `wp_users` LEFT JOIN `customers` ON wp_users.user_email = customers.custconemail", OBJECT );
foreach($old_users as $old_users_each) {
update_user_meta($old_users_each->ID,'_ci_user_old',$old_users_each->customerid);
}
}
function register_shipment_arrival_order_status() {
register_post_status( 'wc-comp-phone-order', array(
'label' => 'Completed Phone Order',
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop( 'Completed Phone Order <span class="count">(%s)</span>', 'Completed Phone Order <span class="count">(%s)</span>' )
) );
register_post_status( 'wc-comp-web-order', array(
'label' => 'Completed Web Order',
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop( 'Completed Web Order <span class="count">(%s)</span>', 'Completed Web Order <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_shipment_arrival_order_status' );
function add_awaiting_shipment_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-comp-phone-order'] = 'Completed Phone Order';
$new_order_statuses['wc-comp-web-order'] = 'Completed Web Order';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );
//url formate
function ord_formatUrl($str, $sep='-'){
$res = strtolower($str);
$res = preg_replace('/[^[:alnum:]]/', ' ', $res);
$res = preg_replace('/[[:space:]]+/', $sep, $res);
return trim($res, $sep);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment