0

whenever I use WC_Order() in a plugin that I created for getting the order data I get Fatal error: Uncaught Error: Class "WC_Order" not found

What I want to achieve is getting the order data like shipping address, product name, product description and client name to make another API call that associate with the Woocommerce order

this's the simple popular code

global $woocommerce, $post;
$order =  new WC_Order(1413);

$order_data = $order->get_data(); // The Order data

the error

Fatal error: Uncaught Error: Class "WC_Order" not found in C:\xampp\htdocs\wordpress\wp-content\plugins\Scalablepress\index.php
Steve
  • 43
  • 6
  • We need more information, need to know when you're running this code because you might be calling your code before WooCommerce initializes on your website. – Vijay Hardaha Sep 15 '22 at 20:12
  • I run it in a plugin in the `index.php` but later it will be associated with the order complete hook should I make the code with another function or what should I do to make the priority after wc files. – Steve Sep 15 '22 at 20:20
  • 1
    wrap your code into a function then hook your function with `init` action using `add_action()` function. – Vijay Hardaha Sep 15 '22 at 20:27
  • Yes it works. this way, you can make an answer and just to make the question solved – Steve Sep 15 '22 at 21:33

1 Answers1

0

Try to use this:

$order = wc_get_order( $order_id );

With that, you can access all other order data like this:

$order_id  = $order->get_id();
$parent_id = $order->get_parent_id();
$user_id   = $order->get_user_id();
$user      = $order->get_user();

The question has been answered something like this before: How to get WooCommerce order details

SyNoXaT
  • 55
  • 1
  • 5