简体   繁体   English

将自定义字段 (ACF) 添加到 WooCommerce 完成订单 email 通知

[英]Add custom field (ACF) to WooCommerce completed order email notification

There are many threads which deal with the topic "custom fields in WooCommerce emails" but I couldn't find the exact case I am struggling with.有很多线程处理“WooCommerce 电子邮件中的自定义字段”主题,但我找不到我正在努力解决的确切情况。

I could achieve 50% of this project to display the field as a meta in the order table.我可以实现该项目的 50% 以将字段显示为订单表中的元数据。

add_action( 'woocommerce_order_item_meta_end', 'custom_product_info', 20, 4 );
function custom_product_info ( $item_id, $item, $order, $plain_text ) {
    $id = $item->get_product_id();
    $erlebnisidgc = get_field('erlebnis_id',$id);
    if($erlebnisidgc){
        echo "<p>Here's your Link</p><a href=https://b-ceed.de/?eventid='.$erlebnisidgc'>Testlink</a>";
    }
}

So this code works perfectly with the custom field.因此,此代码与自定义字段完美配合。 Problem is that this output isn't shown only in the customer_completed_order email but also in all other emails.问题是这个 output 不仅显示在customer_completed_order email 中,而且还显示在所有其他电子邮件中。

Therefore, I tried this code snippet:因此,我尝试了这个代码片段:

add_action( 'woocommerce_order_item_meta_end', 'custom_product_info', 20, 4 );
function custom_product_info ( $item_id, $item, $order, $plain_text ) {
    if ( $email->id == 'customer_completed_order' ) {
        $id = $item->get_product_id();
        $erlebnisidgc = get_field('erlebnis_id',$id);
        if($erlebnisidgc){
            echo "<p>Here's your Link</p><a href=https://b-ceed.de/?eventid='.$erlebnisidgc'>Testlink</a>";
        }
    }
}

But now the output won't be displayed in any email anymore and it triggers a internal server error.但是现在 output 将不再显示在任何 email 中,它会触发内部服务器错误。 Any advice?有什么建议吗?

$email ( $email->id ) is not passed as argument to the woocommerce_order_item_meta_end hook, therefore it is undefined $email ( $email->id ) 没有作为参数传递给woocommerce_order_item_meta_end钩子,因此它是未定义的

So to target specific email notifications a workaround will be needed, this can be done by creating a global variable via the woocommerce_email_before_order_table hook因此,要针对特定的 email 通知,需要一种解决方法,这可以通过woocommerce_email_before_order_table钩子创建一个全局变量来完成

So you get:所以你得到:

// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    $GLOBALS['email_data'] = array(
        'email_id'  => $email->id, // The email ID (to target specific email notification)
        'is_email'  => true // When it concerns a WooCommerce email notification
    );
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
 
function action_woocommerce_order_item_meta_end( $item_id, $item, $order, $plain_text ) {
    // Getting the custom 'email_data' global variable
    $ref_name_globals_var = $GLOBALS;
    
    // Isset & NOT empty
    if ( isset ( $ref_name_globals_var ) && ! empty( $ref_name_globals_var ) ) {
        // Isset
        $email_data = isset( $ref_name_globals_var['email_data'] ) ? $ref_name_globals_var['email_data'] : '';
        
        // NOT empty
        if ( ! empty( $email_data ) ) {
            // Target specific emails, several can be added in the array, separated by a comma
            $target_emails = array( 'customer_completed_order' );
            
            // Target specific WooCommerce email notifications
            if ( in_array( $email_data['email_id'], $target_emails ) ) {
                // Get product ID
                $product_id = $item->get_product_id();
                
                // Get field
                $erlebnisidgc = get_field( 'erlebnis_id', $product_id );
                
                // Has some value
                if ( $erlebnisidgc ) {
                    echo '<p>Here is your Link</p>';
                    echo '<a href="https://b-ceed.de/?eventid=' . $erlebnisidgc . '">Testlink</a>';
                }
            }           
        }
    }
}
add_action( 'woocommerce_order_item_meta_end', 'action_woocommerce_order_item_meta_end', 10, 4 );

Used in this answer:在这个答案中使用:

暂无
暂无

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

相关问题 Woocommerce订单已完成电子邮件通知 - Woocommerce Order Completed Email notification 将可变产品的“描述”字段内容添加到 woocommerce “已完成订单” email - Add "description" field contents for variable product to woocommerce "Completed order" email 在 WooCommerce 'Order Completed' 电子邮件中添加自定义产品字段 - Add Custom Product Field in WooCommerce 'Order Completed' Emails 在 WooCommerce 订单中显示 ACF 订单自定义字段 - Display ACF order custom field in WooCommerce orders 在新订单Woocommerce电子邮件通知中添加自定义链接 - Add a custom link in new order Woocommerce email notification 我需要禁用在账单 email 字段 woocommerce 中指定的电子邮件地址的已完成订单通知 - I need to disable notification of a completed order for emails addresses that are specified in the billing email field woocommerce WooCommerce 4.0 自定义结帐和电子邮件、管理员订单和感谢页面上的 ACF 字段值 - WooCommerce 4.0 custom checkout & ACF field value on email, admin order, and thank you page 从Woocommerce中已完成的订单电子邮件通知中删除订单明细表 - Remove order details table from completed order email notification in Woocommerce 在 WooCommerce 客户完成订单电子邮件通知中添加基于运输方式 ID 的消息 - Add message based on shipping method ID in WooCommerce customer completed order email notification 通过 WooCommerce 管理员电子邮件通知中的“woocommerce_email_order_meta”挂钩显示产品自定义字段 - Display product custom field via "woocommerce_email_order_meta" hook in WooCommerce admin email notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM