我正在编写插件,为每个在商店花费的X发送优惠券代码,这个功能非常好但我在向新注册用户发送优惠券时遇到了麻烦。 发送邮件功能(此处有意XYZ):
function sendFirstVoucher($to_email, $discount, $name, $coupon){
$headers = 'From: XYZ' . "";
$subject = 'Your ' . $discount . '% discount voucher for XYZ';
$message = 'Hi . ' . $name . 'and thank you for registering with XYZ! \r\n\r\n' .
"blah blah blah " . $coupon . " when you check out on our website, you can apply the code in either the cart page or check out page. \r\n\r\n" .
"From now on when you make purchases you will also receive additional discount codes for every $100 spent. More information can be found at our website (link to membership program page). \r\n" .
"If you have any questions please feel free to contact us at XYZ \r\n\r\n" .
"Kind Regards, \r\nThe XYZ";
wp_mail($to_email, $subject, $message, $headers);
}
在主插件文件中:
function send_coupon_to_freshly_registered_user($customer_id, $new_customer_data, $password_generated) {
$coupon_code = generateCoupon(5);
$user_email = $new_customer_data["user_email"];
$user_login = $new_customer_data["user_login"];
sendFirstVoucher($user_email, "5", $user_login, $coupon_code);
}
最后在文件的近端有:
add_action('woocommerce_created_customer', 'send_coupon_to_freshly_registered_user',10,3);
但是,一旦我通过仪表板创建用户或通过wp-login页面注册自己(因为网站处于维护模式),它不会发送任何电子邮件或创建优惠券(它适用于X凭证的类似功能) $ 100)
有谁知道如何完成这个?
亲切的问候, 汤姆
答案 0 :(得分:1)
好的,我通过使用核心wp函数实现了这个目标:
add_action('user_register', 'send_coupon_to_freshly_registered_user',10,1);
function send_coupon_to_freshly_registered_user($user_id) {
$user = get_user_by('id',$user_id); //new line
$user_login = stripslashes($user->user_login); //changed line
$user_email = stripslashes($user->user_email); //changed line
$coupon_code = generateCoupon(5);
sendFirstVoucher($user_email, "5", $user_login, $coupon_code);
}
我希望它会帮助别人。也许这不是一个真正的答案,而是一个适合我的答案。
问候,
汤姆