John Davidson

php - Add Applied Coupon Description to WooCommerce Admin Email notifications

0 comments
Message:


Trying to put the description of applied coupon into the "New Order" admin email with not success


So far trying to use this code I put into functions.php


add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {

// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;

foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
$coupon_desc[] = $coupon->get_description();

}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';

$coupon_des = reset($coupon_desc);
echo '<p>'.__( 'Coupon Description: ').$coupon_des.'<p>';
}
// For multiple coupons
}

Unless it doesn't produce any error, I can't see the coupon description into the "New Order" admin email


Somebody can help me? Thank you in advance



My Answer:
WooCommerce admin email notifications can be achieved by customizing the email template. Here's how you can add the applied coupon description to the WooCommerce admin email notifications:

1. Open your theme's functions.php file or create a custom plugin for adding custom code.

2. Add the following code snippet to retrieve the applied coupon description:

php
function add_coupon_description_to_admin_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $sent_to_admin ) {
$coupons = $order->get_used_coupons();
$coupon_description = '';

foreach ( $coupons as $coupon ) {
$coupon_object = new WC_Coupon( $coupon );
$coupon_description .= $coupon_object->get_description() . ', ';
}

// Remove the last comma and space
$coupon_description = rtrim( $coupon_description, ', ' );

echo '

Applied Coupon Description: ' . $coupon_description . '

';
}
}
add_action( 'woocommerce_email_before_order_table', 'add_coupon_description_to_admin_email', 10, 4 );


3. Save the changes and test by placing an order with a coupon applied. The admin email notifications should now display the applied coupon description.

This code snippet hooks into the `woocommerce_email_before_order_table` action to add the applied coupon description to the admin email notifications. It retrieves the used coupons from the order and gets the description for each coupon, then displays it in the email template.

Make sure to test this code on a staging site before implementing it on a live site to ensure compatibility with your theme and other plugins.

Rate this post

4 of 5 based on 1628 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us