Woocommerce - 自定义电子邮件类创建重复的电子邮件 [英] Woocommerce - custom email classes creates duplicate emails

查看:575
本文介绍了Woocommerce - 自定义电子邮件类创建重复的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间我发布了一个有关问题的问题,根据订购产品所属的存储(自定义字段),我需要发送不同的电子邮件。
所以,如果只订购了属于Storage1的产品,那么只有Email1被发送。如果一个属于Storage1的产品和一个属于Storage2的产品被订购,则电子邮件1将被发送出来,包含product1,而email2将包含product2。
链接到原始问题:定制电子邮件取决于产品元数据



这一切现在都可以工作,但由于某些原因,我收到双重电子邮件。如果我从Storage1订购一个产品,我会收到两个Email1发送到我的收件箱...如果我订购5,10或20个产品,我只收到一个额外的电子邮件,但这是足够的。



所以我想看看有没有人经历过同样的事情,他们如何修复?



我会添加代码为了清楚起见,这里再次出现。



我复制了 class-wc-new-order 并准确创建了两个新类与原来一样。我将ID和类名分别更改为存储1和存储2。



我通过执行以下操作加载课程:



$ code //将我们的自定义类添加到WC电子邮件类
add_filter('woocommerce_email_classes',[$ this,'custom_order_email_add_email_classes'],10,1);

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

然后我编辑了 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
$ trigger = true;
}
}
}

如果(!$ this-> is_enabled()||!$ this-> get_recipient()){
返回
}

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 用于默认显示产品。



已编辑的电子邮件订单详细信息。



如果($ 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 =tdstyle =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

//显示标题/图像等
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 = image_size [1])。'width ='。esc_attr($ image_size [0])。style =vertical-align:middle; margin-'。(is_rtl()?'left':'right' ':10px;/>< / div>',$ item);
}

//产品名称
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()。')';
}

//允许其他插件在这里添加其他产品信息
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);
}

//允许其他插件在此添加其他产品信息
do_action('woocommerce_order_item_meta_end',$ item_id,$ item,$ order,$ plain_text);

?>< / td>
< td class =tdstyle =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 =tdstyle =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 = $命令 - > 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 =tdstyle =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

//显示标题/图像等
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 = image_size [1])。'width ='。esc_attr($ image_size [0])。style =vertical-align:middle; margin-'。(is_rtl()?'left':'right' ':10px;/>< / div>',$ item);
}

//产品名称
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()。')';
}

//允许其他插件在这里添加其他产品信息
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);
}

//允许其他插件在此添加其他产品信息
do_action('woocommerce_order_item_meta_end',$ item_id,$ item,$ order,$ plain_text);

?>< / td>
< td class =tdstyle =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 =tdstyle =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'=>数组(32,32),
'plain_text'=> $ plain_text,
'sent_to_admin'=> $ sent_to_admin,
));
}

如果您有任何回答或建议,如果有更好的方法可以做到这一点,我都是耳朵。



请问,

解决方案

SkyVerge的教程我用旧的方式加载电子邮件类。
更新和工作的代码,@bhamrick @ wordpress.org的信用:

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

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


Awhile ago I posted a question regarding a problem, I needed to send out different emails depending on what storage (custom field) the ordered products belonged to. So, if only a product belonging to Storage1 was ordered, only Email1 was supposed to be sent. If one product belonging to Storage1 AND one product belonging to Storage2 was ordered, email1 would be sent out containing product1 and email2 would contain product2. Link to original question: Custom order emails depending on product meta data

This all works now, but for some reason I get double emails. If I order one product from Storage1 I get TWO Email1 sent to my inbox... It does not matter if I order 5, 10, or 20 products, I only get ONE extra email, but that is enough.

So I would like to see if anyone experienced the same thing, and how they fixed it?

I'll add the code here again, for clarity.

I copied the class-wc-new-order and created two new classes exactly the same as the original. I changed ID and class names to storage 1 and storage 2 respectively.

I load the classes by doing the following:

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

I then edited the trigger()-function in both email classes. Notice that I did not change anything with the trigger actions, except for testing but I still get double emails every time, no matter what trigger actions are active. If I deactivate all trigger actions I do not get any emails at all, of course.

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

In order to filter out Storage2 products from Email1 I had to override email-order-details.php. This code is entered just below <tbody> in the template, and I took the code from the order-email-items.php which is used to display the products by default.

Edited email-order-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,
    ) ); 
    }

I would be very greatful if any of you have any answers or suggestions, also if there are any better ways to do this I am all ears.

Regards,

解决方案

The tutorial at SkyVerge I used loaded the email classes the wrong (old) way. Updated and working code, credits to @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;
}

这篇关于Woocommerce - 自定义电子邮件类创建重复的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆