customization - Send a custom WooCommerce email when custom order change

Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this 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!

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this 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!

Share Improve this question edited Dec 10, 2019 at 14:37 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Dec 7, 2019 at 16:58 EmilyEmily 111 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 ) );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744920582a4601108.html

相关推荐

  • customization - Send a custom WooCommerce email when custom order change

    Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信