1
votes

I have developed a custom code which sends e-mails to the customers based on the category of the product they buy. The problem with my site is one product is having many categories. The script is working correctly, other than that it sends many emails equal to the number of categories a product have. I want it to send only one e-mail per product.

Here is the code:

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

Any help in this regard will be appraciated.

1
It should be useful for others if you could add the code of your custom email template. Thanks.LoicTheAztec
My custom e-mail template is an HTML file which contains only text instruction for the product.Shahid Khattak
Add it anyways in your question please.LoicTheAztec

1 Answers

2
votes

Your code is outdated since Woocommerce 3 and have many mistakes. If you enable the debug on Wordpress you will get a lot of error messages. Try the following instead that will send an email for each product when it matches with a defined product category:

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

It should better work now (but not tested with your product categories).

3 custom email templates should be located in the woocommerce folder > emails subfolder (of the active theme):
woo-custom-emails-per-product-liquify.php
woo-custom-emails-per-product-terpenes.php
woo-custom-emails-per-product-isolates.php