Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Get order items on checkout

I am trying to hook into a 3rd party application when a cart is checked out. Essentially i need to pass information about the order such as the products that are inside the order.

Everything i find points me towards the hook: woocommerce_new_order

When i use that hook i can get some information about the order but not everything.

add_action('woocommerce_new_order','order_check',10,1);

function order_check($order_id){
    echo 'Order id is: '.$order_id;
    $order = new WC_Order($order_id);
    print_r($order);

    echo '-----';


    /** CHECK IF order has items */
    $order_item = $order->get_items();
    print_r($order_item);

    exit;
}

For instance the above code sample will print the order array but when it call $order->get_items() nothing is returned.

like image 289
AndrewMac Avatar asked Feb 22 '16 20:02

AndrewMac


1 Answers

At the time woocommerce_new_order fires, order items is not yet populated.

Instead, use the hook woocommerce_checkout_order_processed and you'll find that all items are then populated.

like image 58
sean2078 Avatar answered Sep 27 '22 19:09

sean2078