Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据Woocommerce中的产品类别按项目定制电子邮件通知_Php_Wordpress_Woocommerce_Send_Email Notifications - Fatal编程技术网

Php 根据Woocommerce中的产品类别按项目定制电子邮件通知

Php 根据Woocommerce中的产品类别按项目定制电子邮件通知,php,wordpress,woocommerce,send,email-notifications,Php,Wordpress,Woocommerce,Send,Email Notifications,我开发了一个自定义代码,根据客户购买的产品类别向他们发送电子邮件。我的网站的问题是一个产品有很多类别。该脚本工作正常,只是它发送的电子邮件数量与产品的类别数量相等。我希望每个产品只发送一封电子邮件 代码如下: add_action("woocommerce_order_status_changed", "wcepp_notification_email"); function wcepp_notification_email( $order_id, $checkout = null ) {

我开发了一个自定义代码,根据客户购买的产品类别向他们发送电子邮件。我的网站的问题是一个产品有很多类别。该脚本工作正常,只是它发送的电子邮件数量与产品的类别数量相等。我希望每个产品只发送一封电子邮件

代码如下:

add_action("woocommerce_order_status_changed", "wcepp_notification_email");
function wcepp_notification_email( $order_id, $checkout = null ) {
    global $woocommerce;

    $order = new WC_Order( $order_id );

    if($order->status === 'processing' ) {

        function wcepp_get_custom_email_html( $order, $heading = false, $mailer, $cat_slug ) {
            $template = 'emails/woo-custom-emails-per-product-'.$cat_slug.'.php';
            return wc_get_template_html( $template, array(
                    'order'         => $order,
                    'email_heading' => $heading,
                    'sent_to_admin' => true,
                    'plain_text'    => false,
                    'email'         => $mailer
                )
            );
        }

        $liquify_array = array('cutting-agent', 'dabjuice-liquify');
        $terpene_array = array('terpene-blends', 'strain-specific', 'hybrid', 'indica', 'sativa', 'terpene-sample-kits', 'hybrid-sample-kit', 'indica-sample-kit', 'sativa-sample-kit');
        $isolates_array = array('raw-terpene-isolates', 'alpha', 'beta');

        // getting the order products
        $items = $order->get_items();

        // let's loop through each of the items
        foreach ( $items as $item ) {
            $categories = get_the_terms( $item['product_id'] , 'product_cat' );

            foreach( $categories as $categorie ) {
                $cat_slug = $categorie->slug;

                if(in_array($cat_slug, $liquify_array)) {
                    $new_cat_slug = 'liquify';
                } elseif(in_array($cat_slug, $terpene_array)) {
                    $new_cat_slug = 'terpenes';
                } elseif(in_array($cat_slug, $isolates_array)) {
                    $new_cat_slug = 'isolates';
                }

                if(isset($new_cat_slug)) {
                    // Create a mailer
                    $mailer = $woocommerce->mailer();

                    //Prepare Email
                    $recipient = $order->billing_email;

                    if($new_cat_slug == 'liquify') {
                        $subject = 'Instructions for usage of your Liquify Product';
                    } elseif($new_cat_slug == 'terpenes') {
                        $subject = 'Instructions for usage of your Terpenes Product';
                    } elseif($new_cat_slug == 'isolates') {
                        $subject = 'Instructions for usage of your Raw Terpenes Isolates';
                    }

                    $content = wcepp_get_custom_email_html( $order, $subject, $mailer, $new_cat_slug );
                    $headers = "Content-Type: text/html\r\n";

                    //send the email through wordpress
                    $mailer->send( $recipient, $subject, $content, $headers );
                }
            }
        }
    }
}

我们将感谢您在这方面提供的任何帮助。

您的代码自Woocommerce 3以来已经过时,并且有许多错误。如果在Wordpress上启用调试,您将收到大量错误消息。请尝试以下方法,当每个产品与定义的产品类别匹配时,该方法将为每个产品发送电子邮件:

// Get email content from the custom template
function get_custom_email_content_html( $order, $heading = false, $mailer, $category_slug ) {
    $template = 'emails/woo-custom-emails-per-product-'.$category_slug.'.php';
    return wc_get_template_html( $template, array(
            'order'         => $order,
            'email_heading' => $heading,
            'sent_to_admin' => true,
            'plain_text'    => false,
            'email'         => $mailer
        )
    );
}

// Send a custom email notification for each order item on order processing status change
add_action( 'woocommerce_order_status_changed', 'processing_custom_email_notification', 10, 4 );
function processing_custom_email_notification( $order_id, $status_from, $status_to, $order ) {

    if( $status_to === 'processing' ) {

        $liquify_array = array('cutting-agent', 'dabjuice-liquify');
        $terpene_array = array('terpene-blends', 'strain-specific', 'hybrid', 'indica', 'sativa', 'terpene-sample-kits', 'hybrid-sample-kit', 'indica-sample-kit', 'sativa-sample-kit');
        $isolates_array = array('raw-terpene-isolates', 'alpha', 'beta');

        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            $product_category = '';
            $categories_slugs = get_the_terms( $item->get_product_id() , 'product_cat', array( 'fields' => 'slugs' ) );

            foreach( $categories_slugs as $term_slug ) {
                if( in_array( $term_slug, $liquify_array ) ) {
                    $product_category = 'liquify';
                    break;
                } elseif(in_array( $term_slug, $terpene_array ) ) {
                    $product_category = 'terpenes';
                    break;
                } elseif(in_array( $term_slug, $isolates_array ) ) {
                    $product_category = 'isolates';
                    break;
                }
            }

            if( isset( $product_category ) ) {
                // Email subject
                if( $product_category == 'liquify' ) {
                    $subject = __("Instructions for usage of your Liquify Product");
                } elseif($product_category  == 'terpenes' ) {
                    $subject = __("Instructions for usage of your Terpenes Product");
                } elseif( $product_category == 'isolates' ) {
                    $subject = __("Instructions for usage of your Raw Terpenes Isolates");
                }
                $mailer    = WC()->mailer(); // Get an instance of the WC_Emails Object
                $recipient = $order->get_billing_email(); // Email recipient
                $content   = get_custom_email_content_html( $order, $subject, $mailer, $product_category ); // Email content (template)
                $headers   = "Content-Type: text/html\r\n"; // Email headers

                // Send the email
                WC()->mailer()->send( $recipient, $subject, $content, $headers );
            }
        }
    }
}
它现在应该可以更好地工作了(但不需要在您的产品类别中进行测试)

3个自定义电子邮件模板应位于
woocommerce
文件夹>
emails
子文件夹(活动主题):
woo每个产品的自定义电子邮件liquify.php

woo每个产品的定制电子邮件terpenes.php

woo为每个产品定制电子邮件。php


感谢您的帮助,但如果订单包含所有主要类别的产品,它会发送所有三封电子邮件吗?@ShahidKhattak您在问题中要求根据您定义的产品类别按产品(订单项)发送电子邮件通知。我的答案代码将采用第一个匹配的产品类别(定义主题和内容),并将按产品发送电子邮件(如果有匹配的产品类别)。因此它按产品发送1封电子邮件。此代码发送一封带有错误消息的电子邮件,消息如下注意:wc_get_模板调用不正确/home2/amayfield/test.adriennemayfield.com/wp content/plugins/woocommerce/templates/emails/woo每个产品的自定义电子邮件-.php不存在。回溯:编辑发布、更新发布、插入发布、执行操作(“保存发布”)、执行钩子->执行操作、执行钩子->应用过滤器、WC管理框->保存框、执行操作('woocommerce\u process\u shop\u order\u Meta')、执行钩子->应用过滤器、执行钩子->执行钩子->执行钩子->执行钩子->执行钩子过滤器、执行钩子->执行钩子->执行钩子->执行钩子->执行钩子->执行钩子->执行钩子过滤器、执行钩子->保存、WC订单保存、WC订单保存、WC订单状态、执行操作('woocommerce\u order\u status\u changed'),WP_Hook->do_action,无法复制完整的错误消息,但您可以清楚地看到,它并没有将类别slug后缀添加到电子邮件模板名称中。如果您可以添加自定义电子邮件模板的代码,这对其他人应该很有用。谢谢。我的自定义电子邮件模板是一个HTML文件,其中仅包含产品的文本说明t、 请在你的问题中加上它。