Woocommerce,不同的订单电子邮件取决于订单中的产品 [英] Woocommerce, different order emails depending on product in order

查看:27
本文介绍了Woocommerce,不同的订单电子邮件取决于订单中的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据订购的产品发送两封不同的new_order"电子邮件.情况是产品存储在不同的位置,我需要发送一封电子邮件,其中仅包含存储 1 中存在的产品,另一封邮件包含存储 2 中存在的产品.如果只订购了位于存储 1 的产品,则根本不应发送电子邮件 2,反之亦然.

I need to send two different "new_order" emails depending on the products ordered. The case is that the products are stored at different locations and I need one email to be sent containing only the products present at storage 1, and the other mail containing the products present at storage 2. If only products located at storage 1 was ordered, email 2 should not be sent at all, and vice versa.

存储信息是每个产品上的自定义元字段.

The storage information is a custom meta field on each product.

我添加了一个自定义电子邮件类,它是 class-wc-email-new-order 的副本,但仅在需要时更改了名称.

I have added a custom email-class which is a copy of class-wc-email-new-order but only with changed name where needed.

我一直在为此查看挂钩和过滤器,但我无法一路走下去.我也一直在考虑覆盖 email-order-details.php 文件并检查每个产品元字段,但我不确定这是否是正确的方法.我想即使表中没有产品,电子邮件仍然会被发送,可以这么说.

I've been looking through the hooks and filters for this, but I was not able to go all the way. I've also been thinking about overriding the email-order-details.phpfile and check each products meta field, but I am not sure if that is the right way to go. I guess the email would still be sent even if there are no products in the table, so to speak.

我的问题是我不知道在使用不同的过滤器时发送了哪些电子邮件,所以我也无法检查该条件.

My problem is that I don't know which of the emails are sent out when using the different filters, so I cannot check that condition either.

您是要创建两封带有模板和所有内容的新电子邮件并继续这样做,还是我在这里遗漏了什么?

Would you just create two new emails with templates and everything and go that way, or is it something I'm missing here?

如果我能澄清任何事情,请告诉我.

If I can clarify anything, please let me know.

在与@helgatheviking 交谈后,我意识到我需要明确指出,只有当订单包含位于存储 1 的产品时才发送有关存储 1 的电子邮件,并且仅应在订购时发送有关存储 2 的电子邮件包含的产品位于存储 2.如果订单包含两种产品,则应发送两封电子邮件,但仅显示各自的产品.

After talking a bit with @helgatheviking I realized that I needed to clearify that the emails regarding storage 1 should be sent only when the order contained products located at storage 1 and emails regarding storage 2 should only be sent when the order contained products located at storage 2. If the order contained both kind of products, both emails should be sent, but only displaying their respective products.

推荐答案

在仔细考虑并反思@helgatheviking 所说的内容后,我想出了一个解决方案.

After thinking it through and reflecting upon what @helgatheviking said I was able to come up with a solution.

我复制了 class-wc-new-order 并创建了两个与原始类完全相同的新类.我将 ID 和类名分别更改为存储 1 和存储 2.

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: EDIT WRONG METHOD, SEE BELOW INSTEAD

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

请注意,这是在一个特殊的插件中,如果您要在您的functions.php 中使用它,您必须稍微编辑代码.

Notice that this is within a special plugin, you'll have to edit the code a bit if you were to use it in your functions.php.

然后我编辑了两个电子邮件类中的 trigger() 函数:

I then edited the trigger()-function in both email classes:

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

一切似乎都正常,但在接受我自己的答案之前,我会做更多的测试.再次感谢 Helgatheviking 带领我走上正轨.:)

Everything seems to be behaving as it should but I'll do some more testing before accepting my own answer. Thanks again to Helgatheviking for leading me onto the right track. :)

忘记添加我对 email-order-details.php 的覆盖.这是在

forgot to add my override of email-order-details.php. This is added just below <tbody>

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

显示订单商品的代码直接取自order-email-items.我不确定是否有更好的方法来实现这个想法,如果是这种情况,请随时纠正我.

The code displaying order items are taken directly from order-email-items. I am not sure if there is a better way to do this thought, feel free to correct me if this is the case.

编辑,Skyverge 使用旧方法加载自定义电子邮件类.加载自定义电子邮件类的正确方法是:

EDIT, Skyverge was using an old method to load custom email classes. Correct method for loading custom email classes is:

    //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 ) {

         $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天全站免登陆