Function to copy a WooCommerce order

Function to copy / duplicate a WooCommerce order. Useful for writing a custom code snippet that requires this function.

function ccom_duplicate_order( $original_order_id ) {
	
	// Load Original Order
	$original_order = wc_get_order( $original_order_id );
	$user_id = $original_order->get_user_id();

	// Setup Cart
	WC()->frontend_includes();
	WC()->session = new WC_Session_Handler();
	WC()->session->init();
	WC()->customer = new WC_Customer( $user_id, true );
	WC()->cart = new WC_Cart();

	// Setup New Order
	$checkout = WC()->checkout();
	WC()->cart->calculate_totals();
	$order_id = $checkout->create_order( [ ] );
	$order = wc_get_order( $order_id );
	update_post_meta( $order_id, '_customer_user', get_current_user_id() );

	// Header
	update_post_meta( $order_id, '_order_shipping',         get_post_meta($original_order_id, '_order_shipping', true) );
	update_post_meta( $order_id, '_order_discount',         get_post_meta($original_order_id, '_order_discount', true) );
	update_post_meta( $order_id, '_cart_discount',          get_post_meta($original_order_id, '_cart_discount', true) );
	update_post_meta( $order_id, '_order_tax',              get_post_meta($original_order_id, '_order_tax', true) );
	update_post_meta( $order_id, '_order_shipping_tax',     get_post_meta($original_order_id, '_order_shipping_tax', true) );
	update_post_meta( $order_id, '_order_total',            get_post_meta($original_order_id, '_order_total', true) );
	update_post_meta( $order_id, '_order_key',              'wc_' . apply_filters('woocommerce_generate_order_key', uniqid( 'order_' ) ) );
	update_post_meta( $order_id, '_customer_user',          get_post_meta($original_order_id, '_customer_user', true) );
	update_post_meta( $order_id, '_order_currency',         get_post_meta($original_order_id, '_order_currency', true) );
	update_post_meta( $order_id, '_prices_include_tax',     get_post_meta($original_order_id, '_prices_include_tax', true) );
	update_post_meta( $order_id, '_customer_ip_address',    get_post_meta($original_order_id, '_customer_ip_address', true) );
	update_post_meta( $order_id, '_customer_user_agent',    get_post_meta($original_order_id, '_customer_user_agent', true) );

	// Billing
	update_post_meta( $order_id, '_billing_city',           get_post_meta($original_order_id, '_billing_city', true));
	update_post_meta( $order_id, '_billing_state',          get_post_meta($original_order_id, '_billing_state', true));
	update_post_meta( $order_id, '_billing_postcode',       get_post_meta($original_order_id, '_billing_postcode', true));
	update_post_meta( $order_id, '_billing_email',          get_post_meta($original_order_id, '_billing_email', true));
	update_post_meta( $order_id, '_billing_phone',          get_post_meta($original_order_id, '_billing_phone', true));
	update_post_meta( $order_id, '_billing_address_1',      get_post_meta($original_order_id, '_billing_address_1', true));
	update_post_meta( $order_id, '_billing_address_2',      get_post_meta($original_order_id, '_billing_address_2', true));
	update_post_meta( $order_id, '_billing_country',        get_post_meta($original_order_id, '_billing_country', true));
	update_post_meta( $order_id, '_billing_first_name',     get_post_meta($original_order_id, '_billing_first_name', true));
	update_post_meta( $order_id, '_billing_last_name',      get_post_meta($original_order_id, '_billing_last_name', true));
	update_post_meta( $order_id, '_billing_company',        get_post_meta($original_order_id, '_billing_company', true));

	// Shipping
	update_post_meta( $order_id, '_shipping_country',       get_post_meta($original_order_id, '_shipping_country', true));
	update_post_meta( $order_id, '_shipping_first_name',    get_post_meta($original_order_id, '_shipping_first_name', true));
	update_post_meta( $order_id, '_shipping_last_name',     get_post_meta($original_order_id, '_shipping_last_name', true));
	update_post_meta( $order_id, '_shipping_company',       get_post_meta($original_order_id, '_shipping_company', true));
	update_post_meta( $order_id, '_shipping_address_1',     get_post_meta($original_order_id, '_shipping_address_1', true));
	update_post_meta( $order_id, '_shipping_address_2',     get_post_meta($original_order_id, '_shipping_address_2', true));
	update_post_meta( $order_id, '_shipping_city',          get_post_meta($original_order_id, '_shipping_city', true));
	update_post_meta( $order_id, '_shipping_state',         get_post_meta($original_order_id, '_shipping_state', true));
	update_post_meta( $order_id, '_shipping_postcode',      get_post_meta($original_order_id, '_shipping_postcode', true));

	// Shipping Items
	$original_order_shipping_items = $original_order->get_items( 'shipping' );
	foreach ( $original_order_shipping_items as $original_order_shipping_item ) {
		$item_id = wc_add_order_item( $order_id, array(
			'order_item_name' => $original_order_shipping_item['name'],
			'order_item_type' => 'shipping'
		) );
		if ( $item_id ) {
			wc_add_order_item_meta( $item_id, 'method_id', $original_order_shipping_item['method_id'] );
			wc_add_order_item_meta( $item_id, 'cost', wc_format_decimal( $original_order_shipping_item['cost'] ) );
		}
	}

	// Coupons
	$original_order_coupons = $original_order->get_items( 'coupon' );
	foreach ( $original_order_coupons as $original_order_coupon ) {
		$item_id = wc_add_order_item( $order_id, array(
			'order_item_name' => $original_order_coupon['name'],
			'order_item_type' => 'coupon'
		) );
		if ( $item_id ) {
			wc_add_order_item_meta( $item_id, 'discount_amount', $original_order_coupon['discount_amount'] );
		}
	}

	// Payment
	update_post_meta( $order_id, '_payment_method', get_post_meta( $original_order_id, '_payment_method', true ) );
	update_post_meta( $order_id, '_payment_method_title', get_post_meta( $original_order_id, '_payment_method_title', true ) );
	update_post_meta( $order_id, 'Transaction ID', get_post_meta( $original_order_id, 'Transaction ID', true ) );

	// Line Items
	foreach( $original_order->get_items() as $originalOrderItem ) {
		$itemName = $originalOrderItem['name'];
		$qty = $originalOrderItem['qty'];
		$lineTotal = $originalOrderItem['line_total'];
		$lineTax = $originalOrderItem['line_tax'];
		$productID = $originalOrderItem['product_id'];
		$item_id = wc_add_order_item( $order_id, array(
				'order_item_name' => $itemName,
				'order_item_type' => 'line_item'
		) );
		wc_add_order_item_meta( $item_id, '_qty', $qty );
		wc_add_order_item_meta( $item_id, '_tax_class', $originalOrderItem['tax_class'] );
		wc_add_order_item_meta( $item_id, '_product_id', $productID );
		wc_add_order_item_meta( $item_id, '_variation_id', $originalOrderItem['variation_id'] );
		wc_add_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $lineTotal ) );
		wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $lineTotal ) );
		wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $lineTax ) );
		wc_add_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $originalOrderItem['line_subtotal_tax'] ) );
	}

	// Close New Order
	$order->calculate_totals();
	$order->payment_complete();

	// Note
	$order->update_status( 'processing' );
	$message = sprintf(
		'This order was duplicated from order %d.',
		$original_order_id
	);
	$order->add_order_note( $message );

	// Return
	return $order_id;

}

How to use

PHP Code Snippets

  1. Log into a staging, development, or locally hosted clone of your site
  2. Install and activate Code Snippets
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the Description tab above
  5. Check to ensure formatting came over properly and no syntax errors show up in the editor
  6. Customize the code as desired
  7. Add a meaningful title
  8. Select whether to run on front-end or back-end (or both)
  9. Click “Save and Activate”
  10. Test your site to ensure it works
  11. Disable if any problems, or recover
  12. Repeat for live environment

License

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Disclaimer of warranty:

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.

Support

  1. Describe the issue and what you’ve observed.
  2. Describe your expected outcome(s).
  3. List steps to reproduce the issue.
  4. Optionally provide screen-shot or video URLs.