Skip to content
Advertisement

Can not get the product details from order_id on the new order hook function

With WooCommerce, I have the following hook in my function.php after the new order is submitted:

JavaScript

The above code is not giving me any output i’e it is not entering inside the foreach loop that’s why var_dump() not giving me any output, but if I mention the order_id specifically like create_job_openings($order_id=517) it works, even I tried echo $order_id before foreach loop, it is giving me the order_id, then why it is not entering the foreach loop?

note: when I try var_dump($items); before foreach loop its giving me

JavaScript

Why it is not able to get the product details even if there are products in it after new order is made?

Advertisement

Answer

Update 2 — The working solution (using an email notification hook)

The problem is when using email notification hook can fire an action 2 times, for example when a new order is made (notification for the Shop manager and notification for the customer).

You want to use this “New Order” event for Orders that are in “processing” status.

To avoid your action to be fired 2 times using New order notification WooCommerce event, we use 'customer_processing_order' instead of 'new_order' email ID (notification event).

Here we don’t need to get the $order object, as we got it as an argument in this hooked function.

So here is your final functional code:

JavaScript

This is the validated and working answer to this question

Related Working Answers:


Update 1 (hook alternative)

Trying using woocommerce_thankyou hook, that is fired on review order after order has been processed:

JavaScript

(Not working for the OP)


You should try this instead wc_get_order() function this way and your code will be:

JavaScript

You can have a look to How to get WooCommerce order details where a lot of things are explained…

(Not working for the OP)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement