概要
woocommerce_order_is_paid
アクションは、WooCommerceで注文が支払われた際にトリガーされるフックです。このアクションは、特定の処理を行いたい場合に利用されます。たとえば、支払いが成功したときにメールを送信したり、在庫管理を行ったりするのに適しています。以下のような機能を実装する際に便利です。
- 支払い確認後の注文ステータスの更新
- 顧客へのサンキューメールの送信
- 在庫の減少処理
- アナリティクスへのデータ送信
- サードパーティのリマインダーシステムとの統合
- クーポンの発行や適用
構文
add_action('woocommerce_order_is_paid', 'your_function_name', 10, 1);
パラメータ
$order
: 支払われたWooCommerceの注文オブジェクト。
戻り値
- なし。アクションフックなので、何かを返すことはありません。
使用可能なプラグインWooCommerceのバージョン
- WooCommerce 3.0.0以上で利用可能。
ワードプレスのバージョン
- 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_action('woocommerce_order_is_paid', 'send_order_confirmation_email');
function send_order_confirmation_email($order) {
$to = $order->get_billing_email();
$subject = 'ご注文ありがとうございます!';
$message = 'ご注文が正常に処理されました。';
wp_mail($to, $subject, $message);
}
このコードは、注文が支払われた際に顧客へ確認メールを送信します。
サンプルコード 2: 在庫の更新
add_action('woocommerce_order_is_paid', 'update_inventory_on_payment');
function update_inventory_on_payment($order) {
foreach ($order->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
$quantity = $item->get_quantity();
$product = wc_get_product($product_id);
$product->reduce_stock_quantity($quantity);
}
}
このコードは、支払い後に商品在庫を減少させます。
サンプルコード 3: アナリティクスへのデータ送信
add_action('woocommerce_order_is_paid', 'send_data_to_analytics');
function send_data_to_analytics($order) {
$data = [
'order_id' => $order->get_id(),
'total' => $order->get_total(),
'currency' => $order->get_currency(),
];
// ここでアナリティクスAPIにデータを送信
}
このコードは、支払い後に注文データをアナリティクスのAPIに送信します。
サンプルコード 4: サードパーティサービスとの統合
add_action('woocommerce_order_is_paid', 'integrate_with_third_party_service');
function integrate_with_third_party_service($order) {
$response = wp_remote_post('https://third-party-service.com/api', [
'body' => json_encode(['order_id' => $order->get_id()]),
'headers' => ['Content-Type' => 'application/json'],
]);
}
このコードは、支払いが完了した際にサードパーティサービスにリクエストを送信します。
サンプルコード 5: クーポン適用
add_action('woocommerce_order_is_paid', 'apply_discount_coupon');
function apply_discount_coupon($order) {
if (!$order->has_discount('sample_coupon')) {
$order->apply_coupon('sample_coupon');
$order->calculate_totals();
}
}
このコードは、支払い後に特定のクーポンを適用し、合計を再計算します。