php - 在特定的 WooCommerce 电子邮件上显示产品 ACF 字段
问题描述
所以我几乎明白了,但是指定 WooCommerce 电子邮件的最后一个细节让我有点困惑。
我需要显示产品的 ACF(高级自定义字段)字段(在这种情况下是自定义运输时间)
- 但仅在新订单电子邮件中(处理订单并等待发送给客户的付款订单),然后将新订单电子邮件发送给管理员。
这是我发现的主要方式:“在 Woocommerce 交易电子邮件中显示产品 ACF 字段值”提前谢谢@LoicTheAztec
我还为其添加了一些条件设置(请注意,我的 PHP 刚刚开始复制粘贴),这些设置运行良好。
但是,我无法解决的是使其仅适用于新订单电子邮件。我有这样的设置,它可以工作,但是它显示在所有包含电子邮件订单详细信息的电子邮件上,并且我无法在已完成的订单电子邮件上显示运输时间,因为它会造成混乱。
// 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;
}
我试过切换
if ( 'new_order' == $email->id )
至
if ( 'new_order' != $email->id )
但这只会让它在任何地方都不起作用。
我也觉得可能是这个部分
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 )
但这会使电子邮件返回错误。
解决方案
通常这应该与下面的代码一起使用
注意:我的回答主要基于:“仅为 WooCommerce 管理员电子邮件通知自定义订单项元数据”。学分:@ Loictheaztec所以不要忘记支持这个答案!
// 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 );
推荐阅读
- java - 我有一个字符串包含一个重复的单词,例如 72 次,我想将文本拆分为许多字符串,这些字符串包含该单词之间的子字符串
- python - 如何显示来自 Django CharField 的图像?
- angular - 减法方法返回 NaN 结果
- sql - 我有一个带有联合的 SQL 查询。如果第一个查询中的字段不包含列中的结果,我只需要在第二个查询中包含数据
- angular - Angular,如何从 JSON 订阅特定数据?
- git - Pycharm没有清除文件的git状态
- vue.js - Vuetify 组合框未在 Enter 上创建新对象
- git - 在 VS 2019 中使用 gitmessage 模板
- keras - 数据增强:steps_per_epoch 到底是什么意思?
- elasticsearch - Elasticsearch:无法使用过滤器和排序查询布尔嵌套查询