久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Woocommerce 3 中訪問受訂單項保護的數據

Accessing Order Items protected data in Woocommerce 3(在 Woocommerce 3 中訪問受訂單項保護的數據)
本文介紹了在 Woocommerce 3 中訪問受訂單項保護的數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試獲取訂單的行項目.

我正在這樣做:

$order = new WC_Order(147);foreach ($order->get_items() as $key => $lineItem) {print_r('<pre>----');打印_r($lineItem);print_r('----</pre>');}

我可以看到我需要的所有數據,但數組顯示:

[meta_data:protected] =>大批

如何訪問該數組以獲取值?

謝謝.

解決方案

從 WooCommerce 3.0+ 開始,訂單項目有了新的對象類 WC_Order_Item_Product.
現在不能像以前一樣直接訪問訂單項屬性

因此,如果您查看輸出的原始數據,您會發現每個行項目現在都是一個對象,并且您將能夠以獨占方式訪問受保護的數據:

  1. WC_Order_Item_Product getters 方法(或者用 setters 方法改變它)...
  2. WC_Order_Item get_formatted_meta_data( '', true ) 方法來訪問所有元數據.它提供了一組可訪問的對象.請參閱WC_Data 方法 get_meta() 訪問每個元數據.
  3. WC_Data getters 方法來取消保護此數據并使用以下方法通過數組訪問它:
    • get_data() (這個方法非常有用)
    • get_meta() (這個方法最有用)
    • get_data_keys()
    • get_meta_data() (不會解除數據保護,使用get_formatted_meta_data())
  4. wc_get_order_item_meta() 專用功能.

WC_Order_Item_Product getter 方法:

//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {echo $item->get_type().'
';//訂單項類型echo $item->get_product_id().'
';//產品 IDecho $item->get_variation_id().'
';//變體IDecho $item->get_quantity().'
';//訂單項數量echo $item->get_subtotal().'
';//行項目小計echo $item->get_total().'
';//行項目總計//關聯的產品對象(也不能直接訪問哪些屬性)echo '

';print_r( $item->get_product() );echo '</pre>';//... 等等 ...## 測試原始輸出(受保護)//echo '

';print_r($item);echo '</pre>';}

wc_get_order_item_meta() 功能.在這里,您可以進入wp_woocommerce_order_itemmeta 表并使用相應的meta_key(對于line_item 數據類型項 ID):

//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {echo wc_get_order_item_meta( $item_id, '_product_id', true).'
';//產品編號echo wc_get_order_item_meta( $item_id, '_variation_id', true).'
';//變量 IDecho wc_get_order_item_meta( $item_id, '_qty', true).'
';//數量echo wc_get_order_item_meta( $item_id, '_line_subtotal', true).'
';//行小計//... 等等 ...## 測試原始輸出(受保護的數據)//echo '

';print_r($item);echo '</pre>';}

WC_Data 方法 get_data() 方法(取消對數組中數據的保護):

//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {//在一個可訪問的數組中獲取最有用的 Item 產品數據$item_data = $item->get_data();echo $item_data['id'].'
';//訂單商品IDecho $item_data['order_id'].'
';//訂單編號echo $item_data['product_id'].'
';//產品 IDecho $item_data['variation_id'].'
';//變量 IDecho $item_data['name'].'
';//產品標題(名稱)echo $item_data['quantity'].'
';//訂單項數量echo $item_data['subtotal'].'
';//行項目小計echo $item_data['total'].'
';//行項目總計//... 等等 ...

WC_Data 方法 get_meta() 方法(通過元鍵訪問每個屬性):

//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {echo $item->get_meta('_product_id').'<br>';//產品 IDecho $item->get_meta('_variation_id').'
';//變量 IDecho $item->get_meta('_qty').'
';//訂單項數量echo $item->get_meta('_line_subtotal').'<br>';//行項目小計echo $item->get_meta('_line_subtotal_tax').'
';//行項目小計稅echo $item->get_meta('_line_total').'<br>';//行項目總計echo $item->get_meta('_line_tax').'<br>';//行項目總稅額//變體的產品屬性echo $item->get_meta('pa_color').'<br>';//顏色echo $item->get_meta('pa_size').'<br>';//顏色//自定義項目元數據echo $item->get_meta('custom_meta_key').'<br>';//自定義元鍵可見//... 等等 ...

相關:如何獲取 WooCommerce 訂單詳情

I am trying to get the line items of an order.

I'm doing this:

$order = new WC_Order(147);
foreach ($order->get_items() as $key => $lineItem) {
    print_r('<pre>----');
    print_r($lineItem);
    print_r('----</pre>');
}

I can see all the data I need but the array shows this:

[meta_data:protected] => Array

How can I access this array to get the values?

Thanks.

解決方案

Since WooCommerce 3.0+ for Order items there is new Object class WC_Order_Item_Product.
Now Order items properties can't be accessed directly as before

So if you look at your output raw data you will see that each line item is now an object, and you will be able to access that protected data using exclusively:

  1. WC_Order_Item_Product getters methods (or to change it with the setters methods)…
  2. WC_Order_Item get_formatted_meta_data( '', true ) method to access all meta data. It gives an array of accessible objects. See WC_Data method get_meta() to access each meta data.
  3. WC_Data getters methods to unprotect this data and access it through arrays using methods:
    • get_data() (this method is the very useful)
    • get_meta() (this method is the most useful)
    • get_data_keys()
    • get_meta_data() (does not unprotect the data, use get_formatted_meta_data())
  4. wc_get_order_item_meta() dedicated function.

The WC_Order_Item_Product getters methods:

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
    echo $item->get_type().'<br>'; // The order item type
    echo $item->get_product_id().'<br>'; // The Product ID
    echo $item->get_variation_id().'<br>'; // The variation ID
    echo $item->get_quantity().'<br>'; // Line item quantity
    echo $item->get_subtotal().'<br>'; // Line item subtotal
    echo $item->get_total().'<br>'; // Line item total

    // The associated product object (which properties can't be accessed directly too)
    echo '<pre>'; print_r( $item->get_product() ); echo '</pre>'; 

    // ... and so on ...

    ## Testing raw output (protected)
    // echo '<pre>'; print_r($item); echo '</pre>';
}

The wc_get_order_item_meta() function. Here you can go in wp_woocommerce_order_itemmeta table and output any data for an item ID using the corresponding meta_key (for line_item data type item ID):

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    echo wc_get_order_item_meta( $item_id, '_product_id', true). '<br>'; // Product ID
    echo wc_get_order_item_meta( $item_id, '_variation_id', true). '<br>'; // Variation ID
    echo wc_get_order_item_meta( $item_id, '_qty', true). '<br>'; // quantity
    echo wc_get_order_item_meta( $item_id, '_line_subtotal', true). '<br>'; // Line subtotal

    // ... and so on ...

    ## Testing raw output (protected data)
    // echo '<pre>'; print_r($item); echo '</pre>';
}

The WC_Data method get_data() method (to unprotect the data in an array):

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    // Get the most useful Item product data in an accessible array
    $item_data = $item->get_data();

    echo $item_data['id'].'<br>'; // The order item ID
    echo $item_data['order_id'].'<br>'; // The order ID
    echo $item_data['product_id'].'<br>'; // The Product ID
    echo $item_data['variation_id'].'<br>'; // The Variation ID
    echo $item_data['name'].'<br>'; // The Product title (name)
    echo $item_data['quantity'].'<br>'; // Line item quantity
    echo $item_data['subtotal'].'<br>'; // Line item subtotal
    echo $item_data['total'].'<br>'; // Line item total

    // ... and so on ...

The WC_Data method get_meta() method (to access each property by its meta key):

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    echo $item->get_meta('_product_id').'<br>'; // The Product ID
    echo $item->get_meta('_variation_id').'<br>'; // The Variation ID
    echo $item->get_meta('_qty').'<br>'; // Line item quantity
    echo $item->get_meta('_line_subtotal').'<br>'; // Line item subtotal
    echo $item->get_meta('_line_subtotal_tax').'<br>'; // Line item subtotal tax
    echo $item->get_meta('_line_total').'<br>'; // Line item total
    echo $item->get_meta('_line_tax').'<br>'; // Line item total tax

    // Product attributes for variation
    echo $item->get_meta('pa_color').'<br>'; // Color
    echo $item->get_meta('pa_size').'<br>'; // Color

    // Custom item meta gata
    echo $item->get_meta('custom_meta_key').'<br>'; // custom meta key visible


    // ... and so on ...

Related: How to get WooCommerce order details

這篇關于在 Woocommerce 3 中訪問受訂單項保護的數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 波多野结衣精品在线 | 九色av| 欧美在线一二三 | 国产亚洲精品91 | 欧美成人自拍 | 一区二区三区在线免费看 | 国产欧美日韩综合精品一区二区 | 黑人精品欧美一区二区蜜桃 | 精品国产乱码久久久久久图片 | 国产精品99久久久久久宅男 | 高清一区二区三区 | 91色视频在线观看 | 成人免费三级电影 | 日韩电影一区二区三区 | 久久久久一区二区 | 国产精品久久久久久久久久免费 | 成人一级毛片 | 91精品国产91久久久久久最新 | 中文在线一区二区 | www.888www看片| 久久久久久久国产 | 色精品视频 | 人人射人人 | 亚洲高清视频在线观看 | 狠狠av | 看片91| av久久| 精品亚洲国产成av人片传媒 | 国产成人一区 | 小草久久久久久久久爱六 | 精品一区二区三区四区视频 | 欧美1区| 岛国av一区二区 | 日韩欧美不卡 | 7777精品伊人久久精品影视 | 日本精品久久 | www312aⅴ欧美在线看 | 国产一区二区三区视频 | 日本精品久久久久久久 | hitomi一区二区三区精品 | 国产精品视频网 |