有很多线程涉及“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
电子邮件中,而且还显示在所有其他电子邮件中。
因此,我尝试了以下代码片段:
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>";
}
}
}
但现在输出将不再显示在任何电子邮件中,并且会触发内部服务器错误。有什么建议吗?
最佳答案
$email
($email->id
) 不会作为参数传递给 woocommerce_order_item_meta_end
Hook ,因此它是未定义的
因此,要针对特定的电子邮件通知,需要一种解决方法,这可以通过通过 woocommerce_email_before_order_table
Hook 创建全局变量来完成
所以你得到:
// 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 );
在此答案中使用:
关于wordpress - 将自定义字段 (ACF) 添加到 WooCommerce 已完成订单电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71138648/