简体   繁体   English

将可变产品的“描述”字段内容添加到 woocommerce “已完成订单” email

[英]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.我销售多种产品,每种产品都有 2 个变体,每个变体都需要在已完成的 email 中有一段自定义文本(带有嵌入的 URL)。很多自定义电子邮件:每个产品变体。 I've found many options for functions.php but they are all from many years and woo versions ago.我发现了很多函数选项。php 但它们都是很多年前的版本了。

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.非常流行的“Woo Custom Emails Per Product”插件没有每个变体 function。我不想让每个变体成为自己的产品(因此可以使用该插件),因为我想要每个产品页面,其中赞助人可以 select 他们想要的变化。

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:这是我想要它的地方 go,在我认为是 woocommerce_email_order_items_table 的上方:

screen grab of email showing where text should go email 的屏幕抓取显示文本应在何处 go

I tried adding this to functions.php but it's from 2015 and is for "processing" not "completed" emails:我尝试将其添加到 functions.php 但它是从 2015 年开始的,用于“处理”而不是“已完成”的电子邮件:

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我试过这个,但这是几年前的事了,没有完成我需要的: 将产品描述添加到 WooCommerce email 通知

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;这很接近: 在 WooCommerce“订单已完成”电子邮件中添加自定义产品字段但不是我想要的,因为没有特定于每个变体的自定义字段; 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.我想找到一种方法来编辑 email 模板,因为我认为这将是 go 的理想方式。 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/ .这是我设置的测试页面: 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).只有 Escher 和 Dover 产品有变量,在网络链接的描述中有一个测试短语和 URL(如果您选择该选项,它会弹出,但我最终会用 CSS 隐藏在这里,但我把它留作测试).

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.我觉得将变体描述添加到 email 应该非常简单/简单,也许我没有足够的经验或没有找对地方,但那条特定的数据似乎很难挂钩并显示在订单中确认email。

Thanks!谢谢!

To add custom content to the email template at this point:此时要向 email 模板添加自定义内容:

在此处输入图像描述

You can use the woocommerce_email_before_order_table hook.您可以使用woocommerce_email_before_order_table挂钩。 See here for more information.请参阅此处了解更多信息。

The following function will get the descriptions of each ordered product (if the description is not empty) .下面的 function将获取每个订购产品的描述(如果描述不为空)

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.将其添加到您的活动主题的功能中。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' .状态检查$email->id == 'customer_completed_order'似乎有问题。 I added $status = $order->get_status();我添加了$status = $order->get_status(); and changed it to look for $status == "completed" instead.并将其更改为查找$status == "completed" 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>';
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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