Creating a Custom Email in WooCommerce

WooCommerce is a powerful and flexible e-commerce platform built on WordPress. Customizing it to suit your specific needs can be a game-changer for your online business. One such customization is creating custom email notifications to send to your vendors. This can be useful for order notifications, updates, or any specific information that needs to be communicated to your vendors.

In this blog post, we will walk through the steps to create a custom email in WooCommerce to send to a vendor. We will cover the following:

  1. Setting up the custom email class.
  2. Registering the custom email with WooCommerce.
  3. Triggering the email based on specific events.

Step 1: Setting Up the Custom Email Class

The first step is to create a custom email class. This class will define the content and behavior of the email. Place this code in your theme’s functions.php file or a custom plugin.

if ( ! class_exists( 'WC_Vendor_Email' ) ) {
    class WC_Vendor_Email extends WC_Email {

        public function __construct() {
            $this->id = 'wc_vendor_email';
            $this->title = 'Vendor Email';
            $this->description = 'This email is sent to the vendor when a new order is placed.';
            $this->heading = 'New Order Notification';
            $this->subject = 'New Order Received';

            $this->template_html  = 'emails/vendor-email.php';
            $this->template_plain = 'emails/plain/vendor-email.php';

            add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );

            parent::__construct();
        }

        public function trigger( $order_id ) {
            if ( ! $order_id ) return;

            $this->object = wc_get_order( $order_id );
            $this->recipient = '[email protected]'; // Replace with the vendor's email address

            if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
                return;
            }

            $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
        }

        public function get_content_html() {
            return wc_get_template_html( $this->template_html, array(
                'order'         => $this->object,
                'email_heading' => $this->get_heading(),
                'sent_to_admin' => false,
                'plain_text'    => false,
                'email'         => $this,
            ) );
        }

        public function get_content_plain() {
            return wc_get_template_html( $this->template_plain, array(
                'order'         => $this->object,
                'email_heading' => $this->get_heading(),
                'sent_to_admin' => false,
                'plain_text'    => true,
                'email'         => $this,
            ) );
        }

    }
}

Step 2: Registering the Custom Email with WooCommerce

Next, we need to register our custom email with WooCommerce. This will make WooCommerce aware of the new email class we just created. Add the following code to the same file where you defined the custom email class.

add_filter( 'woocommerce_email_classes', 'register_vendor_email' );
function register_vendor_email( $email_classes ) {
    $email_classes['WC_Vendor_Email'] = include( 'path/to/WC_Vendor_Email.php' ); // Adjust the path as needed
    return $email_classes;
}

Step 3: Triggering the Email Based on Specific Events

In our custom email class, we added a trigger to send the email when an order is completed. This is done using the woocommerce_order_status_completed_notification hook. If you want to trigger the email based on different events, you can change the hook accordingly.

For example, to trigger the email when an order is placed, you can use the woocommerce_thankyou hook.

add_action( 'woocommerce_thankyou', 'trigger_vendor_email', 10, 1 );
function trigger_vendor_email( $order_id ) {
    $email = WC()->mailer()->emails['WC_Vendor_Email'];
    $email->trigger( $order_id );
}

Customizing the Email Template

Finally, you need to create the email templates. Place the HTML and plain text templates in your theme’s WooCommerce email directory (your-theme/woocommerce/emails/).

HTML Template (vendor-email.php):

<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

echo $email_heading . "\n\n";

echo '<p>Dear Vendor,</p>';
echo '<p>You have received a new order. Here are the details:</p>';

// Include order details template
wc_get_template( 'emails/email-order-details.php', array( 'order' => $order ) );

echo '<p>Best regards,</p>';
echo '<p>Your Company</p>';

echo $email_footer;
?>

Plain Text Template (plain/vendor-email.php):

<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

echo $email_heading . "\n\n";

echo "Dear Vendor,\n";
echo "You have received a new order. Here are the details:\n\n";

// Include order details template
wc_get_template( 'emails/plain/email-order-details.php', array( 'order' => $order ) );

echo "Best regards,\n";
echo "Your Company\n";

echo $email_footer . "\n";
?>

Conclusion

By following these steps, you can create a custom email in WooCommerce to send notifications to your vendors. This customization can improve communication and streamline processes in your e-commerce store. Remember to test your custom email thoroughly to ensure it works as expected.

Feel free to modify the code and templates to fit your specific needs. Happy coding!

Get A No Obligation Quote

Do You Need Help With Your WooCommerce Site?

Click through to the next page and complete the form to get a free no obligation quote to fix any issue you are having with your WooCommerce site.