Preface:

The Fields Factory module is used:

When choosing coffee, you can add sugar to it. When an order is completed, this additive is listed in the meta order.

Question: how to get these meta in the construction below?

Perhaps you know the solutions on how to register these supplements so that you can work with the API later? Ideally, I want to ensure that these supplements get your article.

This code is written in functions.php to collect information about the order when it is made.

 $items = $order->get_items(); $sku = array(); $product_qty = array(); $product_price = array(); foreach( $items as $key => $item){ $product_qty[] = $item['qty']; $product_price[] = $item['line_total']; $item_id = $item['product_id']; $product_item = wc_get_product($item_id); $sku[] = $product_item->get_sku(); } 
  • What a way - to remove the previous questions? You are already asking the third question about the same topic, and considering English SO, already the fourth. Here the knowledge base, questions should be saved. - KAGG Design
  • I apologize, the fact is that it seemed that the question is not too clear. Yes, and reacted to him like "what's there to understand?". On a foreign site and told to delete. And at all without having estimated a question, minus and mark as the duplicate. But the year 2018 and many functions are outdated, they are not used, and they cause an error. And refer to the old answers, and where at all not php, but json. Therefore, I made a small question that will be useful to many. As from modules, custom fields can be pulled out only from the meta data of the order. - Alexandra Kuznetsova

2 answers 2

Order is a post. You can get the metadata of any post using get_post_meta() . In this case:

 $order_meta = get_post_meta( $order->get_id() ); 
  • Thank you for the answer, but the question still concerns a certain design. And so we get the whole array. And it turns out the problem is that there is no data about the module in them, but when placing an order it shows the additives on the page. - Alexandra Kuznetsova
  • Although it makes no sense to enter there, as this is general information. And in it, the goods are broken, respectively, and the additives relative to the goods. Perhaps here you need to look for other solutions and register the API of custom fields and their values. What is the best way to do in this situation, ask another question or change this? - Alexandra Kuznetsova
  • Add details to the question, because no one knows what meta is there for you. But in general, I consider the answer to be exhaustive, because you need to select the necessary elements from the array - well, you somehow have to do it yourself. Look at the contents of this array, and select from it what you need. - KAGG Design
  • On the one hand, yes, but in this case, in the context of the required additives, it turns out that they are not there at all and cannot be. Now I think how to correctly ask a question. They are derived by some functions and it seems that they need to be registered or caught. Do you have any thoughts on this? - Alexandra Kuznetsova
  • I know nothing about your supplements. If there are none, then there are no such elements in the array. Build code based on this. Or create a minimal, self-contained and reproducible example: ru.stackoverflow.com/help/mcve - KAGG Design

This code displays meta product data. Custom additions to the product are not stored in the order meta.

 $order = wc_get_order( $order_id ); // Цикл WC_Order_Item_Product, чтобы получить элементы заказа для версии WC 3+ foreach( $order->get_items() as $item_id => $item_product ){ // Получить общие данные в массиве: $item_product_data_array = $item_product->get_data(); // Получить специальные метаданные в массиве: $item_product_meta_data_array = $item_product->get_meta_data(); // Получать только дополнительные метаданные (отформатированные в незащищенном массиве) $formatted_meta_data = $item->get_formatted_meta_data(); }