Created
August 10, 2020 09:31
-
-
Save fencermonir/2bc72a741926fe2ff915c42084c276d4 to your computer and use it in GitHub Desktop.
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
/** | |
* Helper function: it maps the original product ID to the new product ID. | |
* @param string $originId | |
* @return int | |
*/ | |
function getProductIdOld($originId) | |
{ | |
$ids = [ | |
'OOD012' => 4225, | |
'OOD011' => 4001, | |
'ODF001' => 4003, | |
'ODF002' => 4083, | |
'ODF003' => 4226, | |
'OOG001' => 4011, | |
'OOG002' => 4012, | |
'OOG003' => 4013, | |
'OOG004' => 4014, | |
'OOG005' => 4015, | |
'OOG006' => 4016, | |
'OOG007' => 4018, | |
'OOG008' => 4019, | |
'OOG009' => 4020, | |
'OOG010' => 4021, | |
'OOG011' => 4022, | |
'OOG013' => 4256, | |
'OOG014' => 4258, | |
'OOG015' => 4260, | |
'OOG016' => 4024, | |
'OOG017' => 4025, | |
'OOG018' => 4026, | |
'OOG019' => 4027, | |
'OOG020' => 15608, | |
'OOV002' => 4160, | |
'OOV001' => 4066, | |
'OAH001' => 4028, | |
'OAH002' => 4029, | |
'OAH003' => 4030, | |
'OAH004' => 4031, | |
'OAH006' => 4033, | |
'OAH007' => 4234, | |
'OAH009' => 4681, | |
'OON001' => 4035, | |
'OON002' => 4038, | |
'OON003' => 4036, | |
'OON004' => 4039, | |
'OON005' => 4040, | |
'OON006' => 4037, | |
'OON007' => 4655, | |
'OON008' => 10759, | |
'OOE001' => 4044, | |
'OOE002' => 4041, | |
'OOE003' => 4042, | |
'OOE004' => 4045, | |
'OOE005' => 4043, | |
'OOE006' => 7210, | |
'OOE007' => 7254, | |
'OOE008' => 7273, | |
'OOE009' => 12385, | |
'OOE010' => 7022, | |
'OOE11' => 4391, | |
'OOE012' => 4383, | |
'OOE013' => 4387, | |
'OOE014' => 4389, | |
'OOE016' => 4571, | |
'OOE017' => 4662, | |
'OOE018' => 7024, | |
'OOE019' => 7026, | |
'HN001' => 7063, | |
'HN002' => 7065, | |
'HN003' => 7067, | |
'HN004' => 7069, | |
'OOS001' => 4046, | |
'OOS002' => 4047, | |
'OOS003' => 4223, | |
'OOS004' => 4049, | |
'OOS005' => 4050, | |
'OOS006' => 4051, | |
'OOS008' => 4053, | |
'OOS009' => 4054, | |
'OOS011' => 4056, | |
'OOS012' => 4057, | |
'OOS013' => 4058, | |
'OOS014' => 4059, | |
'OOS015' => 4060, | |
'OOS016' => 4061, | |
'OOS017' => 4062, | |
'OOS018' => 4063, | |
'SPP001' => 4064, | |
'SPP002' => 4679, | |
'SPP003' => 4677 | |
]; | |
if (array_key_exists($originId, $ids)) { | |
return $ids[$originId]; | |
} | |
return false; | |
} | |
/** | |
* Upload order old data | |
*/ | |
function old_order_data_form($atts, $content = null) | |
{ | |
ob_start(); | |
if (isset($_POST['old_order_form'])) { | |
$file_name = $_FILES["order_data"]["tmp_name"]; | |
$assoc_array = $keys = array(); | |
if ($_FILES["order_data"]["size"] > 0) { | |
$file = fopen($file_name, "r"); | |
$row = 1; | |
while (($data = fgetcsv($file, 0, ",")) !== false) { | |
if ($row !== 1) { | |
if (array_filter($data)) { | |
// $assoc_array[] = $data; | |
list( | |
$createdAt, $full_name, $phone, $address, $area, $product, $volume | |
) = $data; | |
if (empty($product) || empty($phone) || empty($full_name)) { | |
continue; | |
} | |
// Prepare the customer address for the order. | |
// echo 'sheet-' . $createdAt; | |
// echo '<br>'; | |
// echo 'strtotime: ' . strtotime($createdAt) . '<br>'; | |
// $createdAt = date("Y-m-d H:i:s", strtotime($createdAt)); | |
// echo $createdAt; | |
// echo '<br>'; | |
$customerAddress = [ | |
'first_name' => "", | |
'last_name' => $full_name, | |
'phone' => $phone, | |
'address_1' => $address, | |
'city' => $area, | |
'state' => $area, | |
]; | |
$productId = getProductIdOld($product); | |
if ($productId) { | |
// Fetch the product from the database. | |
$product = wc_get_product($productId); | |
if ($product) { | |
$order = wc_create_order(['status' => 'completed', 'customer_id' => 0]); | |
$order->set_address($customerAddress, 'billing'); | |
$order->set_address($customerAddress, 'shipping'); | |
// Add the product to the order. | |
// In this case, we only add a single unit of each product. | |
$order->add_product($product, $volume); | |
// Persist the order. | |
$order->calculate_totals(); | |
// Confirm the order payment and add a note to it. | |
$order->add_order_note('Order automatically imported from legacy system.'); | |
$order->payment_complete(); | |
$order->update_meta_data('old_order_data', 'yes'); | |
$order->save(); | |
// Update the order date to the original order date. | |
// This is useful to not screw with the order statistics. | |
$createdAt = date("Y-m-d H:i:s", strtotime($createdAt)); | |
// echo $createdAt; | |
// echo '<br>'; | |
wp_update_post([ | |
'ID' => $order->get_id(), | |
'post_date' => $createdAt, | |
'post_date_gmt' => $createdAt, | |
'post_modified' => $createdAt, | |
'post_modified_gmt' => $createdAt | |
], true); | |
} | |
} | |
} | |
} | |
$row = $row + 1; | |
} | |
fclose($file); | |
} | |
echo '<pre>'; | |
echo 'Row Count: ' . $row; | |
echo '</pre>'; | |
} | |
?> | |
<form action="" method="post" enctype="multipart/form-data"> | |
<input type="file" name="order_data"> | |
<input type="submit" name="old_order_form" value="Submit"> | |
</form> | |
<?php | |
return ob_get_clean(); | |
} | |
add_shortcode('old_order_data', 'old_order_data_form'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment