WooCommerce

WooCommerce Class WC_Order

WC_Order这个类是WooCommerce里操作订单的类,实例化这个类就获取了一个订单,之后就能读取订单的各种属性,或者操作订单。本文简单记录了常用的方法。

名词解释

  • order_item – 订单中的详细条目,order_item的类型有product、fee、coupon、shipping等,最常见的line_item是指product。
  • order_item_meta – order_item的详细信息,如果把order_item类比成post,那order_item_meta就是post_meta。
  • item_subtotal – 产品的单价,不包含优惠。
  • item_total – 产品优惠完的价格。
  • line_subtotal – 一个产品的单价乘以它的购买数量,即一个产品的总价,不包含优惠。
  • line_total – 就是line_subtotal加上优惠。

实例化一个WC_Order类

$order = wc_get_order( 871 ); // 871 is the order ID

价格优惠等信息

// Get totals for display on pages and in emails including subtotal,total,shipping,tax,refund etc.
$order->get_order_item_totals();
$order->get_discount_total();
$order->get_discount_tax();
$order->get_shipping_total();
$order->get_shipping_tax();
$order->get_cart_tax();
$order->get_total();
$order->get_total_tax();
$order->get_total_discount();
$order->get_subtotal();
$order->get_tax_totals();
$order->get_formatted_order_total();
$order->get_subtotal_to_display( $compound = false, $tax_display = '' );
$order->get_shipping_to_display( $tax_display = '' );
$order->get_discount_to_display( $tax_display = '' );

// Return an array of shipping costs within this order.
$order->get_shipping_methods();

// Gets formatted shipping method title.
$order->get_shipping_method();

$order->get_coupon_codes();

Getters


$order->get_type();
$order->get_currency();
$order->get_version();
$order->get_status();

// Gets the order number for display (by default, order ID).
$order->get_order_number();

// e.g. wc_order_48atSN91iwA8C
$order->get_order_key();

$order->get_customer_id();

//Get the user associated with the order. False for guests. 
$order->get_user();

$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_formatted_billing_full_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_formatted_shipping_full_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_shipping_phone();
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_customer_note();

$order->get_date_completed();
$order->get_date_paid();

$order->get_cart_hash();

// Raw addresses in array format.
$order->get_address();

$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();

// usually checkout
$order->get_created_via();

// Get a google map url with the shipping address as its parameter.
$order->get_shipping_address_map_url();

// Get basic order data in array format.
$order->get_base_data();

// Get all class data in array format.
$order->get_data();

$order->has_billing_address();
$order->has_shipping_address();

Setters

这些方法只能改变存储在类中的值,不会修改数据库。如果要更改数据库,需要调用save方法

$order->set_version( $value );
$order->set_currency( $value );
$order->set_discount_total( $value );
$order->set_discount_tax( $value );
$order->set_shipping_total( $value );
$order->set_shipping_tax( $value );
$order->set_cart_tax( $value );
$order->set_total( $value );
$order->set_order_key( $value );
$order->set_customer_id( $value );
$order->set_billing_first_name( $value );
$order->set_billing_last_name( $value );
$order->set_billing_company( $value );
$order->set_billing_address_1( $value );
$order->set_billing_address_2( $value );
$order->set_billing_city( $value );
$order->set_billing_state( $value );
$order->set_billing_postcode( $value );
$order->set_billing_country( $value );
$order->set_billing_email( $value );
$order->set_billing_phone( $value );
$order->set_shipping_first_name( $value );
$order->set_shipping_last_name( $value );
$order->set_shipping_company( $value );
$order->set_shipping_address_1( $value );
$order->set_shipping_address_2( $value );
$order->set_shipping_city( $value );
$order->set_shipping_state( $value );
$order->set_shipping_postcode( $value );
$order->set_shipping_country( $value );
$order->set_shipping_phone( $value );
$order->set_payment_method( $payment_method  );
$order->set_payment_method_title( $value  );
$order->set_transaction_id( $value  );
$order->set_customer_ip_address( $value  );
$order->set_customer_user_agent( $value  );
$order->set_created_via( $value  );
$order->set_customer_note( $value  );
$order->set_date_completed( $date  );
$order->set_date_paid( $date  );
$order->set_cart_hash( $value  );
$order->set_status( 'wc-failed', 'A manual update', true );

举例:如果用set_status()修改订单状态,需要这样做。

$order->set_status( 'wc-failed', 'A manual update', true );
$order->save();

更新Order Meta,也就是postmeta表里的数据

$order->update_meta_data( 'custom_order_meta_key', 'your value' );
$order->save();

Conditionals

// Check if an order key is valid.
$order->key_is_valid( $key );

// See if order matches cart_hash.
$order->has_cart_hash( $key );

// Checks if an order can be edited, specifically for use on the Edit Order screen.
$order->is_editable( $key );

// Returns if an order has been paid for based on the order status.
$order->is_paid();

// Checks if product download is permitted.
$order->is_download_permitted();

// Checks if an order needs display the shipping address, based on shipping method.
$order->needs_shipping_address();

// Returns true if the order contains a downloadable product.
$order->has_downloadable_item();

// Get downloads from all line items for this order.
$order->get_downloadable_items();

// Checks if an order needs payment, based on status and order total.
$order->needs_payment();

// See if the order needs processing before it can be completed.
$order->needs_processing();

URLs and Endpoints

// Generates a URL so that a customer can pay for their (unpaid - pending) order. Pass 'true' for the checkout version which doesn't offer gateway choices.
$order->get_checkout_payment_url( $on_checkout = false );

// Generates a URL for the thanks page (order received).
$order->get_checkout_order_received_url();

// Generates a URL so that a customer can cancel their (unpaid - pending) order.
$order->get_cancel_order_url();

// Generates a raw (unescaped) cancel-order URL for use by payment gateways.
$order->get_cancel_order_url_raw();

//  Generates a URL to view an order from the my account page.
$order->get_view_order_url();

// Get's the URL to edit the order in the backend.
$order->get_edit_order_url();

Order notes

// Adds a note (comment) to the order. Order must exist.
$order->add_order_note( 'my note', $is_customer_note = 0, $added_by_user = false );

$order->get_customer_order_notes();

Refunds

// Get order refunds. refunds are stored as a custom post type
$order->get_refunds();

// Get amount already refunded.
$order->get_total_refunded();

$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();

$order->get_item_count_refunded( $item_type = '' );
$order->get_total_qty_refunded( $item_type = 'line_item' );
$order->get_qty_refunded_for_item( $item_id, $item_type = 'line_item' );
$order->get_total_refunded_for_item( $item_id, $item_type = 'line_item' );
$order->get_tax_refunded_for_item( $item_id, $tax_id, $item_type = 'line_item' );
$order->get_total_tax_refunded_by_rate_id( $rate_id );
$order->get_remaining_refund_amount();
$order->get_remaining_refund_items();

Order Status

$order->set_status( $new_status, $note = '', $manual_update = false );
$order->update_status( $new_status, $note = '', $manual = false );

Order Item Handling

// Return an array of items/products within this order. 
$order->get_items( $types = 'line_item' );

$order->get_coupons();
$order->get_fees();
$order->get_taxes();

// Gets the count of order items of a certain type.
$order->get_item_count( $item_type = '' );

// Get an order item object by order_item_id
$order->get_item( $item_id );

$order->remove_item( $item_id );
$order->add_item( $item );
$order->add_product( $product, $qty = 1, $args = array() );

// Item unit price before discount
$order->get_item_subtotal( $item, $inc_tax = false, $round = true );

// Item unit price after discount
$order->get_item_total( $item, $inc_tax = false, $round = true );

// Item total price (unit price * qty) before discount
$order->get_line_subtotal( $item, $inc_tax = false, $round = true );

// Item total price (unit price * qty) after discount
$order->get_line_total( $item, $inc_tax = false, $round = true );

$order->get_item_tax( $item, $round = true );
$order->get_line_tax( $item );
$order->get_formatted_line_subtotal( $item, $tax_display = '' );

示例:获取订单的子项目

// 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();
    $name         = $item->get_name();
    $quantity     = $item->get_quantity();
    $subtotal     = $item->get_subtotal();
    $total        = $item->get_total();
    $tax          = $item->get_subtotal_tax();
    $taxclass     = $item->get_tax_class();
    $taxstat      = $item->get_tax_status();
    $allmeta      = $item->get_meta_data();
    $somemeta     = $item->get_meta( '_whatever', true );
    $type         = $item->get_type();
    
}

根据条件获取多个订单

WC_Order_Query可以获取多个订单,每个订单都是一个WC_Order的实例,可以用上述方法操作。下面的代码会选出最近的10个订单。

$query = new WC_Order_Query( array(
    'limit' => 10,
    'orderby' => 'date',
    'order' => 'DESC',
) );
$orders = $query->get_orders();

动态生成订单并返回付款链接

function create_order() {

  $address = array(
    'first_name' => 'Lily',
    'last_name'  => 'Corson',
    'company'    => 'Your company',
    'email'      => 'youremail@email.com',
    'phone'      => '19283811981',
    'address_1'  => 'Your address',
    'city'       => 'San Francisco',
    'state'      => 'Ca',
    'postcode'   => '92121',
    'country'    => 'US'
  );
  
  // Now we create the order
  $order = wc_create_order();
  
  // Add a product to the cart, ID - 268, Quantity - 1
  $order->add_product( get_product( 268 ), 1);
  
  // Set addresses
  $order->set_address( $address, 'billing' );
  $order->set_address( $address, 'shipping' );

  // Set payment gateway
  $payment_gateways = WC()->payment_gateways->payment_gateways();
  $order->set_payment_method( $payment_gateways['bacs'] );
  
  // Set some props to help identify orders created dynamically
  $order->add_order_note( 'Created By System');
  $order->set_created_via( 'dynamic_Generated' );
  $order->set_customer_id( get_current_user_id() );
  $order->set_customer_ip_address( WC_Geolocation::get_ip_address() );
  $order->set_customer_user_agent( wc_get_user_agent() );

  // Calculate totals
  $order->calculate_totals();

  // Return the payment url
  return $order->get_checkout_payment_url();
}

参考: