我正在使用woo-commerce並嘗試從數據庫中獲取所有訂單詳細信息。我是新手,無法使其發生。我已經創建了order_details.php
模板,可以從中獲取所有訂單數據。如何在wordpress/woocommerce中獲得所有訂單詳情?
任何人都可以幫我嗎?
我正在使用woo-commerce並嘗試從數據庫中獲取所有訂單詳細信息。我是新手,無法使其發生。我已經創建了order_details.php
模板,可以從中獲取所有訂單數據。如何在wordpress/woocommerce中獲得所有訂單詳情?
任何人都可以幫我嗎?
試試此代碼。
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$orders = $my_query->posts;
echo "</pre>";
print_r($orders);
echo "<pre>";
Customize As par Ur Urerment。
您可以通過用戶下面的代碼
require(dirname(__FILE__) . '/wp-load.php');
global $wpdb;
$orders = get_posts(array(
'post_type' => 'shop_order',
'posts_per_page' => '-1'
));
您需要包括wp-load.php
文件
require(dirname(__FILE__) . '/wp-load.php');
與此代碼之前添加全局變量作爲global $wpdb;
。
$my_course_query = new WP_Query(array('post_type'=>'shop_order','post_status'=>'wc-completed'));
狀態更改爲woocoommerce狀態
由於Woocommerce巨型主要更新3.0+事情已經改變了很多:
有一些新的類訂單項目:
WC_Order_Item類。
WC_Order_Item_Product類。
所以也訂單項的屬性將無法訪問像以前那樣在foreach循環中,你將不得不使用this specific getter and setter methods代替
// Get an instance of the WC_Order object (same as before)
$order = wc_get_order($order_id);
// Get the order ID
$order_id = $order->get_id();
// Get the custumer ID
$order_id = $order->get_user_id();
獲取,並獲得了訂單數據屬性(在一系列值中):
$order = wc_get_order($order_id);
$order_data = $order->get_data(); // The Order data
$order_id = $order_data['id'];
$order_parent_id = $order_data['parent_id'];
$order_status = $order_data['status'];
$order_currency = $order_data['currency'];
$order_version = $order_data['version'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method_title = $order_data['payment_method_title'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method = $order_data['payment_method'];
## Creation and modified WC_DateTime Object date string ##
// Using a formated date (with php date() function as method)
$order_date_created = $order_data['date_created']->date('Y-m-d H:i:s');
$order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s');
// Using a timestamp (with php getTimestamp() function as method)
$order_timestamp_created = $order_data['date_created']->getTimestamp();
$order_timestamp_modified = $order_data['date_modified']->getTimestamp();
$order_discount_total = $order_data['discount_total'];
$order_discount_tax = $order_data['discount_tax'];
$order_shipping_total = $order_data['shipping_total'];
$order_shipping_tax = $order_data['shipping_tax'];
$order_total = $order_data['cart_tax'];
$order_total_tax = $order_data['total_tax'];
$order_customer_id = $order_data['customer_id']; // ... and so on
## BILLING INFORMATION:
$order_billing_first_name = $order_data['billing']['first_name'];
$order_billing_last_name = $order_data['billing']['last_name'];
$order_billing_company = $order_data['billing']['company'];
$order_billing_address_1 = $order_data['billing']['address_1'];
$order_billing_address_2 = $order_data['billing']['address_2'];
$order_billing_city = $order_data['billing']['city'];
$order_billing_state = $order_data['billing']['state'];
$order_billing_postcode = $order_data['billing']['postcode'];
$order_billing_country = $order_data['billing']['country'];
$order_billing_email = $order_data['billing']['email'];
$order_billing_phone = $order_data['billing']['phone'];
## SHIPPING INFORMATION:
$order_shipping_first_name = $order_data['shipping']['first_name'];
$order_shipping_last_name = $order_data['shipping']['last_name'];
$order_shipping_company = $order_data['shipping']['company'];
$order_shipping_address_1 = $order_data['shipping']['address_1'];
$order_shipping_address_2 = $order_data['shipping']['address_2'];
$order_shipping_city = $order_data['shipping']['city'];
$order_shipping_state = $order_data['shipping']['state'];
$order_shipping_postcode = $order_data['shipping']['postcode'];
$order_shipping_country = $order_data['shipping']['country'];
獲取訂單商品並使用WC_Order_Item_Pro訪問數據管和WC_Order_Item方法:
$order = wc_get_order($order_id);
// Iterating through each WC_Order_Item_Product objects
foreach ($order->get_items() as $item_key => $item_values):
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item_values->get_id();
## Using WC_Order_Item_Product methods ##
$item_name = $item_values->get_name(); // Name of the product
$item_type = $item_values->get_type(); // Type of the order item ("line_item")
$product_id = $item_values->get_product_id(); // the Product id
$wc_product = $item_values->get_product(); // the WC_Product object
## Access Order Items data properties (in an array of values) ##
$item_data = $item_values->get_data();
$product_name = $item_data['name'];
$product_id = $item_data['product_id'];
$variation_id = $item_data['variation_id'];
$quantity = $item_data['quantity'];
$tax_class = $item_data['tax_class'];
$line_subtotal = $item_data['subtotal'];
$line_subtotal_tax = $item_data['subtotal_tax'];
$line_total = $item_data['total'];
$line_total_tax = $item_data['total_tax'];
endforeach;
感謝您step.But遺憾的是它返回一個空array.Let我解釋一下你,我已經woocommerce安裝在我的本地和有2個訂單it.All我需要得到這些訂單來自數據庫的詳細信息。請讓我知道如何獲取所有這些數據。 @Dev Danidhariya –
'全球$ woocommerce;' 在我的代碼中添加頂部並檢查。 –
你好,解決你的問題。 –