WooCommerceの関数を調べながらのメモ書き程度に
なおWooCommerce versionは2.6以上です。
参考
woocommerce wiki
wc_get_orders and WC_Order_Query <-ほぼこちらの翻訳
概要
WooCommerceのオーダー(注文)の一覧を取る標準の関数はwc_get_orders と WC_Order_Queryです。これは安全で将来的なWooCommerceのバージョンによるデーターベースの変更にも対応しています。
wc_get_orders
とWC_Order_Query
はちょうどget_posts
とWP_Query
の関係のようなものです。
簡単な例
johnさんの2016年支払いのオーダーは以下のように取得します。
$orders = wc_get_orders( array(
'billing_first_name' => 'John',
'date_paid' => '2016-01-01...2016-12-31',
) );
最新から10件のオーダーをIDでリターン
$query = new WC_Order_Query( array(
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
メールアドレスが'woocommerce@woocommerce.com'の顧客のオーダーを取得(WC_Order_Queryで)
$query = new WC_Order_Query();
$query->set( 'customer', 'woocommerce@woocommerce.com' );
$orders = $query->get_orders();
wc_get_orders
はWC_Order_Query
のget_orders()
を呼び出しているので、このような取得でも特に問題ない。
よく使いそうなパラメーター
受注ステータス 'status'
WooCommerceは内部でpost_statusを'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled'を持っている。(カスタムしたステータスもこの状態で取得できる。要学習)
on-holdステータスの場合
$args = array(
'status' => 'on-hold',
);
$orders = wc_get_orders( $args );
取得数など limit paged
limit 取得数
paged 上記limitの何ページ目か
最新のオーダーの4から6件目
$args = array(
'limit' => 3,
'paged' => 2,
);
$orders = wc_get_orders( $args );
ページネーション paginate
真偽値で指定。trueを指定した場合、returnが変わる。
ordersとtotalとmax_num_pagesを持つオブジェクトになる。
$args = array(
'paginate' => true,
);
$results = wc_get_orders( $args );
echo $results->total . " orders found\n";
echo 'Page 1 of ' . $results->max_num_pages . "\n";
echo 'First order id is: ' . $results->orders[0]->get_id() . "\n";
リターン return
'ids'か'objects'を指定できる。IDno配列かオブジェクトの配列か。
$args = array(
'return' => 'ids',
);
$orders = wc_get_orders( $args );
その他の指定
支払いの指定
以下のような指定があるらしい。勉強中
currency : 通貨単位 USDとかJPYとか?
prices_include_tax : 内税 'yes' or 'no'
payment_method :支払い方法
payment_method_title : 支払い方法の名称
discount_total
discount_tax
shipping_total
shipping_tax
cart_tax
total
購入者の指定
customer : emailかcustomer idで指定
customer_id : customer idで指定
住所と名前
購入者(billing)と発送先(shipping) まだ使ったことないです。
billing_first_name
billing_last_name
billing_company
billing_address_1
billing_address_2
billing_city
billing_state
billing_postcode
billing_country
billing_email
billing_phone
shipping_first_name
shipping_last_name
shipping_company
shipping_address_1
shipping_address_2
shipping_city
shipping_state
shipping_postcode
shipping_country
customer_ip_address : 不明
日付の指定
date_created, date_modified, date_completed, date_paidにそれぞれ指定できる。
以下のような形式で指定できる。また、タイムスタンプでも指定可能
YYYY-MM-DD
YYYY-MM-DD
>YYYY-MM-DD
YYYY-MM-DDより後
>=YYYY-MM-DD
YYYY-MM-DD以後
<YYYY-MM-DD
YYYY-MM-DDより前
<=YYYY-MM-DD
YYYY-MM-DD以前
YYYY-MM-DD...YYYY-MM-DD
の間
支払いが2016-02-12より後
$args = array(
'date_paid' => '>2016-02-12',
);
$orders = wc_get_orders( $args );
カスタムパラメーター
カスタムクエリ変数を追加することができる。
下記のようにフィルターフックをかける。
function handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['customvar'] ) ) {
$query['meta_query'][] = array(
'key' => 'customvar',
'value' => esc_attr( $query_vars['customvar'] ),
);
}
return $query;
}
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_custom_query_var', 10, 2 );
そしたら下記のように取得できる。
$orders = wc_get_orders( array( 'customvar' => 'somevalue' ) );
まだまだ勉強中。間違えあったら追記します。