2014-11-24 179 views
9

我添加了一個自定義狀態wc-order-confirmed發送自定義郵件:WooCommerce - 自定義訂單狀態變化

// Register new status 
function register_order_confirmed_order_status() { 
    register_post_status('wc-order-confirmed', array(
     'label' => 'Potvrzení objednávky', 
     'public' => true, 
     'exclude_from_search' => false, 
     'show_in_admin_all_list' => true, 
     'show_in_admin_status_list' => true, 
     'label_count' => _n_noop('Potvrzení objednávky <span class="count">(%s)</span>', 'Potvrzení objednávky <span class="count">(%s)</span>') 
    )); 
} 
add_action('init', 'register_order_confirmed_order_status'); 

// Add to list of WC Order statuses 
function add_order_confirmed_to_order_statuses($order_statuses) { 
    $new_order_statuses = array(); 
// add new order status after processing 
    foreach ($order_statuses as $key => $status) { 
     $new_order_statuses[ $key ] = $status; 
     if ('wc-processing' === $key) { 
      $new_order_statuses['wc-order-confirmed'] = 'Potvrzení objednávky'; 
     } 
    } 
    return $new_order_statuses; 
} 
add_filter('wc_order_statuses', 'add_order_confirmed_to_order_statuses'); 

我添加了一個自定義的電子郵件wc_confirmed_order

/** 
* A custom confirmed Order WooCommerce Email class 
* 
* @since 0.1 
* @extends \WC_Email 
*/ 
class WC_Confirmed_Order_Email extends WC_Email { 


    /** 
    * Set email defaults 
    * 
    * @since 0.1 
    */ 
    public function __construct() { 

     // set ID, this simply needs to be a unique name 
     $this->id = 'wc_confirmed_order'; 

     // this is the title in WooCommerce Email settings 
     $this->title = 'Potvrzení objednávky'; 

     // this is the description in WooCommerce email settings 
     $this->description = 'Confirmed Order Notification'; 

     // these are the default heading and subject lines that can be overridden using the settings 
     $this->heading = 'Potvrzení objednávky'; 
     $this->subject = 'Potvrzení objednávky'; 

     // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar 
     $this->template_html = 'emails/customer-confirmed-order.php'; 
     $this->template_plain = 'emails/plain/admin-new-order.php'; 

     // Trigger on confirmed orders 
     add_action('woocommerce_order_status_pending_to_order_confirmed', array($this, 'trigger')); 
     add_action('woocommerce_order_status_processing_to_order_confirmed', array($this, 'trigger')); 

     // Call parent constructor to load any other defaults not explicity defined here 
     parent::__construct(); 

     // this sets the recipient to the settings defined below in init_form_fields() 
     $this->recipient = $this->get_option('recipient'); 

     // if none was entered, just use the WP admin email as a fallback 
     if (! $this->recipient) 
      $this->recipient = get_option('admin_email'); 
    } 


    /** 
    * Determine if the email should actually be sent and setup email merge variables 
    * 
    * @since 0.1 
    * @param int $order_id 
    */ 
    public function trigger($order_id) { 
      // bail if no order ID is present 
     if (! $order_id) 
      return; 

     if ($order_id) { 
      $this->object  = wc_get_order($order_id); 
      $this->recipient = $this->object->billing_email; 

      $this->find['order-date']  = '{order_date}'; 
      $this->find['order-number'] = '{order_number}'; 

      $this->replace['order-date'] = date_i18n(wc_date_format(), strtotime($this->object->order_date)); 
      $this->replace['order-number'] = $this->object->get_order_number(); 
     } 


     if (! $this->is_enabled() || ! $this->get_recipient()) 
      return; 

     // woohoo, send the email! 
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); 
    } 


    /** 
    * get_content_html function. 
    * 
    * @since 0.1 
    * @return string 
    */ 
    public function get_content_html() { 
     ob_start(); 
     wc_get_template($this->template_html, array(
      'order'   => $this->object, 
      'email_heading' => $this->get_heading() 
     )); 
     return ob_get_clean(); 
    } 


    /** 
    * get_content_plain function. 
    * 
    * @since 0.1 
    * @return string 
    */ 
    public function get_content_plain() { 
     ob_start(); 
     wc_get_template($this->template_plain, array(
      'order'   => $this->object, 
      'email_heading' => $this->get_heading() 
     )); 
     return ob_get_clean(); 
    } 


    /** 
    * Initialize Settings Form Fields 
    * 
    * @since 2.0 
    */ 
    public function init_form_fields() { 

     $this->form_fields = array(
      'enabled' => array(
       'title' => 'Enable/Disable', 
       'type' => 'checkbox', 
       'label' => 'Enable this email notification', 
       'default' => 'yes' 
      ), 
      'recipient' => array(
       'title'  => 'Recipient(s)', 
       'type'  => 'text', 
       'description' => sprintf('Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', esc_attr(get_option('admin_email'))), 
       'placeholder' => '', 
       'default'  => '' 
      ), 
      'subject' => array(
       'title'  => 'Subject', 
       'type'  => 'text', 
       'description' => sprintf('This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject), 
       'placeholder' => '', 
       'default'  => '' 
      ), 
      'heading' => array(
       'title'  => 'Email Heading', 
       'type'  => 'text', 
       'description' => sprintf(__('This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.'), $this->heading), 
       'placeholder' => '', 
       'default'  => '' 
      ), 
      'email_type' => array(
       'title'  => 'Email type', 
       'type'  => 'select', 
       'description' => 'Choose which format of email to send.', 
       'default'  => 'html', 
       'class'  => 'email_type', 
       'options'  => array(
        'plain'  => __('Plain text', 'woocommerce'), 
        'html'  => __('HTML', 'woocommerce'), 
        'multipart' => __('Multipart', 'woocommerce'), 
       ) 
      ) 
     ); 
    } 


} // end \WC_confirmed_Order_Email class 

我可以看到郵件的電子郵件設置,以及訂單狀態下拉菜單中的狀態。現在,只要訂單狀態更改爲wc-order-confirmed,我就需要發送新的電子郵件。過渡鉤似乎永遠不會被解僱。

我也試過:

/** 
* Register the "woocommerce_order_status_pending_to_quote" hook which is necessary to 
* allow automatic email notifications when the order is changed to refunded. 
* 
* @modified from http://stackoverflow.com/a/26413223/2078474 to remove anonymous function 
*/ 
add_action('woocommerce_init', 'so_25353766_register_email'); 
function so_25353766_register_email(){ 
    add_action('woocommerce_order_status_pending_to_order_confirmed', array(WC(), 'send_transactional_email'), 10, 10); 
    add_action('woocommerce_order_status_processing_to_order_confirmed', array(WC(), 'send_transactional_email'), 10, 10); 
} 

也似乎哪個不是在所有的工作...任何想法,請?

回答

10

正如Xcid的答案所示,您需要註冊電子郵件。

在WC 2.2+我相信你可以通過做以下幾點:

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

我已經添加了一個過濾器,WooCommerce 2.3,所以當所散發出來的自定義郵件就能被添加到電子郵件操作的列表WooCommerce登記:

// 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'); 
12

你需要的掛鉤是:

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

最簡單的解決方案。謝謝 – 2016-12-20 09:14:45

+0

掛鉤是正確的和工作。即使訂單狀態剛創建爲「等待」,您也可以發送任何狀態更改的郵件。 wc() - >郵件程序正在使用php的郵件類,所以你甚至可以使用wp_mail函數發送郵件和添加附件等,如果你知道如何發送郵件與PHP腳本很容易使用。 – 2017-08-27 14:52:09

4

正如你可以在這裏看到: https://github.com/woothemes/woocommerce/blob/f8a161c40673cb019eb96b04c04a774ca040a15a/includes/abstracts/abstract-wc-order.php#L2097 你可以用這個鉤子:

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

與您自定義狀態應該給:

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

我想,你還可以添加你自定義的電子郵件給寄件人,如果沒有:

只需添加:

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

編輯:

您需要到instanciate woocommerce電子郵件之前,所以你可以添加

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

謝謝,這看起來很有希望!加入WC() - > mailer()是什麼意思? – user1049961 2014-11-24 21:26:27

+0

WC() - >郵件程序()是一個內部的woocommerce函數,可以實現woocommerce電子郵件服務,換句話說,函數將實例化您的自定義類。 – XciD 2014-11-24 21:29:04

+0

是的,我明白WC() - >郵件()應instanciate我的課,但我不知道該把它放在哪裏?你的意思是把它添加到'woocommerce_order_status_wc-order-confirmed'動作或者在哪裏? – user1049961 2014-11-24 21:46:14

1

你可以嘗試觀看時訂單狀態更改,以便把這個成的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"); 

此功能將觸發您的電子郵件,並進行發送。

相關問題