当Woocommerce订单使用优惠码时发送电子邮件通知

4
如何在特定优惠券被使用时向商业伙伴发送订单通知?
我发现了一个解决方案,当优惠券应用时可以发送电子邮件通知,链接如下: 在WooCommerce中应用特定优惠码时发送电子邮件通知 但是,我需要找到一个解决方案,当订单提交后再发送通知,因为并不总是在应用优惠券后立即提交订单。
每个优惠券都将有自己的电子邮件地址。
1个回答

3

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

// 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以来,适用于所有WooCommerce版本。
// 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文件中。已测试并可行。

在这里输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接