1

我试图在 admin-ajax.php 请求中检索订单信息,但它返回的受保护对象没有数据。有没有办法访问这个?

我正在使用这个:

$data = new WC_Order($order_id);
var_dump($data);

这在其中运行时有效:

add_action('woocommerce_order_status_completed', 'send_documents_to_customer');

但我还需要将其作为管理员 ajax 请求运行:

add_action('wp_ajax_send_documents_to_customer', 'send_documents_to_customer');

但它返回受保护:

object(WC_Order)#15861 (14) { ["status_transition":protected]=> bool(false) ["data":protected]=> array(28) { ["parent_id"]=> int(0) ["status"]=> string(0) "" ["currency"]=> string(0) "" ["version"]=> string(0) "" ["prices_include_tax"]=> bool(false) ["date_created"]=> NULL ["date_modified"]=> NULL ["discount_total"]=> int(0) ["discount_tax"]=> int(0) ["shipping_total"]=> int(0) ["shipping_tax"]=> int(0) ["cart_tax"]=> int(0) ["total"]=> int(0) ["total_tax"]=> int(0) ["customer_id"]=> int(0) ["order_key"]=> string(0) "" ["billing"]=> array(11) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" ["email"]=> string(0) "" ["phone"]=> string(0) "" } ["shipping"]=> array(9) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" } ["payment_method"]=> string(0) "" ["payment_method_title"]=> string(0) "" ["transaction_id"]=> string(0) "" ["customer_ip_address"]=> string(0) "" ["customer_user_agent"]=> string(0) "" ["created_via"]=> string(0) "" ["customer_note"]=> string(0) "" ["date_completed"]=> NULL ["date_paid"]=> NULL ["cart_hash"]=> string(0) "" } ["items":protected]=> array(1) { ["line_items"]=> array(0) { } } ["items_to_delete":protected]=> array(0) { } ["cache_group":protected]=> string(6) "orders" ["data_store_name":protected]=> string(5) "order" ["object_type":protected]=> string(5) "order" ["id":protected]=> int(0) ["changes":protected]=> array(0) { } ["object_read":protected]=> bool(true) ["extra_data":protected]=> array(0) { } ["default_data":protected]=> array(28) { ["parent_id"]=> int(0) ["status"]=> string(0) "" ["currency"]=> string(0) "" ["version"]=> string(0) "" ["prices_include_tax"]=> bool(false) ["date_created"]=> NULL ["date_modified"]=> NULL ["discount_total"]=> int(0) ["discount_tax"]=> int(0) ["shipping_total"]=> int(0) ["shipping_tax"]=> int(0) ["cart_tax"]=> int(0) ["total"]=> int(0) ["total_tax"]=> int(0) ["customer_id"]=> int(0) ["order_key"]=> string(0) "" ["billing"]=> array(11) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" ["email"]=> string(0) "" ["phone"]=> string(0) "" } ["shipping"]=> array(9) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" } ["payment_method"]=> string(0) "" ["payment_method_title"]=> string(0) "" ["transaction_id"]=> string(0) "" ["customer_ip_address"]=> string(0) "" ["customer_user_agent"]=> string(0) "" ["created_via"]=> string(0) "" ["customer_note"]=> string(0) "" ["date_completed"]=> NULL ["date_paid"]=> NULL ["cart_hash"]=> string(0) "" } ["data_store":protected]=> object(WC_Data_Store)#15862 (4) { ["instance":"WC_Data_Store":private]=> object(WC_Order_Data_Store_CPT)#15863 (3) { ["internal_meta_keys":protected]=> array(49) { [0]=> string(14) "_customer_user" [1]=> string(10) "_order_key" [2]=> string(15) "_order_currency" [3]=> string(19) "_billing_first_name" [4]=> string(18) "_billing_last_name" [5]=> string(16) "_billing_company" [6]=> string(18) "_billing_address_1" [7]=> string(18) "_billing_address_2" [8]=> string(13) "_billing_city" [9]=> string(14) "_billing_state" [10]=> string(17) "_billing_postcode" [11]=> string(16) "_billing_country" [12]=> string(14) "_billing_email" [13]=> string(14) "_billing_phone" [14]=> string(20) "_shipping_first_name" [15]=> string(19) "_shipping_last_name" [16]=> string(17) "_shipping_company" [17]=> string(19) "_shipping_address_1" [18]=> string(19) "_shipping_address_2" [19]=> string(14) "_shipping_city" [20]=> string(15) "_shipping_state" [21]=> string(18) "_shipping_postcode" [22]=> string(17) "_shipping_country" [23]=> string(15) "_completed_date" [24]=> string(10) "_paid_date" [25]=> string(10) "_edit_lock" [26]=> string(10) "_edit_last" [27]=> string(14) "_cart_discount" [28]=> string(18) "_cart_discount_tax" [29]=> string(15) "_order_shipping" [30]=> string(19) "_order_shipping_tax" [31]=> string(10) "_order_tax" [32]=> string(12) "_order_total" [33]=> string(15) "_payment_method" [34]=> string(21) "_payment_method_title" [35]=> string(15) "_transaction_id" [36]=> string(20) "_customer_ip_address" [37]=> string(20) "_customer_user_agent" [38]=> string(12) "_created_via" [39]=> string(14) "_order_version" [40]=> string(19) "_prices_include_tax" [41]=> string(15) "_date_completed" [42]=> string(10) "_date_paid" [43]=> string(15) "_payment_tokens" [44]=> string(22) "_billing_address_index" [45]=> string(23) "_shipping_address_index" [46]=> string(15) "_recorded_sales" [47]=> string(29) "_recorded_coupon_usage_counts" [48]=> string(16) "_shipping_method" } ["meta_type":protected]=> string(4) "post" ["object_id_field_for_meta":protected]=> string(0) "" } ["stores":"WC_Data_Store":private]=> array(18) { ["coupon"]=> string(24) "WC_Coupon_Data_Store_CPT" ["customer"]=> string(22) "WC_Customer_Data_Store" ["customer-download"]=> string(31) "WC_Customer_Download_Data_Store" ["customer-session"]=> string(30) "WC_Customer_Data_Store_Session" ["order"]=> string(23) "WC_Order_Data_Store_CPT" ["order-refund"]=> string(30) "WC_Order_Refund_Data_Store_CPT" ["order-item"]=> string(24) "WC_Order_Item_Data_Store" ["order-item-coupon"]=> string(31) "WC_Order_Item_Coupon_Data_Store" ["order-item-fee"]=> string(28) "WC_Order_Item_Fee_Data_Store" ["order-item-product"]=> string(32) "WC_Order_Item_Product_Data_Store" ["order-item-shipping"]=> string(33) "WC_Order_Item_Shipping_Data_Store" ["order-item-tax"]=> string(28) "WC_Order_Item_Tax_Data_Store" ["payment-token"]=> string(27) "WC_Payment_Token_Data_Store" ["product"]=> string(25) "WC_Product_Data_Store_CPT" ["product-grouped"]=> string(33) "WC_Product_Grouped_Data_Store_CPT" ["product-variable"]=> string(34) "WC_Product_Variable_Data_Store_CPT" ["product-variation"]=> string(35) "WC_Product_Variation_Data_Store_CPT" ["shipping-zone"]=> string(27) "WC_Shipping_Zone_Data_Store" } ["current_class_name":"WC_Data_Store":private]=> string(23) "WC_Order_Data_Store_CPT" ["object_type":"WC_Data_Store":private]=> string(5) "order" } ["meta_data":protected]=> NULL }

4

2 回答 2

0

您可以使用WC Order提供的功能获取所有订单数据

于 2017-06-05T13:27:48.713 回答
0

事实证明,$order_id 实际上并没有将值传递给 admin-ajax.php:

function send_documents_to_customer($order_id) {

所以我在下面写了:

if(is_admin() && !$order_id > 0 && isset($_GET['order_id'])) {
    $order_id = $_GET['order_id'];
}

这可确保始终设置有效值。

于 2017-06-05T13:55:03.543 回答