Woocommerce – Copy New Order Details to New Table

I found the below script somewhere on here before and having trouble getting it to work. Basically I want to copy basic order details into a new table (which is in the same database), so I can run some reports off of it from non wordpress page. I have changed the table name and fields and copied this into my functions.php in the theme. But it doesnt seem to do anything. Any help would be great, im really stuck. I’m running woocommerce 2.2.8 if that makes any difference.

function add_neworders ($order_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
$total = $order->order_total;
$date = $order->order_date;

global $wpdb;
$table =  $wpdb->prefix . 'finacedata';

$wpdb->insert($table, array( 

'desctipion' => $name, 
'status' => '2',
'date' => $date,
'amount' => $total,
'invoice' => $order_id,
));

add_action( 'woocommerce_new_order', 'add_neworders' );}

Related posts

Leave a Reply

2 comments

  1. This is probably a late answer, but the woocommerce_new_order action runs when the order is created, but the data is not populated just yet, so everything is empty.

    Use the woocommerce_checkout_order_processed action instead, which runs when the order is created and all of the order data(like line items) are stored.

  2. you can’t add add_action into function. put outside the function:

    function add_neworders ($order_id) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
        $total = $order->order_total;
        $date = $order->order_date;
    
        global $wpdb;
        $table =  $wpdb->prefix . 'finacedata';
    
        $wpdb->insert($table, array(
    
        'desctipion' => $name,
        'status' => '2',
        'date' => $date,
        'amount' => $total,
        'invoice' => $order_id,
        ));
    }
    add_action( 'woocommerce_new_order', 'add_neworders' );