WP SITES

3086 Coded Tutorials & 288 Plugins

Send customer email when order is manually cancelled or refunded by admin

The following code snippets enable you to send a email to the customer when an order or cancelled or refunded.

When you need to send a customer email for a refund, use this code.

add_action('woocommerce_order_refunded', function ($order_id, $refund_id) {
    $order = wc_get_order($order_id);
    if (!$order) {
        return;
    }

    $to = $order->get_billing_email();
    if (empty($to)) {
        return;
    }

    $subject = 'Your order has been refunded';
    $headers = ['Content-Type: text/html; charset=UTF-8'];

    $message  = '<p>Dear ' . esc_html($order->get_billing_first_name()) . ',</p>';
    $message .= '<p>Your order #' . esc_html($order->get_order_number()) . ' has been refunded.</p>';
    $message .= '<p>If you have any questions, feel free to contact us.</p>';
    $message .= '<p>Thank you,<br>' . get_bloginfo('name') . '</p>';

    wp_mail($to, $subject, $message, $headers);
}, 10, 2);

Use the following code to send a customer email when the order is manually cancelled.

add_action('woocommerce_order_status_cancelled', function ($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) {
        return;
    }

    $to = $order->get_billing_email();
    if (empty($to)) {
        return;
    }

    $subject = 'Your order has been cancelled';
    $headers = ['Content-Type: text/html; charset=UTF-8'];

    $message  = '<p>Dear ' . esc_html($order->get_billing_first_name()) . ',</p>';
    $message .= '<p>Your order #' . esc_html($order->get_order_number()) . ' has been cancelled.</p>';
    $message .= '<p>If you have any questions, feel free to contact us.</p>';
    $message .= '<p>Thank you,<br>' . get_bloginfo('name') . '</p>';

    wp_mail($to, $subject, $message, $headers);
});

Add the code to the end of your child themes functions file or custom code snippets plugin.

Modify the text within these p tags to anything you like :

<p>If you have any questions, feel free to contact us.</p>'

Example email :

Another option is to :

  1. Create your own custom email template
  2. Include all the customers order information:
  • Order number
  • Customer name
  • List of products with quantities and prices
  • Order total
// Disable admin cancellation email
add_filter('woocommerce_email_enabled_cancelled_order', '__return_false');

add_action('woocommerce_order_status_cancelled', function ($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) {
        return;
    }

    $to = $order->get_billing_email();
    if (empty($to)) {
        return;
    }

    $subject = sprintf('Order #%s has been cancelled', $order->get_order_number());
    $headers = array('Content-Type: text/html; charset=UTF-8');

    // Start building the email content
    $message = '<div style="background-color: #f7f7f7; padding: 20px; font-family: Arial, sans-serif;">';
    $message .= '<div style="background-color: #ffffff; padding: 20px; border-radius: 5px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">';
    
    // Header
    $message .= '<h1 style="color: #720eec; margin-bottom: 20px;">Order Cancelled</h1>';
    
    // Order details
    $message .= '<p>Dear ' . esc_html($order->get_billing_first_name()) . ',</p>';
    $message .= '<p>Your order #' . esc_html($order->get_order_number()) . ' has been cancelled.</p>';
    
    // Order items
    $message .= '<h2 style="color: #720eec; margin: 20px 0;">Order Details</h2>';
    $message .= '<table style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">';
    $message .= '<tr style="background-color: #f8f8f8;"><th style="padding: 10px; text-align: left;">Product</th><th style="padding: 10px; text-align: right;">Total</th></tr>';
    
    foreach ($order->get_items() as $item) {
        $message .= '<tr>';
        $message .= '<td style="padding: 10px; border-bottom: 1px solid #eee;">' . esc_html($item->get_name()) . ' × ' . esc_html($item->get_quantity()) . '</td>';
        $message .= '<td style="padding: 10px; border-bottom: 1px solid #eee; text-align: right;">' . wc_price($item->get_total()) . '</td>';
        $message .= '</tr>';
    }
    
    // Order totals
    $message .= '<tr><td colspan="2" style="padding: 10px;"></td></tr>';
    $message .= '<tr><td style="padding: 10px; text-align: right;"><strong>Total:</strong></td>';
    $message .= '<td style="padding: 10px; text-align: right;"><strong>' . wc_price($order->get_total()) . '</strong></td></tr>';
    $message .= '</table>';
    
    // Footer
    $message .= '<p>If you have any questions, please contact us.</p>';
    $message .= '<p>Thank you,<br>' . get_bloginfo('name') . '</p>';
    
    $message .= '</div></div>';

    wp_mail($to, $subject, $message, $headers);
});

Leave a Reply

New Plugins