当定制订单更改时发送定制WooCommerce电子邮件

时间:2019-12-07 作者:Emily

当订单状态更改时,我面临发送自定义电子邮件的问题。

我创建了一封自定义WooCommerce电子邮件“WC\\u Ready\\u To\\u Ship\\u电子邮件”和一封自定义WooCommerce订单“WC Ready To Ship”。

我试图实现的是,当状态订单更改为“准备发货”时,将向客户发送一封电子邮件。

这是我插件文件中的代码

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;
}
这是class-wc-ready-to-ship-email.php 文件

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
WooCommerce->设置->电子邮件上的一切似乎都正常,日志文件中没有错误。

你能帮我找到一个发送电子邮件的方法吗!

1 个回复
SO网友:iv.draganov

我认为你需要做两个改变:

将筛选器添加到woocommerce_email_actions 这将添加触发通知的动作挂钩:

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\' );
<在email类中更改触发器挂钩(添加_notification 后缀):
// Trigger on new paid orders
add_action( \'woocommerce_order_status_ready-to-ship_notification\', array( $this, \'trigger\', 10, 2 ) );