2
votes

I'm selling multiple products, each with 2 variations that will each need to have a custom bit of text (with with URLs embedded) in the Completed email. Lots of custom emails: per product and variation. I've found many options for functions.php but they are all from many years and woo versions ago.

The very popular "Woo Custom Emails Per Product" plugin does not have a per-variation function. I do not want to make each variation its own product (and could therefore use that plugin) since I want a single product page for each, where the patron can select the variation they want.

So I decided the best way to add the info for each variation is in the "Description" field for the variation.

Here's where I would like it go, above what I believe is the woocommerce_email_order_items_table:

screen grab of email showing where text should go

I tried adding this to functions.php but it's from 2015 and is for "processing" not "completed" emails:

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
function render_product_description($item_id, $item, $order){
    $_product = $order->get_product_from_item( $item );
    echo "<br>" . $_product->post->post_content; 

}

add_action('woocommerce_order_item_meta_end', 'render_product_description',10,3);

I've tried this, but it's from years ago and did not accomplish what I need: Add the product description to WooCommerce email notifications

This is close: Add Custom Product Field in WooCommerce 'Order Completed' Emails but not quite what I want, because there is not a custom field specific to each variation; it seems the only thing custom to each variation is "Description."

I'd like to find a way to edit the email template, as I think that would be the ideal way to go here. If it can simply list the Description contents for each item's variation ordered, I can format that text to be self-explanatory for the patron, and then the order summary box (with Product/Quantity/Price) will stay clean and just list the items.

This is a test page I set up: https://www.chambermusicpittsburgh.org/concerts-and-tickets-new-store/. Only the Escher and Dover product has the variables, with a tester phrase and URL in the Description for the weblink (which will pop down if you choose that option, but which I will eventually hide here with CSS but I've left it for testing).

I feel like adding the variation Description to the email should be super straightforward/simple, and maybe I'm not experienced enough or not looking in the right place, but that particular piece of data seems extremely hard to hook into and display in the Order Confirmed email.

Thanks!

1
update from Automattic directly: 'That could be done with custom code but it would require a lot of work." update from WooCommerce directly: "This is a fairly complex development topic." I'm just confused why there doesn't seem to be a hook or API call for the Description field in a product variation, that could be inserted into the email template. I'm clearly not experienced enough with WooCommerce to know why this is. Thanks for any help!hot ham water

1 Answers

0
votes

To add custom content to the email template at this point:

enter image description here

You can use the woocommerce_email_before_order_table hook. See here for more information.

The following function will get the descriptions of each ordered product (if the description is not empty).

The description is obtained from the product variation NOT the variable product.

// adds the product description before the order table in the completed order email
add_action( 'woocommerce_email_before_order_table', 'add_content_before_order_table', 99, 4 );
function add_content_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {

    // only for the email of the completed order
    if ( ! $email->id == 'customer_completed_order' ) {
        return;
    }

    // initializes the array with all product descriptions
    $descriptions = array();

    // for each product ordered
    foreach ( $order->get_items() as $order_item  ) {
        $product = $order_item->get_product();
        // get the product description
        if ( $product->get_description() ) {
            $descriptions[] = $product->get_description();
        }
    }

    // if the $descriptions is not empty it shows it
    if ( ! empty( $descriptions ) ) {
        foreach ( $descriptions as $content ) {
            echo '<p>' . $content . '</p>';
        }
    }

}

The code has been tested and works. Add it to your active theme's functions.php.