Send an email notification to customer when a specific coupon code is applied in WooCommerce(在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知)
本文介绍了在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在顾客结账时使用了特定的促销代码‘FREECLASS’后,我正在尝试向他发送电子邮件。
我所做的是在注册后向客户发送代码‘FREECLASS’。我希望客户在使用该代码后收到额外的自定义消息。
基于Send an email notification when a specific coupon code is applied in WooCommerce答案代码,这是我到目前为止所做的,但它不起作用。
add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon', 10, 1 );
function custom_email_on_applied_coupon( $coupon_code ){
if( $coupon_code == 'FREECLASS' ){
// Get user billing email
global $user_login;
$user = get_user_by('login', $user_login );
$email = $user->billing_email;
$to = "$email"; // Recipient
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied'), $coupon_code );
wp_mail( $to, $subject, $content );
}
}
这是我的第一个WooCommerce项目,如果能得到一些帮助,我将不胜感激。
推荐答案
无需使用全局变量,通过$user_id
即可获取WC_Customer实例对象,然后获取计费邮箱地址
- wp_mail()-发送电子邮件,类似于PHP的mail函数
因此您得到:
function action_woocommerce_applied_coupon( $coupon_code ) {
// NOT logged in, return
if ( ! is_user_logged_in() ) return;
// Compare
if ( $coupon_code == 'freeclass' ) {
// Get user ID
$user_id = get_current_user_id();
// Get the WC_Customer instance Object
$customer = New WC_Customer( $user_id );
// Billing email
$email = $customer->get_billing_email();
// NOT empty
if ( ! empty ( $email ) ) {
// Recipient
$to = $email;
$subject = sprintf( __('Coupon "%s" has been applied', 'woocommerce' ), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer', 'woocommerce' ), $coupon_code );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
wp_mail( $to, $subject, $content, $headers );
}
}
}
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 );
这篇关于在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:在WooCommerce中应用特定优惠券代码时向客户发送电


基础教程推荐
猜你喜欢
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01