4
5

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 の定期購入時の支払いのフック

Posted at

WooCommerce Subscriptions の決済方法

定期購入時の決済時の処理に関しては、一回目と二回目とで異なるので、それはそれぞれフックや上書きする関数などで対応をします。

WooCommerce Subscriptions の1回目の定期購入時決済

一般的に通常決済と定期購入決済の処理をクラスで拡張クラスを使うことが多いです。
その形のほうがわかりやすいと思います。

通常の決済と同じ

process_payment()を拡張クラスを使うので上書きするような方法もあります。
その場合は、通常と同じようにしたい場合は、親クラスの関数を使う処理を提示するなどしたら良いです。

return parent::process_payment($order_id);

そして、定期購入かどうかを判別しないといけませんので、よく以下のような関数を作って定期購入を含む注文かどうかを判別します。

例:is_subscription
/**
 * Is $order_id a subscription?
 * @param  int  $order_id
 * @return boolean
 */
protected function is_subscription( $order_id ) {
	return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) );
}

WooCommerce Subscriptions の2回目以降の定期購入時のフック

2つ有りますが、基本的に決済方法によって異なる処理が多いので、決済手段に特化した処理の方を利用することが多いと思います。
ただ、すべての定期購入に関して何か特別な処理をしたい時は、2つ目のフックを利用するのが良いです。

定期購入の決済手段に特化した際に利用するフック

Action: woocommerce_scheduled_subscription_payment_{$gateway_id}
パラメーターは2つあります。
$renewal_total : 「数値(float)」で定期購入金額なります。
$renewal_order : WC_Orderのオブジェクトになります。

定期購入の際に利用できるフック

Action: woocommerce_scheduled_subscription_payment
パラメーターは一つです。
$subscription->get_id() : 定期購入として保存された注文の Order ID です。

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?