當(dāng)前位置:首頁>WordPress建站>WordPress開發(fā)>如何獲取 WooCommerce 訂單詳細(xì)信息?

如何獲取 WooCommerce 訂單詳細(xì)信息?

眾所周知,WooCommerce 是最流行的電子商務(wù)解決方案平臺,它已經(jīng)擁有默認(rèn)模板設(shè)計的每個頁面的默認(rèn)模板。

因此,有時我們想使用自定義模板更改模板的設(shè)計,并且在該模板中,我們需要按照自定義設(shè)計顯示訂單詳細(xì)信息。

讓我們開始吧。

獲取 WooCommerce 訂單詳細(xì)信息

我們必須發(fā)送自定義電子郵件正文內(nèi)容。因此,我們必須使用WC_Order對象來獲取 WooCommerce 訂單詳細(xì)信息。
我們還可以使用wc_get_orders(),這是獲取訂單對象并獲取特定參數(shù)的訂單詳細(xì)信息的函數(shù)。

$order_id = 145;
$order = new WC_Order( $order_id );
//$order = wc_get_order( $order_id );

$orderid  = $order->get_id(); // 獲取訂單 ID
$parent_id = $order->get_parent_id(); // 獲取父級訂單 ID

$user_id   = $order->get_user_id(); // 獲取用戶 ID
$user      = $order->get_user(); // 獲取 WP_User 對象

// 獲取訂單狀態(tài)
$order_status  = $order->get_status(); // 獲取訂單狀態(tài)

// Get Order Dates
$date_created  = $order->get_date_created(); // 獲取創(chuàng)建日期
$date_modified = $order->get_date_modified(); // 獲取修改日期
$date_completed = $order->get_date_completed(); // 獲取訂單完成日期
$date_paid = $order->get_date_paid(); // 獲取訂單付款日期

//獲取訂單賬單詳情
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$billing_company = $order->get_billing_company();
$billing_address1 = $order->get_billing_address_1();
$billing_address2 = $order->get_billing_address_2();
$billing_city = $order->get_billing_city();
$billing_state = $order->get_billing_state();
$billing_postcode = $order->get_billing_postcode();
$billing_country = $order->get_billing_country();
$billing_email = $order->get_billing_email();
$billing_phone = $order->get_billing_phone();
$billing_formatted_name = $order->get_formatted_billing_full_name();
$billing_formatted_address = $order->get_formatted_billing_address();

//獲取訂單物流詳情
$shipping_first_name = $order->get_shipping_first_name();
$shipping_last_name = $order->get_shipping_last_name();
$shipping_company = $order->get_shipping_company();
$shipping_address1 = $order->get_shipping_address_1();
$shipping_address2 = $order->get_shipping_address_2();
$shipping_city = $order->get_shipping_city();
$shipping_state = $order->get_shipping_state();
$shipping_postcode = $order->get_shipping_postcode();
$shipping_country = $order->get_shipping_country();
$shipping_formatted_name = $order->get_formatted_shipping_full_name();
$shipping_formatted_address = $order->get_formatted_shipping_address();

//獲取訂單付款詳情
$currency      = $order->get_currency(); // 獲取所用的貨幣 
$payment_title = $order->get_payment_method_title(); // 獲取付款方式名稱
$payment_method = $order->get_payment_method(); // 獲取付款方式 ID
$fees = $order->get_fees(); // 獲取訂單手續(xù)費
$subtotal = $order->get_subtotal(); // 獲取訂單費用小計
$total_tax = $order->get_total_tax(); // 獲取訂單總稅費
$total = $order->get_total(); // 獲取訂單總費用

所以,我們看看如何借助getter 方法獲取 WooCoomerce 訂單詳細(xì)信息。您可以在此處查看更多方法。

如果您想獲取和訪問訂單對象的受保護數(shù)據(jù),則可以使用get_data()功能。

這與我們在上面對$order對象實例所做的相同。例如:$order->get_data()

它將數(shù)據(jù)作為關(guān)聯(lián)數(shù)組返回,我們可以將此數(shù)組與 key=>pair 值一起使用。

讓我們看看這個例子。

$order_id = 145;
$order = wc_get_order( $order_id );

$order_data = $order->get_data(); // 獲取訂單數(shù)據(jù)
$orderid  = $order_data['id']; // 獲取訂單 ID
$parent_id = $order_data['parent_id']; // 獲取父級訂單 ID

// 獲取訂單狀態(tài)
$order_status  = $order_data['status']; // 獲取訂單狀態(tài)

// Get Order Dates
$date_created  = $order_data['date_created']; // 獲取創(chuàng)建日期
$date_modified = $order_data['date_modified']; // 獲取修改日期
$date_completed = $order_data['date_completed']; // 獲取訂單完成日期
$date_paid = $order_data['date_paid']; // 獲取訂單付款日期

//獲取訂單賬單信息
$billing_first_name = $order_data['billing']['first_name'];
$billing_last_name = $order_data['billing']['last_name'];
$billing_company = $order_data['billing']['company'];
$billing_address1 = $order_data['billing']['address_1'];
$billing_address2 = $order_data['billing']['address_2'];
$billing_city = $order_data['billing']['city'];
$billing_state = $order_data['billing']['state'];
$billing_postcode = $order_data['billing']['postcode'];
$billing_country = $order_data['billing']['country'];
$billing_email = $order_data['billing']['email'];
$billing_phone = $order_data['billing']['phone'];

//獲取訂單物流信息
$shipping_first_name = $order_data['shipping']['first_name'];
$shipping_last_name = $order_data['shipping']['last_name'];
$shipping_company = $order_data['shipping']['company'];
$shipping_address1 = $order_data['shipping']['address_1'];
$shipping_address2 = $order_data['shipping']['address_2'];
$shipping_city = $order_data['shipping']['city'];
$shipping_state = $order_data['shipping']['state'];
$shipping_postcode = $order_data['shipping']['postcode'];
$shipping_country = $order_data['shipping']['country'];

//獲取訂單付款信息
$subtotal = $order_data['subtotal']; // 獲取訂單費用小計
$total_tax = $order_data['total_tax']; // 獲取訂單總稅費
$total = $order_data['total']; // 獲取訂單總費用

使用 get_data() 函數(shù),您可以使用訂單數(shù)據(jù)作為數(shù)組屬性來獲取 WooCommerce 訂單詳細(xì)信息。

如果您在一個訂單中有多個項目,您還可以使用函數(shù) get_items()循環(huán)訂單項目。

請參閱簡單示例以了解這一點。

foreach ( $order->get_items() as $item_id => $item ) {
   $product = $item->get_product();
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   
   $name = $item->get_name();
   $quantity = $item->get_quantity();

   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();

   $all_meta = $item->get_meta_data();
   $single_meta = $item->get_meta( '_meta_key', true );

   $type = $item->get_type();

   // 等等...
}

在這里,我們循環(huán)訂單項目并使用 getter 方法獲取值,如我們在上面使用的那樣。

因此,在本文中,我們使用:

WC_Order

wc_get_order()

get_data()

get_items()

我希望通過本文介紹,你可以掌握如何獲取 WooCommerce 訂單詳細(xì)信息并使用它。

注:本文內(nèi)容出自 yourblogcoach.com,由 WordPress大學(xué) 翻譯整理。

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
WordPress開發(fā)

WordPress 5.9 對文章、文章類型和分類法的更改

2021-12-14 9:53:02

WordPress插件商城相關(guān)

8 個好用的 WooCommerce 收藏夾/愿望清單插件

2022-3-11 23:58:14

0 條回復(fù) A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

沁阳市| 临沭县| 灵寿县| 资溪县| 三河市| 休宁县| 弋阳县| 六安市| 永兴县| 师宗县| 子洲县| 随州市| 吉首市| 百色市| 全南县| 海南省| 普陀区| 和政县| 元朗区| 中方县| 富源县| 涿鹿县| 白水县| 贵溪市| 商南县| 浦东新区| 毕节市| 海城市| 留坝县| 常熟市| 温州市| 水富县| 塘沽区| 邹城市| 成都市| 东阳市| 南靖县| 平阳县| 惠水县| 宁南县| 自贡市|