Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add Related Products To The Order Processing Emails in WooCommerce?

Ever received an email suggesting products based on what you’ve bought before? Well, product suggestions in processing order emails are another easy way to encourage customers to make additional purchases. When sending cross-selling products based on the products purchased, customers may opt to buy the suggested items. Let’s see how to add this smart selling trick via WooCommerce customization and boost your sales!

Solution: Add Related Products To The Order Processing Emails

The code retrieves the order details, including product IDs, searches for related products based on their categories, and creates the necessary HTML structure to show product suggestions in order processing emails.

function ts_order_mail_product_suggestion($atts) {
    $atts = shortcode_atts(
        array(
            'id' => '',
        ),
        $atts,
        'order_mail_product_suggestion'
    );
    $orderId = esc_attr($atts['id']);
    $order = wc_get_order((int)$orderId);
    $items = $order->get_items();
    $cat = array();
    $pid = array();
    foreach ($items as $item) {
        $pid[] = $item->get_product_id();
        $terms = wp_get_post_terms($item->get_product_id(), 'product_cat', array('fields' => 'ids'));
        foreach ($terms as $term) {
            $cat[] = $term;
        }
    }
    $uniqueCat = array_unique($cat);
    $uniquePid = array_unique($pid);
    $html = '';
    $args = array(
        'post_type'             => 'product',
        'stock'                 => 1,
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => '20',
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'field'         => 'term_id',
                'terms'         => implode(',', $uniqueCat),
                'operator'      => 'IN'
            ),
            array(
                'taxonomy'      => 'product_visibility',
                'field'         => 'slug',
                'terms'         => 'exclude-from-catalog',
                'operator'      => 'NOT IN'
            )
        )
    );
    $loop = new WC_Product_Query($args);
    $products = $loop->get_products();
    if ($products) {
        $html .= '<div id="suggestion" style="padding: 20px 0;border-top: 1px solid #eee;">';
            $html .= '<h2 style="text-align: center;font-weight: 400;color: #000;">PLUS, MORE THINGS YOU MAY LIKE</h2>';
            $html .= '<table style="width: 100%;table-layout: fixed;border-collapse: collapse;">';
                $html .= '<tbody>';
                    $html .= '<tr>';
                            $i = 0;
                            foreach ($products as $product) {
                                if (in_array($product->get_id(), $pid)) {
                                    continue;
                                }
                                $html .= '<td align="center" style="padding: 5px;border: 1px solid #eee;">';
                                if (has_post_thumbnail($product->get_id())) {
                                    $html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog');
                                } else {
                                    $html .= '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="300px" height="300px" />';
                                }
                                $html .= '<h3><a style="color: #000;font-weight: normal;text-decoration: none;font-size: 13px;text-align: center;display: block;line-height: 16px;" href="' . get_permalink($product->get_id()) . '">' . esc_attr($product->get_title() ? $product->get_title() : $product->get_id()) . '</a></h3>';
                                $html .= '<p><a style="font-weight: normal;text-decoration: none;display: block;margin: 0 auto;max-width: 72px;padding: 4px 9px;text-align: center;background-color: #000;color: #fff;font-size: 13px;" href="' . get_permalink($product->get_id()) . '" class="shop-now">Shop Now</a></p>';
                                $i++;
                                if ($i == 4) {
                                    break;
                                }
                            }
                    $html .= '</tr>';
                $html .= '</tbody>';
            $html .= '</table>';
        $html .= '</div>';
    }
    // now return the prepared html
    return $html;
}
// register shortcode
add_shortcode('ts_email_product_suggestion', 'ts_order_mail_product_suggestion');

add_action('woocommerce_email_after_order_table', 'ts_add_product_suggestion_to_order_email', 10, 4);

function ts_add_product_suggestion_to_order_email($order, $sent_to_admin, $plain_text, $email) {
    // Only add product suggestion to customer emails and if the order status is "processing"
    if (!$sent_to_admin && $order->get_status() === 'processing') {
        $id = $order->get_id();
        echo do_shortcode('[ts_email_product_suggestion id="' . $id . '" ]');
    }
}

Output

The related products are displayed in processing order emails sent to customers as shown below. When customers click the “Shop Now” button, it redirects them to the respective product page, allowing them to buy the suggested product.

How to Add Related Products To The Order Processing Emails in WooCommerce? - Tyche Softwares

In the same way, you can also show product suggestions at different touchpoints of your website. For instance, you can upsell related products on the order processing status page. Do you know any other special spot on the WooCommerce page that can improve upsell? Let us know in the comment section, and we will make a separate post on it :v:

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x