Send an email notification when custom order status changes in WooCommerce

前端 未结 2 957
青春惊慌失措
青春惊慌失措 2021-01-07 07:14

I have created a custom order status in my WooCommerce called Back Order (wc-backorder):

/**
     * Add custom status to orde         


        
相关标签:
2条回答
  • 2021-01-07 07:52
    add_action("woocommerce_order_status_changed", "my_custom_notification");
    
    function my_custom_notification($order_id, $checkout=null) {
       global $woocommerce;
       $order = new WC_Order( $order_id );
       if($order->status === 'backorder' ) {
          // Create a mailer
          $mailer = $woocommerce->mailer();
    
          $message_body = __( 'Hello world!!!' );
    
          $message = $mailer->wrap_message(
            // Message head and message body.
            sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
    
          // Cliente email, email subject and message.
         $mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
         }
    
       }
    

    Try this

    0 讨论(0)
  • 2021-01-07 07:55

    - EDIT / UPDATE -

    As the code tutorial you are using is really outdated (2013) for this new mega major version 3.0+, this custom function hooked in woocommerce_order_status_changed action hook will do the job. So You will be able to send a customized Processing email notification, when order status is changed to your custom status.

    Here is that working and tested code for WC 3.0+:

    add_action('woocommerce_order_status_changed', 'backorder_status_custom_notification', 10, 4);
    function backorder_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
    
       if( $order->has_status( 'backorder' )) {
    
            // Getting all WC_emails objects
            $email_notifications = WC()->mailer()->get_emails();
    
            // Customizing Heading and subject In the WC_email processing Order object
            $email_notifications['WC_Email_Customer_Processing_Order']->heading = __('Your processing Back order','woocommerce');
            $email_notifications['WC_Email_Customer_Processing_Order']->subject = 'Your {site_title} processing Back order receipt from {order_date}';
    
            // Sending the customized email
            $email_notifications['WC_Email_Customer_Processing_Order']->trigger( $order_id );
        }
    
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.


    AS your custom status is wc-backorder, but not wc-order-confirmed, you just need to replace everywhere wc-order-confirmed by wc-backorder.

    To make it work, you will have to change the 2 last hooked functions this way:

    add_action( 'woocommerce_order_status_wc-backorder', array( WC(), 'send_transactional_email' ), 10, 1 );
    
    
    add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
    function filter_woocommerce_email_actions( $actions ){
        $actions[] = 'woocommerce_order_status_wc-backorder';
        return $actions;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This should work (I can't test it as there is no the code of your custom plugin).


    Reference source code: woocommerce_order_status_{$this->status_transition[to]} action hook

    0 讨论(0)
提交回复
热议问题