最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Send a custom notification to customer on WooCommerce cancelled order status

programmeradmin2浏览0评论

In WooCommerce, I am trying to send an email notification to the customer, when an order has a status "cancelled".

Here is my code placed in my child theme's function.php file:

add_filter('woocommerce_order_status_changed', 'send_mail_abandonned_order', 10, 4);
function woocommerce_send_mail_abandonned_order($order, $new_status)
{
    if (!$order ||  $new_status != 'cancelled') return;
    print("Entering in function"); //nothing append
    throw new("error"); // Nothing append  
    //$order = new WC_Order($order_id);
    $order = wc_get_order($order);
    if ( $new_status == 'cancelled' ) {
        //$customer_email = $order->get_billing_email(); // The customer email
        ob_start();
        include( 'email/mail.php' );
        $message =  ob_get_clean();
        define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));
        // Get woocommerce mailer from instance
        $mailer = WC()->mailer();
        // Wrap message using woocommerce html email template
        $wrapped_message = $mailer->wrap_message("Vous n'avez pas oublié quelque chose ?", $message);
        // Create new WC_Email instance
        $wc_email = new WC_Email;
        // Style the wrapped message with woocommerce inline styles
        $html_message = $wc_email->style_inline($wrapped_message);
        // Send the email using wordpress mail function
        wp_mail( "[email protected]", 'Oups Vous n\'avez pas oubliez quelque chose ?', $html_message, HTML_EMAIL_HEADERS );
    } 
}

But I don't know why this hook is not working properly. When I change the order status to cancelled and try to throw an error, nothing is appended to my php errors logs. It seems that this hook is not working.

How to solve this issue?

In WooCommerce, I am trying to send an email notification to the customer, when an order has a status "cancelled".

Here is my code placed in my child theme's function.php file:

add_filter('woocommerce_order_status_changed', 'send_mail_abandonned_order', 10, 4);
function woocommerce_send_mail_abandonned_order($order, $new_status)
{
    if (!$order ||  $new_status != 'cancelled') return;
    print("Entering in function"); //nothing append
    throw new("error"); // Nothing append  
    //$order = new WC_Order($order_id);
    $order = wc_get_order($order);
    if ( $new_status == 'cancelled' ) {
        //$customer_email = $order->get_billing_email(); // The customer email
        ob_start();
        include( 'email/mail.php' );
        $message =  ob_get_clean();
        define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));
        // Get woocommerce mailer from instance
        $mailer = WC()->mailer();
        // Wrap message using woocommerce html email template
        $wrapped_message = $mailer->wrap_message("Vous n'avez pas oublié quelque chose ?", $message);
        // Create new WC_Email instance
        $wc_email = new WC_Email;
        // Style the wrapped message with woocommerce inline styles
        $html_message = $wc_email->style_inline($wrapped_message);
        // Send the email using wordpress mail function
        wp_mail( "[email protected]", 'Oups Vous n\'avez pas oubliez quelque chose ?', $html_message, HTML_EMAIL_HEADERS );
    } 
}

But I don't know why this hook is not working properly. When I change the order status to cancelled and try to throw an error, nothing is appended to my php errors logs. It seems that this hook is not working.

How to solve this issue?

Share Improve this question edited May 10, 2019 at 10:05 Oliver asked May 6, 2019 at 13:31 OliverOliver 152 silver badges11 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 0

A better solution will be to use all arguments included in woocommerce_order_status_cancelled hooked function, so the missing $order variable.

Also as the action hook woocommerce_order_status_cancelled is triggered when order status change to "cancelled", so in your code, if ($order->has_status('cancelled')) { is not needed.

Then your code will be:

add_filter('woocommerce_order_status_cancelled', 'woocommerce_send_mail_abandonned_order', 10, 2 );
function woocommerce_send_mail_abandonned_order( $order_id, $order )
{
    define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));

    // Get WooCommerce mailer from instance
    $mailer = WC()->mailer();

    ob_start();
    include( 'email/mail.php' );
    $message = ob_get_clean();

    // Wrap message using woocommerce html email template
    $wrapped_message = $mailer->wrap_message("Vous n'avez pas oublié quelque chose ?", $message);

    // Create new WC_Email instance
    $wc_email = new WC_Email;

    // Style the wrapped message with woocommerce inline styles
    $html_message = $wc_email->style_inline($wrapped_message);

    // Send the email using wordpress mail function
    wp_mail( $order->get_billing_email(), 'Oups Vous n\'avez pas oubliez quelque chose ?', $html_message, HTML_EMAIL_HEADERS );
}

Or with woocommerce_order_status_changed hook (your question hook):

add_filter('woocommerce_order_status_changed', 'woocommerce_send_mail_abandonned_order', 10, 4 );
function woocommerce_send_mail_abandonned_order(  $order_id, $old_status, $new_status, $order )
{
    if ( $new_status === 'cancelled') 
    {
        define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));

        // Get WooCommerce mailer from instance
        $mailer = WC()->mailer();

        ob_start();
        include( 'email/mail.php' );
        $message = ob_get_clean();

        // Wrap message using woocommerce html email template
        $wrapped_message = $mailer->wrap_message("Vous n'avez pas oublié quelque chose ?", $message);

        // Create new WC_Email instance
        $wc_email = new WC_Email;

        // Style the wrapped message with woocommerce inline styles
        $html_message = $wc_email->style_inline($wrapped_message);

        // Send the email using wordpress mail function
        wp_mail( $order->get_billing_email(), 'Oups Vous n\'avez pas oubliez quelque chose ?', $html_message, HTML_EMAIL_HEADERS );
    }
}

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

Here is the solution...


// CHANGE THE HOOK DESCRIPTION
add_filter('woocommerce_order_status_cancelled', 'woocommerce_send_mail_abandonned_order',21, 1);
function woocommerce_send_mail_abandonned_order($order_id)
{
    if (!$order_id) return;
    $order = wc_get_order($order_id);
    if ($order->has_status('cancelled')) {
        //$customer_email = $order->get_billing_email(); // The customer email
        ob_start();
        include( 'email/mail.php' );
        $message =  ob_get_clean();
        define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));
        // Get woocommerce mailer from instance
        $mailer = WC()->mailer();
        // Wrap message using woocommerce html email template
        $wrapped_message = $mailer->wrap_message("Vous n'avez pas oublié quelque chose ?", $message);
        // Create new WC_Email instance
        $wc_email = new WC_Email;
        // Style the wrapped message with woocommerce inline styles
        $html_message = $wc_email->style_inline($wrapped_message);
        // Send the email using wordpress mail function
        wp_mail( "[email protected]", 'Oups Vous n\'avez pas oubliez quelque chose ?', $html_message, HTML_EMAIL_HEADERS );
    } 
}

Change $order by $order_id too

发布评论

评论列表(0)

  1. 暂无评论