在多个产品的 woocommerce 订单电子邮件中显示自定义字段
displaying custom fields in woocommerce order email for multiple products
我正在尝试在发送给客户的 woocommerce 电子邮件中显示许多使用 ACF 创建的自定义字段,但我仍然不知道如何处理多个产品订单的字段。
到目前为止,我已经使用 helgatheviking here 建议的代码取得了一些成就,但我只能一次显示 1 个产品的 CF
现在我想弄清楚如何将它写成一个循环,以便在同一封邮件中显示许多产品的这些字段。不幸的是,我是一个很好的复制和过去的人,我仍在谷歌搜索一些方法来在这种情况下正确地编写一个循环,但到目前为止我没有运气。你能帮帮我吗?
到目前为止,这是我 functions.php 中的代码:
<?php
add_action( 'woocommerce_email_order_details', 'my_custom_order_details', 5, 4 );
function my_custom_order_details( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id == "customer_on_hold_order" ){
$field1 = null;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$field1 = get_post_meta($product_id, 'field-1', true);
$field2 = get_post_meta($product_id, 'field-2', true);
$field3 = get_post_meta($product_id, 'field-3', true);
$field4 = get_post_meta($product_id, 'field-4', true);
}
if( $field1 && $plain_text ){
echo "Field 1: " . $field1 . "\n\n";
} else if( $field1 && ! $plain_text ){
<h2>My custom fields infos:</h2>
<p><strong>Product Name:</strong> <?php echo $product_name ?></p>
<p><strong>Field 1:</strong> <?php echo $field1 ?></p>
<p><strong>Field 2:</strong> <?php echo $field2 ?></p>
<p><strong>Field 3:</strong> <?php echo $field3 ?></p>
<p><strong>Field 4:</strong> <?php echo $field4 ?></p>
<?php
}
}
}
以这种方式解决了编辑代码问题:
add_action( 'woocommerce_email_order_details', 'my_custom_order_details', 5, 4 );
function my_custom_order_details( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id == "customer_on_hold_order" ){
$field1 = null;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$field1 = get_post_meta($product_id, 'field-1', true);
$field2 = get_post_meta($product_id, 'field-2', true);
$field3 = get_post_meta($product_id, 'field-3', true);
$field4 = get_post_meta($product_id, 'field-4', true);
if( $field1 && $plain_text ){
echo "Field 1: " . $field1 . "\n\n";
} else if( $field1 && ! $plain_text ){
<h2>My custom fields infos:</h2>
<p><strong>Product Name:</strong> <?php echo $product_name ?></p>
<p><strong>Field 1:</strong> <?php echo $field1 ?></p>
<p><strong>Field 2:</strong> <?php echo $field2 ?></p>
<p><strong>Field 3:</strong> <?php echo $field3 ?></p>
<p><strong>Field 4:</strong> <?php echo $field4 ?></p>
<?php
}
}
}
}
我正在尝试在发送给客户的 woocommerce 电子邮件中显示许多使用 ACF 创建的自定义字段,但我仍然不知道如何处理多个产品订单的字段。
到目前为止,我已经使用 helgatheviking here 建议的代码取得了一些成就,但我只能一次显示 1 个产品的 CF
现在我想弄清楚如何将它写成一个循环,以便在同一封邮件中显示许多产品的这些字段。不幸的是,我是一个很好的复制和过去的人,我仍在谷歌搜索一些方法来在这种情况下正确地编写一个循环,但到目前为止我没有运气。你能帮帮我吗?
到目前为止,这是我 functions.php 中的代码:
<?php
add_action( 'woocommerce_email_order_details', 'my_custom_order_details', 5, 4 );
function my_custom_order_details( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id == "customer_on_hold_order" ){
$field1 = null;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$field1 = get_post_meta($product_id, 'field-1', true);
$field2 = get_post_meta($product_id, 'field-2', true);
$field3 = get_post_meta($product_id, 'field-3', true);
$field4 = get_post_meta($product_id, 'field-4', true);
}
if( $field1 && $plain_text ){
echo "Field 1: " . $field1 . "\n\n";
} else if( $field1 && ! $plain_text ){
<h2>My custom fields infos:</h2>
<p><strong>Product Name:</strong> <?php echo $product_name ?></p>
<p><strong>Field 1:</strong> <?php echo $field1 ?></p>
<p><strong>Field 2:</strong> <?php echo $field2 ?></p>
<p><strong>Field 3:</strong> <?php echo $field3 ?></p>
<p><strong>Field 4:</strong> <?php echo $field4 ?></p>
<?php
}
}
}
以这种方式解决了编辑代码问题:
add_action( 'woocommerce_email_order_details', 'my_custom_order_details', 5, 4 );
function my_custom_order_details( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id == "customer_on_hold_order" ){
$field1 = null;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$field1 = get_post_meta($product_id, 'field-1', true);
$field2 = get_post_meta($product_id, 'field-2', true);
$field3 = get_post_meta($product_id, 'field-3', true);
$field4 = get_post_meta($product_id, 'field-4', true);
if( $field1 && $plain_text ){
echo "Field 1: " . $field1 . "\n\n";
} else if( $field1 && ! $plain_text ){
<h2>My custom fields infos:</h2>
<p><strong>Product Name:</strong> <?php echo $product_name ?></p>
<p><strong>Field 1:</strong> <?php echo $field1 ?></p>
<p><strong>Field 2:</strong> <?php echo $field2 ?></p>
<p><strong>Field 3:</strong> <?php echo $field3 ?></p>
<p><strong>Field 4:</strong> <?php echo $field4 ?></p>
<?php
}
}
}
}