将产品描述添加到 WooCommerce 电子邮件通知 [英] Add the product description to WooCommerce email notifications

查看:44
本文介绍了将产品描述添加到 WooCommerce 电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 WooCommerce 新订单 电子邮件通知中添加单个产品页面的产品描述/内容(不是简短描述)?

How to add product description/content of single product page (not the short description) to WooCommerce new order email notification?

我需要知道我的产品的具体书面描述,因为它们中的大多数几乎相同.

I need to know specific written description of my products as most of them are almost same.

推荐答案

当您针对特定的电子邮件通知时,首先我们需要获取电子邮件 ID 以针对新订单"电子邮件通知.唯一的方法是先获取并在全局变量中设置值.

As you are targeting a specific email notification, first we need to get the Email ID to target the "New Order" email notification. The only way is to get it before and to set the value in a global variable.

然后在 woocommerce_order_item_meta_end 操作挂钩中挂钩的自定义函数中,我们专门为新订单电子邮件通知显示产品说明.

Then in a custom function hooked in woocommerce_order_item_meta_end action hook, we display the product description exclusively for New Order email notification.

这是代码:

 ## Tested on WooCommerce 2.6.x and 3.0+

// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
    $GLOBALS['email_id_str'] = $email->id;
}

// Displaying product description in new email notifications
add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 4 );
function product_description_in_new_email_notification( $item_id, $item, $order = null, $plain_text = false ){

    // Getting the email ID global variable
    $refNameGlobalsVar = $GLOBALS;
    $email_id = $refNameGlobalsVar['email_id_str'];

    // If empty email ID we exit
    if(empty($email_id)) return;

    // Only for "New Order email notification"
    if ( 'new_order' == $email_id ) {

        if( version_compare( WC_VERSION, '3.0', '<' ) ) { 
            $product_id = $item['product_id']; // Get The product ID (for simple products)
            $product = wc_get_product($item['product_id']); 
        } else {
            $product = $item->get_product();
            
            if( $product->is_type('variation') ) {
                $product = wc_get_product( $item->get_product_id() );
            }
        }

        // Get the Product description (WC version compatibility)
        if ( method_exists( $item['product'], 'get_description' ) ) {
            $product_description = $product->get_description(); // for WC 3.0+ (new)
        } else {
            $product_description = $product->post->post_content; // for WC 2.6.x or older
        }

        // Display the product description
        echo '<div class="product-description"><p>' . $product_description . '</p></div>';
    }
}

此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码已经过测试且有效.

This code is tested and works.

woocommerce_order_item_meta_end 动作钩子中的代码更新和错误解释:

Code Update and Error explanations in woocommerce_order_item_meta_end action hook:

woocommerce_order_item_meta_end (Mike Joley) 的 PHP 警告

这篇关于将产品描述添加到 WooCommerce 电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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