在特定的WooCommerce电子邮件上显示产品ACF字段 [英] Display product ACF fields on a specific WooCommerce email
问题描述
所以我几乎明白了,但是最后指定WooCommerce电子邮件的细节使我有些疑惑.
So I almost got it, but this last detail of specifying WooCommerce emails is leaving my head in a bit of a knot here.
我需要显示产品的ACF(高级自定义字段)字段(在这种情况下,这是自定义发货时间)
I need to show ACF (advanced custom fields) fields of products (in this case it's a custom shipping time)
- 但是仅在新订单电子邮件中(处理订单和等待付款的订单已发送给客户),然后在新订单电子邮件中发送给管理员.
这是我发现的主要方式:显示产品Woocommerce交易电子邮件中的ACF字段值" 提前谢谢@ LoicTheAztec
This was the main way I found: "Display product ACF field value in Woocommerce transaction emails" Thank you in advance @LoicTheAztec
我还向它添加了一些条件设置(请注意,我的PHP刚开始是复制粘贴的)效果很好.
I also added some conditional settings to it (mind me my PHP is very beginning copy-pasty) which are working pretty well.
但是,我无法解决的问题是只能在新订购的电子邮件上使用它.我具有这样的设置,并且可以正常工作,但是它会在包含电子邮件订单详细信息的所有电子邮件中显示,并且无法在完成的订单电子邮件中显示运送时间,因为这会造成混乱.
However what I can't get around is making it work only on new order emails. I have it setup like this, and it works, however it shows on all emails that contain the email order details, and I can't have the shipping time showing on the completed order emails as it will create confusion.
// Display product ACF custom field value shipping in email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Targeting email notifications only
if ( 'new_order' == $email->id )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
$othershipping = get_field( 'shipping_custom', $product->get_id());
if( $shpng_value = get_field('shipping_', $product->get_id())== "24h") {
$item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
<strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>Get it tomorrow(24h)</p>' . '</p>';
}
elseif( $shpng_value = get_field('shipping_', $product->get_id())== "2-5 days") {
$item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
<strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>2-5 days</p>' . '</p>';
}
elseif( $shpng_value = get_field('shipping_', $product->get_id())== "other") {
$item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
<strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . $othershipping . '</p>';
}
return $item_name;
}
我尝试过切换
I have tried switching the
if ( 'new_order' == $email->id )
到
if ( 'new_order' != $email->id )
但这只是使其无法在任何地方使用.
But that just makes it not work anywhere.
我也认为可能是这部分
function custom_order_item_name( $item_name, $item ) {
我需要添加($ order,$ sent_to_admin,$ plain_text,$ email)
function custom_order_item_name( $item_name, $item, $order, $sent_to_admin, $plain_text, $email )
但这会使电子邮件返回错误.
But it makes the email return an error.
推荐答案
通常,该代码应与下面的代码一起使用
Normally this should work with the code below
注意:我的回答主要基于:仅针对WooCommerce管理员电子邮件通知自定义订单商品的元数据" .评分:@ Loictheaztec ,所以请不要忘记对该答案进行投票!
Note: my answer is largely based on: "Customize order item meta only for WooCommerce admin email notifications". CREDITS: @Loictheaztec so don't forget to upvote that answer to!
// Setting the "sent_to_admin" as a global variable
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email) {
$GLOBALS['email_data'] = array(
'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
'email_id' => $email->id, // The email ID (to target specific email notification)
);
}
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);
function custom_order_item_name( $item_name, $item ) {
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the custom 'email_data' global variable
$refNameGlobalsVar = $GLOBALS;
$email_data = $refNameGlobalsVar['email_data'];
// Only for new order
if( is_array( $email_data ) && $email_data['email_id'] == 'new_order' ) {
// Get the WC_Product object (from order item)
$product = $item->get_product();
$othershipping = get_field( 'shipping_custom', $product->get_id());
if( $shpng_value = get_field('shipping_', $product->get_id())== "24h") {
$item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
<strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>Get it tomorrow(24h)</p>' . '</p>';
}
elseif( $shpng_value = get_field('shipping_', $product->get_id())== "2-5 days") {
$item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
<strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>2-5 days</p>' . '</p>';
}
elseif( $shpng_value = get_field('shipping_', $product->get_id())== "other") {
$item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
<strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . $othershipping . '</p>';
}
}
}
return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
这篇关于在特定的WooCommerce电子邮件上显示产品ACF字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!