I researched a little deeper and found that I was very close to solving the problem. I found a very interesting comment that showed me that what was missing was to define global $ wpdb before. And also use the foreach to extract the private item (I did not fully understand this, but it worked). And then it looked like this:
// Take the info order
// Pega informações do pedido
global $wpdb;
$ws_order = new WC_Order( wc_get_order_id_by_order_key($_GET['key']) );
$ws_items = $ws_order->get_items();
// This foreach retur a string of data order: $item_id {"string items": value}
// Este foreach serve para pegar o dados do peido no formato string
foreach ($ws_items as $key => $product ) {
$ws_data_items = $key . $product;
}
You can test to see the output: var_dump($ws_data_items);
In my case, the output was as follows: string (222) "13 {" id ": 13," order_id ": 117," name ":" Monthly " "product_id": 116 , "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "29.9", "subtotal_tax": "0" "Total_tax": "0", "taxes": {"total": [], "subtotal": []}, "meta_data": [
So far you already have the order data, and with them you can do whatever you want.
But ... for my answer to be complete, I'll show you what I needed to do to get the item category from the request, see:
OBS: Surely it should have an easier way of doing this, but it seems to me that there is this information in our great friend Google, much less in the Woocommerce documentation. So ... this is what you have for today ... hehe
First, note that the result is a string with key and value (I also did not understand why it is a string and not an array). This is not at all cool, because you will have to clean up the useless data; I just want the id of the product that (I mean the id of the post_type = product), specifically in this case is 116 .
Let's use the str_replace
and strstr()
functions of PHP. If I explain how each one works, it will be too long, so check out their explanation at php.net [str_replace and strstr :
$ws_only_id_item = str_replace(array(':'), '', strstr(strstr(strstr($ws_data_items, 'product_id'), ',', true), ':'));
After that, I have the number 116 stored in $ws_only_id_item
.
With this information just get the category with WP_Term class as follows.
// isso pega o nome da cateria
// Note que se você tiver mais de uma categoria no produto, é preciso fazer um foreach
// No meu caso, sempre terá apenas uma categoria então eu decidi usar [0]
$ws_array_terms = get_the_terms ( $ws_only_id_item, 'product_cat' );
$ws_obj_cat = $ws_array_terms[0];
echo $ws_obj_cat->name;