4

How to send order notification to a business partner when a specific coupon is used?

I found a solution for the instance when the coupon is applied here : Send an email notification when a specific coupon code is applied in WooCommerce

However, I need to find a solution for when after order is submitted, because the order is not always submitted after coupon is applied.

Each coupon will have its own email address.

Programmer
  • 178
  • 1
  • 2
  • 15
Ajay Singh
  • 289
  • 1
  • 3
  • 10

1 Answers1

3

First we add a setting field to admin Coupon pages, to set an email recipient for for a coupon:

// 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();
    }
}

Then email is sent for each applied coupon to a submitted order, if the email recipient has been set for the applied coupon.

Caution! choose only one of the following functions:

For woocommerce versions Up to 4.3 (new hook)

// 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
            }
        }
    }
}

Or for all WooCommerce versions (since version 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
            }
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399