添加“说明"可变产品的字段内容到woocommerce“已完成的订单";电子邮件 [英] Add "description" field contents for variable product to woocommerce "Completed order" email

查看:24
本文介绍了添加“说明"可变产品的字段内容到woocommerce“已完成的订单";电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在销售多种产品,每个产品都有 2 个变体,每个变体都需要在已完成的电子邮件中包含一段自定义文本(带有嵌入的网址).大量自定义电子邮件:每个产品变体.我为functions.php 找到了很多选项,但它们都来自多年以前的woo 版本.

非常流行的Woo Custom Emails Per Product"插件没有 per-variation 功能.我不想让每个变体都成为自己的产品(因此可以使用该插件),因为我希望每个变体都有一个产品页面,顾客可以在其中选择他们想要的变体.

所以我决定为每个变体添加信息的最佳方式是在描述"中.变量字段.

这是我希望它去的地方,在我认为是 woocommerce_email_order_items_table 之上:

您可以使用 woocommerce_email_before_order_table 钩子.请参阅此处 了解更多信息.

下面的函数将获取每个订购产品的描述 (如果描述不为空).

<块引用>

描述是从产品变体中获得的不是可变产品.

//在完成的订单邮件中在订单表前添加产品描述add_action('woocommerce_email_before_order_table', 'add_content_before_order_table', 99, 4);函数 add_content_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {//仅用于已完成订单的电子邮件如果(!$email->id == 'customer_completed_order'){返回;}//用所有产品描述初始化数组$descriptions = array();//对于每个订购的产品foreach ( $order->get_items() as $order_item ) {$product = $order_item->get_product();//获取产品描述如果 ( $product->get_description() ) {$descriptions[] = $product->get_description();}}//如果 $descriptions 不为空,则显示它如果(!空($描述)){foreach ( $descriptions as $content ) {回声'<p>'.$内容.'</p>';}}}

代码已经过测试并且可以工作.将它添加到您的活动主题的functions.php.

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.

这篇关于添加“说明"可变产品的字段内容到woocommerce“已完成的订单";电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆