2017-06-16 105 views
2

前一段時間,我發佈了一個有關問題的問題,我需要發送不同的電子郵件,具體取決於訂購的產品屬於哪個存儲(自定義字段)。 因此,如果只訂購了屬於Storage1的產品,則只應發送Email1。如果訂購了一個屬於Storage1的產品和一個屬於Storage2的產品,則將發送包含product1的電子郵件1和包含產品2的電子郵件2。 對原來的問題的鏈接:Custom order emails depending on product meta dataWoocommerce - 自定義電子郵件類創建重複的電子郵件

這一切現在,但由於某種原因,我得到雙重電子郵件。如果我從Storage1訂購一件產品,我會將兩封電子郵件1發送到我的收件箱中...如果我訂購5,10或20種產品,我只收到一封額外的電子郵件並不重要,但這足夠了。

所以我想看看是否有人經歷過同樣的事情,他們是如何修復它的?

爲了清楚起見,我會再次在此處添加代碼。

我複製了class-wc-new-order並創建了兩個與原始完全相同的新類。我分別將ID和類名更改爲存儲1和存儲2。

我通過執行以下操作加載類:

//Add our custom class to WC email classes 
add_filter('woocommerce_email_classes', [ $this, 'custom_order_email_add_email_classes' ], 10, 1); 

function custom_order_email_add_email_classes($email_classes) { 

    require(CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage1.php'); 
    require(CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage2.php'); 

    $email_classes['WC_Email_New_Order_Storage1'] = new WC_Email_New_Order_Storage1(); 
    $email_classes['WC_Email_New_Order_Storage2'] = new WC_Email_New_Order_Storage2(); 

    return $email_classes; 
} 

然後我在兩個電子郵件類編輯the trigger() - 功能。 請注意,除了測試之外,我沒有更改任何觸發器操作,但每次都收到兩封電子郵件,無論觸發器操作是否處於活動狀態。如果我停用所有觸發器操作,我當然不會收到任何電子郵件。

public function trigger($order_id, $order = false) { 

    $trigger = false; 

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

    if (is_a($order, 'WC_Order')) { 
     $this->object     = $order; 
     $this->find['order-date']  = '{order_date}'; 
     $this->find['order-number'] = '{order_number}'; 
     $this->replace['order-date'] = wc_format_datetime($this->object->get_date_created()); 
     $this->replace['order-number'] = $this->object->get_order_number(); 

     $items = $order->get_items(); 

     foreach ($items as $item_id => $item) { 

      $product = $item->get_product(); 

      if ($product->get_meta('_product_storage') == 'storage2') {//storage1 in the other email class 
       $trigger = true; 
      } 
     } 
    } 

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

    if($trigger === true) { 
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); 
    } 
    else { 
     return; 
    } 
} 

爲了從Email1中篩選出Storage2產品,我不得不覆蓋email-order-details.php。此代碼僅在模板中的<tbody>下方輸入,我從order-email-items.php獲取用於默認顯示產品的代碼。

編輯電子郵件階details.php:

if ($email->id == 'new_order_storage1') { 

     $items = $order->get_items(); 
     foreach($items as $item_id => $item) { 

      $product = $item->get_product(); 

      if ($product->get_meta('_product_storage') == 'storage1') { 
     ?> 
       <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>"> 
        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php 

         // Show title/image etc 
         if ($show_image) { 
          echo apply_filters('woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ($product->get_image_id() ? current(wp_get_attachment_image_src($product->get_image_id(), 'thumbnail')) : wc_placeholder_img_src()) . '" alt="' . esc_attr__('Product image', 'woocommerce') . '" height="' . esc_attr($image_size[1]) . '" width="' . esc_attr($image_size[0]) . '" style="vertical-align:middle; margin-' . (is_rtl() ? 'left' : 'right') . ': 10px;" /></div>', $item); 
         } 

         // Product name 
         echo apply_filters('woocommerce_order_item_name', $item->get_name(), $item, false); 

         // SKU 
         if ($show_sku && is_object($product) && $product->get_sku()) { 
          echo ' (#' . $product->get_sku() . ')'; 
         } 

         // allow other plugins to add additional product information here 
         do_action('woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text); 

         wc_display_item_meta($item); 

         if ($show_download_links) { 
          wc_display_item_downloads($item); 
         } 

         // allow other plugins to add additional product information here 
         do_action('woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text); 

        ?></td> 
        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters('woocommerce_email_order_item_quantity', $item->get_quantity(), $item); ?></td> 
        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal($item); ?></td> 
       </tr> 
      <?php 
      } 
     } 
    } 
    else if ($email->id == 'new_order_storage2') { 
     $items = $order->get_items(); 
     foreach($items as $item_id => $item) { 

      $product = $item->get_product(); 

      if ($product->get_meta('_product_storage') == 'storage2') { 
     ?> 
       <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>"> 
        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php 

         // Show title/image etc 
         if ($show_image) { 
          echo apply_filters('woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ($product->get_image_id() ? current(wp_get_attachment_image_src($product->get_image_id(), 'thumbnail')) : wc_placeholder_img_src()) . '" alt="' . esc_attr__('Product image', 'woocommerce') . '" height="' . esc_attr($image_size[1]) . '" width="' . esc_attr($image_size[0]) . '" style="vertical-align:middle; margin-' . (is_rtl() ? 'left' : 'right') . ': 10px;" /></div>', $item); 
         } 

         // Product name 
         echo apply_filters('woocommerce_order_item_name', $item->get_name(), $item, false); 

         // SKU 
         if ($show_sku && is_object($product) && $product->get_sku()) { 
          echo ' (#' . $product->get_sku() . ')'; 
         } 

         // allow other plugins to add additional product information here 
         do_action('woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text); 

         wc_display_item_meta($item); 

         if ($show_download_links) { 
          wc_display_item_downloads($item); 
         } 

         // allow other plugins to add additional product information here 
         do_action('woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text); 

        ?></td> 
        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters('woocommerce_email_order_item_quantity', $item->get_quantity(), $item); ?></td> 
        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal($item); ?></td> 
       </tr> 
      <?php 
      } 
     } 
    } 
    else { 
    echo wc_get_email_order_items($order, array(
     'show_sku'  => $sent_to_admin, 
     'show_image' => false, 
     'image_size' => array(32, 32), 
     'plain_text' => $plain_text, 
     'sent_to_admin' => $sent_to_admin, 
    )); 
    } 

,如果您有任何的任何答案或建議,也如果有任何更好的方法來做到這一點,我所有的耳朵,我會非常感激。

問候,

回答

2

在SkyVerge的教程中,我使用加載的郵件類錯誤的(舊)的方式。 更新和工作代碼,學分,以@bhamrick @ wordpress.org:

add_filter('woocommerce_email_classes', [ $this, 'custom_order_email_add_email_classes' ], 10, 1); 

    function custom_order_email_add_email_classes($email_classes) { 

     $email_classes['WC_Email_New_Order_Storage1'] = include(CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage1.php'); 
     $email_classes['WC_Email_New_Order_Storage2'] = include(CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage2.php'); 

    return $email_classes; 
} 
1

我遵循同樣的SkyVerge教程,並發現我重複的電子郵件是由線造成了「新WC_Emails();」在教程中。

WC_Emails類不應直接實例化,而應該由靜態函數「instance()」調用。

在寫這篇文章時,定製電子郵件管理器。PHP文件教程定義瞭如下功能:

public function custom_trigger_email_action($order_id, $posted) { 
    // add an action for our email trigger if the order id is valid 
    if (isset($order_id) && 0 != $order_id) { 
     $order = new WC_order($order_id); 
     new WC_Emails(); 
     do_action('custom_pending_email_notification', $order_id); 
    } 
} 

這看起來應該是這樣,而不是:

public function custom_trigger_email_action($order_id, $posted) { 
    // add an action for our email trigger if the order id is valid 
    if (isset($order_id) && 0 != $order_id) { 
     $order = new WC_order($order_id); 
     WC_Emails::instance(); 
     do_action('custom_pending_email_notification', $order_id); 
    } 
} 

如果創建WC_Emails的新實例,然後將複製的電子郵件。

+0

你好! 是的,我遇到了和你一樣的問題,並且將其作爲我的答案進行了包括,問題就消失了,從那時起我一直沒有任何問題。甚至還添加了一些電子郵件課程。 :) – Magnetize