在WooCommerce订单上使用优惠券代码时发送电子邮件通知

阿杰·辛格(Ajay Singh):

使用特定优惠券时,如何将订单通知发送给业务伙伴?

当在此处应用优惠券时,我找到了实例的解决方案:在WooCommerce中应用了特定优惠券代码时,发送电子邮件通知

但是,我需要为提交订单后的时间找到解决方案,因为在应用优惠券后订单并不总是提交。

每个优惠券都有自己的电子邮件地址。

LoicTheAztec:

首先,我们在管理优惠券页面中添加一个设置字段,以设置优惠券的电子邮件收件人:

// Add a custom field to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_text_field', 10 );
function add_coupon_text_field() {
    woocommerce_wp_text_input( array(
        'id'                => 'email_recipient',
        'label'             => __( 'Email recipient', 'woocommerce' ),
        'placeholder'       => '',
        'description'       => __( 'Send an email notification to a defined recipient' ),
        'desc_tip'    => true, // Or false

    ) );
}

// Save the custom field value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_text_field', 10, 2 );
function save_coupon_text_field( $post_id, $coupon ) {
    if( isset( $_POST['email_recipient'] ) ) {
        $coupon->update_meta_data( 'email_recipient', sanitize_text_field( $_POST['email_recipient'] ) );
        $coupon->save();
    }
}

然后,如果已为应用的优惠券设置了电子邮件收件人,则会将每个应用的优惠券的电子邮件发送到提交的订单。

警告!仅选择以下功能之一:

对于woocommerce版本高达4.3 (新钩子)

// For Woocommerce version 4.3+
add_action( 'woocommerce_checkout_order_created', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order ){
    $used_coupons = $order->get_used_coupons();

    if( ! empty($used_coupons) ){
        foreach ( $used_coupons as $coupon_code ) {
            $coupon    = new WC_Coupon( $coupon_code ); // WC_Coupon Object
            $recipient = $coupon->get_meta('email_recipient'); // get recipient

            if( ! empty($recipient) ) {
                $subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
                $content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
                wp_mail( $recipient, $subject, $content ); // Send email
            }
        }
    }
}

或所有WooCommerce版本(从3.0版开始)

// For all Woocommerce versions (since 3.0)
add_action( 'woocommerce_checkout_update_order_meta', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order_id ){
    $order = wc_get_order( $order_id );

    $used_coupons = $order->get_used_coupons();

    if( ! empty($used_coupons) ){
        foreach ( $used_coupons as $coupon_code ) {
            $coupon    = new WC_Coupon( $coupon_code ); // WC_Coupon Object
            $recipient = $coupon->get_meta('email_recipient'); // get recipient

            if( ! empty($recipient) ) {
                $subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
                $content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
                wp_mail( $recipient, $subject, $content ); // Send email
            }
        }
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 WooCommerce 中应用特定优惠券代码时发送电子邮件通知

将优惠券代码名称添加到Woocommerce查看订单详细信息和电子邮件通知

如何在人们提交电子邮件时通过电子邮件发送优惠券代码

仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中

如何在新用户注册电子邮件模板中发送优惠券代码?

将应用的优惠券描述添加到 WooCommerce 管理员电子邮件通知

自动生成的优惠券未显示在订单电子邮件中

如何在 WooCommerce 中高效地从电子邮件限制中获取优惠券

WooCommerce:手动验证优惠券用户电子邮件

在Woocommerce中成功购买后更改优惠券电子邮件限制

WooCommerce-使用“ save_post”挂钩更新订单时发送通知电子邮件

当订单状态更改为自定义订单状态时,从WooCommerce发送电子邮件

在WooCommerce中自定义订单状态更改时发送电子邮件通知

在WooCommerce管理订单列表上显示使用的优惠券

避免在某些自动完成的订单上重复发送电子邮件通知

当订单状态从待定更改为取消时发送电子邮件通知

如果客户在 WooCommerce 的先前订单中使用过相关优惠券,则限制使用优惠券

如何让 IHostingService 在订单确认时发送电子邮件?

WooCommerce优惠券代码通配符

在WooCommerce Thankyou中发送订单确认电子邮件通知

Woocommerce - 如何以编程方式停止发送电子邮件通知(有时)

添加一栏,其中包含在Woocommerce中的“管理订单”列表上使用的优惠券

生成优惠券代码时混淆

在首次更新商品时发送电子邮件通知

当人偶运行失败时发送电子邮件通知

如何根据woocommerce订单中使用的优惠券更改订单状态

从WooCommerce订单中获取二手优惠券代码和相关折扣金额

发送电子邮件失败时的HTTP状态代码

从C#代码发送电子邮件时出错