基于项目的自定义电子邮件通知基于Woocommerce中的产品类别

时间:2018-11-06 05:19:24

标签: php wordpress woocommerce send email-notifications

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

代码如下:

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

在这方面的任何帮助都会得到赞赏。

1 个答案:

答案 0 :(得分:1)

自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-custom-emails-per-product-liquify.php
  woo-custom-emails-per-product-terpenes.php
  woo-custom-emails-per-product-isolates.php