Title: wc_get_order on function.php gerenate error
Last modified: August 30, 2016

---

# wc_get_order on function.php gerenate error

 *  [alfaex](https://wordpress.org/support/users/alfaex/)
 * (@alfaex)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/wc_get_order-on-functionphp-gerenate-error/)
 * i’m using “woocommerce_payment_complete” to insert some product data in a DB.
   
   i need to get the id of the product that the client bought. i already search 
   a lot of thing and everyone said to use this.
 *     ```
       "$order = wc_get_order(11975);
       $items = $order->get_items();"
       ```
   
 * It work on template files like ‘thankyou’ etc.
    but i need to use this in function.
   php and i get this error.
 * “Call to a member function get_order() on a non-object”
 * i can’t figure out what i need to call inside the “woocommerce_payment_complete”
   action to solve this.
 * Any hints?
 * If i try use this
 *     ```
       $order = new WC_Order(11266);
           $items = $order->get_items();
   
           foreach ( $items as $item ) {
               // echo $item['product_id'] . "<br>";
               var_dump($item['product_id']);
           }
       ```
   
 * the result is NULL
 * wp 4.3.1
    woo 2.4.10
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/wc_get_order-on-functionphp-gerenate-error/#post-6745985)
 * Before getting items, check if $order is an object or FALSE. Looks like it does
   not exist.
 *  Thread Starter [alfaex](https://wordpress.org/support/users/alfaex/)
 * (@alfaex)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/wc_get_order-on-functionphp-gerenate-error/#post-6746067)
 * i get the error when call wc_get_order(), not even run the get_items.
 * i realize that if i use wc_get_order inside the “woocommerce_payment_complete”
   works.
    something like that.
 *     ```
       add_action("woocommerce_payment_complete", "woodle_func",10);
       function woodle_func($orderId){
           $order = wc_get_order($orderId);
       }
       ```
   
 *     ```
       but i will use in some custom action.
       add_action("my_custom_action", "my_custom_func");
       function "my_custom_func($orderId){
           $order = wc_get_order($orderId);
       }
       ```
   
 * i get the error on the `$order = wc_get_order($orderId);`
 * thanks for the reply

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wc_get_order on function.php gerenate error’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [alfaex](https://wordpress.org/support/users/alfaex/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/wc_get_order-on-functionphp-gerenate-error/#post-6746067)
 * Status: not resolved