概要
woocommerce_new_customer
アクションは、WooCommerce で新しい顧客アカウントが作成されたときにトリガーされるフックです。このアクションを使用することで、顧客の登録時に特定の処理を行うことができます。例えば、以下のような機能を実装する際によく使われます。
- 新規顧客にウェルカムメールを送信する
- 顧客データを外部サービスに送信する
- 特定の条件に基づいて顧客をグループ化する
- ユーザー登録時にカスタムフィールドを追加する
- 登録された顧客の情報をログに記録する
- 新規顧客に特典やクーポンを提供する
構文
add_action('woocommerce_new_customer', 'function_name', 10, 1);
パラメータ
user_id
: 新規顧客のユーザー ID(整数型)
戻り値
このアクションは値を戻しません。
対応プラグインおよびバージョン
- WooCommerce バージョン: 3.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_new_customer', 'send_welcome_email', 10, 1);
function send_welcome_email($user_id) {
$user_info = get_userdata($user_id);
$email = $user_info->user_email;
$subject = 'Welcome to our store!';
$message = 'Thank you for registering!';
wp_mail($email, $subject, $message);
}
引用元: https://developer.wordpress.org/reference/functions/wp_mail/
サンプルコード2: 外部サービスへのデータ送信
新規顧客のデータを外部APIに送信するサンプルコードです。
add_action('woocommerce_new_customer', 'send_customer_data_to_external_service', 10, 1);
function send_customer_data_to_external_service($user_id) {
$user_info = get_userdata($user_id);
$data = array('email' => $user_info->user_email);
wp_remote_post('https://example.com/api/customers', array('body' => json_encode($data)));
}
引用元: https://developer.wordpress.org/reference/functions/wp_remote_post/
サンプルコード3: 特典の付与
新規顧客に特典を付与する処理を行うサンプルコードです。
add_action('woocommerce_new_customer', 'give_bonus_points', 10, 1);
function give_bonus_points($user_id) {
// 特典ポイントを顧客に付与する処理
update_user_meta($user_id, 'bonus_points', 100);
}
引用元: https://developer.wordpress.org/reference/functions/update_user_meta/
サンプルコード4: 顧客ログの記録
新規顧客が登録した際に、ログに記録するサンプルコードです。
add_action('woocommerce_new_customer', 'log_new_customer', 10, 1);
function log_new_customer($user_id) {
$user_info = get_userdata($user_id);
error_log('New customer registered: ' . $user_info->user_email);
}
引用元: https://developer.wordpress.org/reference/functions/error_log/
サンプルコード5: カスタムフィールドの追加
新規顧客登録時にカスタムフィールドを追加するサンプルコードです。
add_action('woocommerce_new_customer', 'add_custom_user_meta', 10, 1);
function add_custom_user_meta($user_id) {
add_user_meta($user_id, 'preferred_contact_method', 'email');
}
引用元: https://developer.wordpress.org/reference/functions/add_user_meta/