Wordpress and Woocommerce - Wc_order

Asked

Viewed 176 times

0


I need help with a project.
I have developed a completely custom site in Wordpress. Total pages are standardized, but the user can only access the pages if they have active product. So I need to configure his environment at the time of purchase, ie at woocommerce_is_order_received_page.

Well... I realized that on the successful payment page, woocommerce_is_order_received_page, has a request key on $_GET['key'], and by means of this data I can take part all data.

<?php $data_order = wc_get_order(wc_get_order_id_by_order_key($_GET['key'])); ?>

Here’s the thing... the data I need is encapsulated within private methods and I can’t use it, just visualize it. How do I pick up the purchased item as well as other private data?

inserir a descrição da imagem aqui

1 answer

1

I looked 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 the global $wpdb before. And also use the foreach to extract the private item (I didn’t fully understand this, but it worked). And then it got 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 take a test to see the output: var_dump($ws_data_items); In my case, the exit 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":"29.9","total_tax":"0","Taxes":{"total":[],"subtotal":[]},"meta_data"":[]}"

So far you have already given the order, and with them you can do whatever you want. But... for my answer to be complete, I’ll show you what I had to do to get the item category of the request, see:
NOTE: surely there must be an easier way to do this, but it seems to me that there is this information in our great friend Google, much less in the Woocommerce documentation. So... that’s what’s for today... hehe
First of all, note that the result is a string with key and value (I also didn’t understand why it’s a string and not an array). This is not at all cool, because it will be necessary to clean up the useless data; I just want the product id that (I refer to the post_type id=product), specifically in this case is 116.

Let’s use the functions str_replace and strstr() PHP. If I explain how each of them works, it will get very long, so see their explanation in 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.