简体   繁体   中英

Add "description" field contents for variable product to woocommerce "Completed order" email

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!

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

在此处输入图像描述

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.

This works but added the description in both the processing and the completed mail and admin mails. There seems to something wrong with the status check $email->id == 'customer_completed_order' . I added $status = $order->get_status(); and changed it to look for $status == "completed" instead. It works for me, but please add your thoughts or comments if this is a good way.

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 ) {

  // checking if it's the order status we want
    $status = $order->get_status();
    if ( $status == "completed" ) {
        
        // 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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM