首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在woocommerce订单电子邮件中显示多个产品的自定义字段

在woocommerce订单电子邮件中显示多个产品的自定义字段
EN

Stack Overflow用户
提问于 2016-09-30 13:03:27
回答 1查看 1.3K关注 0票数 0

我试图在发送给客户的woocommerce电子邮件中显示许多用ACF创建的自定义字段,但我仍然无法理解如何处理多个产品订单的字段。

到目前为止,我已经用helgatheviking here的代码实现了一些目标,但是我能够同时显示来自一个产品的CF

现在,我试图找出如何将它写成一个循环,以便在同一封邮件中为许多产品显示这些字段。不幸的是,我是一个很好的复制者&过去的人,我仍然在谷歌上搜索一些方法来正确地在这个上下文中写一个循环,但是到目前为止我没有运气。你能帮帮我吗?

到目前为止,这是我的functions.php中的代码:

代码语言:javascript
复制
<?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
        }

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-03 08:48:49

解决了这样编辑代码的问题:

代码语言:javascript
复制
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
        }

    }
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39792227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档