0
votes

I am using the WooCommerce Follow Up Emails plugin to design a custom email to be sent after an order is made.

The product that is being ordered has a few Add On Fields displayed that are populated when the customer makes the order. One of the fields is an email address.

I want to send the custom follow up email I've designed to the email address provided in the field rather than to the email address of the person that made the order.

I can get the email address by using the order ID as it's assigned as a meta data to it, but I do not know how to override where the follow up email address is sent to.

Looking through the Follow Up Emails' API I can see a few hooks that allow me to get the email by ID for example:

$fue_api->get_email( 2011 )

There is another endpoint that seems to be for updating the email:

    /**
     * Update a follow-up email
     * @param  integer $email_id
     * @param  array  $data
     * @return mixed|json string
     */
    public function update_email( $email_id, $data = array() ) {
        return $this->_make_api_call( 'emails/' . $email_id, $data, 'POST' );
    }

But I am not sure how to change the recipient's email address to the one from the variable?, because:

1) The email of the recipient is only available once an order is made

2) I need to update the email before it's sent and for it to only be sent on order

I don't know how to link these two things together.

I think I need to hook somehow into an event that controls sending emails after an order is made, get the ID of the email I want to modify, and change it's recipient to the one from the order meta data before it's added to the queue.

Any help is greatly appreciated!

1

1 Answers

0
votes

After battling with the WooCommerce Follow Up Emails plugin I think the simplest solution is to just:

  • Hook into the woocommerce new order;

  • Check if the order contains the meta email address;

  • If it does send an email with the data I want;

The cons are that it won't be customise-able from a UI, but the benefit is not having to pay $99 for the plugin to send 1 email that they don't even allow you to do off the shelf (as far as I can see).

Here is an example solution:

/**
 * CUSTOM EMAIL ON ORDER
 */

add_action( 'woocommerce_payment_complete', 'order_completed' );
function order_completed( $order_id ) {
    $variables = array();
    $order = wc_get_order( $order_id );

    // This goes through the meta data items to add them as variables
    foreach ($order->get_items() as $item_id => $item ) {
        $item_meta = $item->get_meta('_WCPA_order_meta_data');
        foreach ($item_meta as $meta ) {
            if (!empty($meta['name'])) {
                $variables[$meta['name']] = $meta['value'];
            }
        }
    }

    // If the email address meta item is there 
    // it means we need to send the custom email
    if (!empty($variables['custom_addon_email'])) {
        $to_email = $variables['custom_addon_email'];
        $headers = 'From: Your Name <[email protected]>' . "\r\n";
        $subject = 'This is my custom email test';
        $template = 'This is just testing of sending a custom email for: ' . $variables['custom_addon_first_name'] . ' ' . $variables['custom_addon_last_name'] . '. Note:' . $variables['custom_addon_note'];

        wp_mail($to_email, $subject, $template, $headers );
    }
}

It is a very crude example, but I hope it helps others. The ['custom_addon_email'], ['custom_addon_first_name'], ['custom_addon_last_name'], ['custom_addon_note'] are the names of the addon fields added to the product and they are the names I gave the fields in the name="" attributes.

Ideally I would have loved to be able to do this with the WooCommerce Follow Up Emails plugin, because it would make customising the email content easier, but I don't see how.