概要
woocommerce_payment_complete_order_status フィルタは、WooCommerce での注文の支払い完了のステータスを変更するためのフックです。このフィルタを使用することで、特定の条件に基づいて支払いが完了した後の注文のステータスをカスタマイズできます。この機能は通常、次のような場面で使用されます。
- 特定の支払いゲートウェイに基づいて異なるステータスを設定する
- クーポンや割引が適用された場合の注文ステータスを変更する
- サブスクリプションが関連する場合に新しいステータスを設定する
- 顧客の地域や国に応じたカスタムステータスを適用する
- 商品の在庫状況に基づいてステータスを変更する
- チェックアウト時のユーザーの選択肢によって異なるステータスを設定する
構文
add_filter('woocommerce_payment_complete_order_status', 'your_function_name', 10, 2);
パラメータ
$status(string): 現在の注文の支払い完了ステータス。$order_id(int): 完了した支払いの注文 ID。
戻り値
- 変更したいステータスを返す(string)。
WooCommerce バージョン
- このフィルタは WooCommerce バージョン 2.0 以降で使用可能です。
WordPress バージョン
- WordPress バージョン 4.0 以降で使用可能です。
この関数のアクションでの使用可能性
| アクション | 使用可能性 |
|---|---|
| mu_plugin_loaded | |
| registered_post_type | |
| plugins_loaded | |
| wp_roles_init | |
| setup_theme | |
| after_setup_theme | |
| set_current_user | |
| init | |
| register_sidebar | |
| wp_loaded | |
| send_headers | |
| parse_query | |
| pre_get_posts | |
| wp | |
| template_redirect | |
| get_header | |
| wp_head |
サンプルコード
サンプルコード 1
add_filter('woocommerce_payment_complete_order_status', 'change_order_status_on_gateway', 10, 2);
function change_order_status_on_gateway($status, $order_id) {
$order = wc_get_order($order_id);
if ($order->get_payment_method() === 'stripe') {
return 'completed';
}
return $status;
}
このコードは、支払い方法が「Stripe」である場合に、注文の支払い完了ステータスを「completed」に変更します。引用元: https://woocommerce.com/
サンプルコード 2
add_filter('woocommerce_payment_complete_order_status', 'custom_order_status_based_on_coupon', 10, 2);
function custom_order_status_based_on_coupon($status, $order_id) {
$order = wc_get_order($order_id);
if ($order->get_used_coupons()) {
return 'on-hold';
}
return $status;
}
このコードは、もしクーポンが使用されている場合、注文の支払い完了ステータスを「on-hold」に変更します。引用元: https://developer.wordpress.org/
サンプルコード 3
add_filter('woocommerce_payment_complete_order_status', 'set_subscription_order_status', 10, 2);
function set_subscription_order_status($status, $order_id) {
$order = wc_get_order($order_id);
if ($order->is_subscription()) {
return 'pending';
}
return $status;
}
このコードは、もし注文がサブスクリプション関連の場合、支払い完了ステータスを「pending」に変更します。引用元: https://woocommerce.com/
サンプルコード 4
add_filter('woocommerce_payment_complete_order_status', 'change_status_based_on_region', 10, 2);
function change_status_based_on_region($status, $order_id) {
$order = wc_get_order($order_id);
if ($order->get_shipping_country() === 'JP') {
return 'completed';
}
return $status;
}
このコードは、日本からの注文に対して支払い完了ステータスを「completed」に変更します。引用元: https://docs.woocommerce.com/
サンプルコード 5
add_filter('woocommerce_payment_complete_order_status', 'stock_based_order_status', 10, 2);
function stock_based_order_status($status, $order_id) {
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product = $item->get_product();
if (!$product->is_in_stock()) {
return 'cancelled';
}
}
return $status;
}
このコードは、注文内のいずれかのアイテムが在庫切れの場合、支払い完了ステータスを「cancelled」に変更します。引用元: https://woocommerce.wordpress.com/