WooCommerce:從 $order 對象獲取訂單信息(總數、項目等)
已發表: 2018-05-16
作為一名 WooCommerce 開發自由職業者,我每天都會重複許多編碼操作,這讓我浪費了時間。 其中之一是:“如果我有 $order 變量/對象,如何獲得____? “。
例如,“我怎樣才能得到訂單總額”? 或者“我怎樣才能得到訂單物品”? 或者可能是訂單 ID、客戶 ID、賬單信息、付款方式、總退款等等……希望這篇文章也能幫助您節省時間!
正如我們在其他文章中看到的,從 $product 對象獲取產品信息並從 $cart 對象獲取購物車信息,並非總是可以直接訪問 $order 變量。
有時,例如,您可能有 $order_id 可用。 在這種情況下,您可以使用 wc_get_order WooCommerce 函數“獲取”訂單對象。
如果您在電子郵件模板中,也可以獲得 $order 信息。 這有助於在您的交易通信中顯示額外的 $order 信息或觸發自定義功能。 不管怎樣,盡情享受吧!
1. 您可以訪問 $order 變量
鉤子(do_action 和 apply_filters)使用傳遞給函數的附加參數。 如果他們允許您使用“$order”對象,您就在做生意。 以下是獲取所有訂單信息的方法:
// Get Order ID and Key
$order->get_id();
$order->get_order_key();
// Get Order Totals $0.00
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product = $item->get_product(); // see link above to get $product info
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$subtotal = $item->get_subtotal();
$total = $item->get_total();
$tax = $item->get_subtotal_tax();
$tax_class = $item->get_tax_class();
$tax_status = $item->get_tax_status();
$allmeta = $item->get_meta_data();
$somemeta = $item->get_meta( '_whatever', true );
$item_type = $item->get_type(); // e.g. "line_item"
}
// Other Secondary Items Stuff
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item_count();
$order->get_item_total();
$order->get_downloadable_items();
$order->get_coupon_codes();
// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
// Get Order Status
$order->get_status();
// Get Thank You Page URL
$order->get_checkout_order_received_url();
2. 您可以訪問 $order_id 變量
如果您有權訪問訂單 ID(同樣,通常 do_action 或 apply_filters 可能會給您這個),您必須首先獲取訂單對象。 然後做與上面完全相同的事情。

// Get $order object from order ID
$order = wc_get_order( $order_id );
// Now you have access to (see above)...
if ( $order ) {
$order->get_formatted_order_total( );
// etc.
// etc.
}
3. 您可以訪問 $email 變量
如果您正在使用 WooCommerce 電子郵件,通常您會將 $email 對像用作參數。 為了從中獲取對象,您需要一個額外的步驟。 然後做與上面完全相同的事情。
// Get $order object from $email
$order = $email->object;
// Now you have access to (see above)...
if ( $order ) {
$order->get_id();
$order->get_formatted_order_total( );
// etc.
// etc.
}