【问题标题】:Add custom field (ACF) to WooCommerce completed order email notification将自定义字段 (ACF) 添加到 WooCommerce 完成的订单电子邮件通知
【发布时间】:2022-02-16 20:40:15
【问题描述】:

有许多主题涉及“WooCommerce 电子邮件中的自定义字段”主题,但我找不到我正在努力解决的确切案例。

我可以实现这个项目的 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>";
    }
}

因此,此代码与自定义字段完美配合。 问题是此输出不仅显示在customer_completed_order 电子邮件中,而且还显示在所有其他电子邮件中。

因此,我尝试了这段代码sn-p:

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>";
        }
    }
}

但是现在输出将不再显示在任何电子邮件中,并且会触发内部服务器错误。有什么建议吗?

【问题讨论】:

    标签: wordpress woocommerce advanced-custom-fields email-notifications


    【解决方案1】:

    $email ($email-&gt;id) 没有作为参数传递给 woocommerce_order_item_meta_end 钩子,因此它是未定义的

    因此,要针对特定​​的电子邮件通知,需要一种解决方法,这可以通过woocommerce_email_before_order_table 挂钩创建一个全局变量来完成

    所以你得到:

    // 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 );
    

    在这个答案中使用:

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多