Send a custom woocommerce email when custom order change

Question

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
, , , , emily 3 years 2019-12-07T10:58:20-05:00 0 Answers 105 views 0

Leave an answer

Browse
Browse