File: /var/www/html/breadsecret.com/test_clone.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('wp-load.php');
session_start();
date_default_timezone_set("Asia/Hong_Kong");
function clone_woocommerce_order($order_id) {
// Get the original order
$original_order = wc_get_order($order_id);
if (!$original_order) {
return; // Order not found
}
// Create a new order
$new_order = wc_create_order();
$new_order->set_customer_id($original_order->get_customer_id());
// Copy billing and shipping data
$new_order->set_billing_first_name($original_order->get_billing_first_name());
$new_order->set_billing_last_name($original_order->get_billing_last_name());
$new_order->set_billing_address_1($original_order->get_billing_address_1());
$new_order->set_billing_address_2($original_order->get_billing_address_2());
$new_order->set_billing_city($original_order->get_billing_city());
$new_order->set_billing_postcode($original_order->get_billing_postcode());
$new_order->set_billing_country($original_order->get_billing_country());
$new_order->set_billing_email($original_order->get_billing_email());
$new_order->set_billing_phone($original_order->get_billing_phone());
$new_order->set_shipping_first_name($original_order->get_shipping_first_name());
$new_order->set_shipping_last_name($original_order->get_shipping_last_name());
$new_order->set_shipping_address_1($original_order->get_shipping_address_1());
$new_order->set_shipping_address_2($original_order->get_shipping_address_2());
$new_order->set_shipping_city($original_order->get_shipping_city());
$new_order->set_shipping_postcode($original_order->get_shipping_postcode());
$new_order->set_shipping_country($original_order->get_shipping_country());
$new_order->update_meta_data( '_shipping_email', $original_order->get_meta( '_shipping_email' ) );
// Copy order items
foreach ($original_order->get_items() as $item_id => $item) {
$new_order->add_product(wc_get_product($item->get_product_id()), $item->get_quantity(), array(
'subtotal' => $item->get_subtotal(),
'total' => $item->get_total(),
));
}
// Copy shipping methods
$shipping_methods = $original_order->get_shipping_methods();
foreach ($shipping_methods as $shipping_item) {
// Create a new shipping item
$shipping_item_new = new WC_Order_Item_Shipping();
$shipping_item_new->set_method_id($shipping_item->get_method_id());
$shipping_item_new->set_method_title($shipping_item->get_method_title());
$shipping_item_new->set_total($shipping_item->get_total());
//$shipping_item_new->set_total_tax($shipping_item->get_total_tax());
$shipping_item_new->calculate_taxes($calculate_tax_for);
// Add the shipping item to the new order
$new_order->add_item($shipping_item_new);
}
// Copy applied coupons
$coupons = $original_order->get_used_coupons();
foreach ($coupons as $coupon_code) {
$new_order->apply_coupon($coupon_code);
}
// Set new order status
$new_order->set_status('pending'); // or use 'processing', etc.
// Calculate totals
$new_order->calculate_totals();
// Save the new order
$new_order->save();
$new_order_id = $new_order->get_id();
$delivery_date = $_GET['delivery_date'];
$new_order->add_order_note("Delivery Date: ".$delivery_date);
$new_order->add_order_note("This order was duplicated from order# ".$original_order->get_order_number());
update_post_meta($new_order_id, 'delivery_date',$_GET['delivery_date']);
if($_GET['adjust_stock']=="Y"){
adjust_stock_action($new_order_id); // only run if order status = processing
}
update_post_meta($new_order_id, 'adjust_stock',$_GET['adjust_stock']);
$display_delivery_date = date("d/m/Y", strtotime($delivery_date));
if($delivery_date!=date("Y-m-d")){
$title = "NEW ORDER";
}else{
$title = "\u{2B50}TODAY";
}
$message = "#".$new_order->get_order_number()." ".$display_delivery_date;
$topic = "newOrder";
initPushNotification($title, $message, $topic, strval($new_order_id));
update_post_meta($new_order_id, 'new_order_notification', '1');
return $new_order_id; // Return the new order ID
}
// Example usage
echo $new_order_id = clone_woocommerce_order(217900); // Replace 123 with the original order ID
?>