1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WooCommerce Subscriptions でコピーする元注文の subscription_id の取得方法

Last updated at Posted at 2019-07-24

WooCommerce Subscriptions で元注文引き出す関数がない

WooCommerce Subscriptions の場合、定期購入時の元注文となる POST ID をかんたんに引き出す関数がありません。
そのため、元注文の post_id を見つけてくる方法を記述します。大きく2パターンあります。

一回目の注文時

まずは最初の注文時です。ここが少しややこしいのですが、最初の注文が出来た後に、2回目以降にコピーして使う定期購入用の注文データを最初の注文番号の後に作る仕組みになっています。なので、最初の注文の post_meta などから探すことが出来ません。
WooCommerce subscription の wcs_get_subscriptions_for_order 関数を利用して以下のように引き出します。

$subscriptions_ids = wcs_get_subscriptions_for_order( $order_id );
foreach( $subscriptions_ids as $subscription_id => $subscription_obj )
    if($subscription_obj->order->id == $order_id) break;
$subscription_id;//order_ID
$subscription_obj;//Order Object

二回目移行の注文時

二回目以降は、post_meta に保存されていますので、簡単に引き出せます。

$subscription_id = $order->get_meta(_subscription_renewal, true);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?