WooCommerce - send custom email on custom order status change

后端 未结 4 742
轻奢々
轻奢々 2020-11-30 05:00

I added a custom status wc-order-confirmed:

// Register new status
function register_order_confirmed_order_status() {
    register_post_status(          


        
相关标签:
4条回答
  • 2020-11-30 05:35

    As you can see here : https://github.com/woothemes/woocommerce/blob/f8a161c40673cb019eb96b04c04a774ca040a15a/includes/abstracts/abstract-wc-order.php#L2097 you can use this hook :

    do_action( 'woocommerce_order_status_' . $new_status, $this->id );

    with you custom status should give :

    add_action( 'woocommerce_order_status_wc-order-confirmed' , array( $this, 'trigger' ) );

    I imagine that you also add you custom email to the mailer, if not :

    just add :

    add_filter( 'woocommerce_email_classes', array($this,'edit_woocommerce_email_classes' ));
    function edit_woocommerce_email_classes( $email_classes ) {
        require_once( 'your-email-class.php' );
        $email_classes[ 'WC_Confirmed_Order_Email' ] = new WC_Confirmed_Order_Email();
        return $email_classes;
     }
    

    Edit :

    You need to instanciate woocommerce emails before so you can add

    add_action( 'init' , 'initiate_woocommerce_email' );
    
    function initiate_woocommerce_email(){
       // Just when you update the order_status on backoffice
       if( isset($_POST['order_status']) ) {
            WC()->mailer();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:35

    You can try to watch when the order status changed so put this into functions.php:

    function confirmed_notifications($order_id, $checkout=null) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
    
        if( $order->status === 'order-confirmed' ) {
    
            // Trigger transactional email to client
            $email = $mailer->emails['WC_Confirmed_Order_Email'];
            $email->trigger( $order_id );
        }
    }
    add_action("woocommerce_order_status_changed", "confirmed_notifications");
    

    This function will trigger your email and send it.

    0 讨论(0)
  • 2020-11-30 05:42

    The hook you need is:

    woocommerce_order_status_changed

    add_action("woocommerce_order_status_changed", "my_awesome_publication_notification");
    
    function my_awesome_publication_notification($order_id, $checkout=null) {
       global $woocommerce;
       $order = new WC_Order( $order_id );
       if($order->status === 'completed' ) {
          // 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 );
         }
    
       }
    }
    
    0 讨论(0)
  • 2020-11-30 05:59

    As Xcid's answer indicates, you need to register the email.

    In WC 2.2+ I believe you can do this via the following:

    add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
    

    I'd added a filter to WooCommerce 2.3, so when that comes out custom emails will be able to be added to the list of email actions that WooCommerce registers:

    // As of WooCommerce 2.3
    function so_27112461_woocommerce_email_actions( $actions ){
        $actions[] = 'woocommerce_order_status_wc-order-confirmed';
        return $actions;
    }
    add_filter( 'woocommerce_email_actions', 'so_27112461_woocommerce_email_actions' );
    
    0 讨论(0)
提交回复
热议问题