1

I am facing a problem to send a custom email when order status changes.

I created a custom WooCommerce email 'WC_Ready_To_Ship_`Email' and a custom WooCommerce order 'wc-ready-to-ship'.

What I am trying to achieve is when the status order changes to 'ready to ship', an email will be sent to the customer.

Here is my code in my plugin file

function plugin_init () {
  if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    // Register new order status to woocommerce
    add_action( 'init', 'plugin_register_custom_status' );
    // Add new order status to woocommerce
    add_filter( 'wc_order_statuses', 'plugin_add_status' );
    // Create custom email for woocommerce
    add_filter( 'woocommerce_email_classes', 'add_ready_to_ship_woocommerce_emails' );
  }
}
add_action('plugins_loaded', 'plugin_init');

// Create custom email for woocommerce
function add_ready_to_ship_woocommerce_emails( $email_classes ) {
  require( 'includes/class-wc-ready-to-ship-email.php' );
  $email_classes['WC_Ready_To_Ship_Email'] = new WC_Ready_To_Ship_Email();
  return $email_classes;
}

and here is the class-wc-ready-to-ship-email.php file

class WC_Ready_To_Ship_Email extends WC_Email {
    public function __construct() {
        $this->id = 'wc_ready_to_ship_email';
        $this->customer_email = true;
        $this->title = 'Ready to ship';
        $this->description = '';

       $this->heading = 'Order Status';
       $this->subject = 'Ready to ship';

       $this->template_html  = 'emails/wc-customer-ready-to-ship.php';
       $this->template_plain = 'emails/plain/wc-customer-ready-to-ship.php';
       $this->template_base  = PLUGIN_PATH . 'templates/';
       $this->placeholders   = array(
            '{order_date}'   => '',
            '{order_number}' => '',
        );
       // Trigger on new paid orders
       add_action( 'woocommerce_order_status_ready-to-ship', array( $this, 'trigger', 10, 2 ) );
       parent::__construct();
  }

  public function trigger( $order_id, $order = false ) {
        $this->setup_locale();

        if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
            $order = wc_get_order( $order_id );
        }

        if ( is_a( $order, 'WC_Order' ) ) {
            $this->object                         = $order;
            $this->recipient                      = $this->object->get_billing_email();
            $this->placeholders['{order_date}']   = wc_format_datetime( $this->object->get_date_created() );
            $this->placeholders['{order_number}'] = $this->object->get_order_number();
        }

        if ( $this->is_enabled() && $this->get_recipient() ) {
            $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
        }

        $this->restore_locale();
  }

  public function get_content_html() {
      return wc_get_template_html( $this->template_html, array(
            'order'              => $this->object,
            'email_heading'      => $this->get_heading(),
            'sent_to_admin'      => false,
            'plain_text'         => false,
            'email'              => $this,
        ), '', $this->template_base );
  }

  public function get_content_plain() {
    return wc_get_template_html( $this->template_plain, array(
            'order'              => $this->object,
            'email_heading'      => $this->get_heading(),
            'sent_to_admin'      => false,
            'plain_text'         => true,
            'email'              => $this,
        ), '', $this->template_base );
  }

} // end WC_Ready_To_Ship_Email class

Everything seems to be OK on WooCommerce -> Settings -> Emails and there is no error in log files.

Can you help me to find a way to send the email, please!

0

I think you need do two changes:

  1. Add a filter to woocommerce_email_actions that adds the action hook which should trigger the notification:
function add_ready_to_ship_woocommerce_email_action( $email_actions ) {
  $email_actions[] = 'woocommerce_order_status_ready-to-ship';
  return $email_actions;
}

add_filter( 'woocommerce_email_actions', 'add_ready_to_ship_woocommerce_email_action' );
  1. Change the trigger hook in the email class (add a _notification suffix):
// Trigger on new paid orders
add_action( 'woocommerce_order_status_ready-to-ship_notification', array( $this, 'trigger', 10, 2 ) );

Not the answer you're looking for? Browse other questions tagged or ask your own question.