Last active
September 26, 2022 08:52
-
-
Save kosvrouvas/32acc490cf560b19e56f4bb4e5b55a3a to your computer and use it in GitHub Desktop.
WooCommerce: Send a second email to customer if a coupon is applied with HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In theme's functions.php or plug-in code: | |
// wp_mail() sents emails only in plain text by default, here we add HTML support | |
function wp_set_content_type(){ | |
return "text/html"; | |
} | |
add_filter( 'wp_mail_content_type','wp_set_content_type' ); | |
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', 'woocommerce' ), $coupon_code ); | |
wp_mail( $to, $subject, $content ); | |
} | |
} | |
} | |
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment