duanbing2963 2016-09-30 13:03
浏览 35
已采纳

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

I'm trying to display many custom fields created with ACF in the woocommerce email sent to the customer but I'm stuck on how handle the fields for a multiple product order.

So far I've achieved something with the code suggested by helgatheviking here but I'm able to display just CF from 1 product at once

Now I'm trying to figure out how to write it as a loop in order to display these fields for many products in the same mail. Unfortunately I'm a good copy&past guy, I'm still googling some way to correctly write a loop in this context but I had no luck so far. Can you help me?

here is the code in my functions.php so far:

<?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 . "

";

    } 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
        }

    }
}
  • 写回答

1条回答 默认 最新

  • dougaoshang0237 2016-10-03 08:48
    关注

    Solved editing the code this way:

    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 . "
    
    ";
    
        } 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
            }
    
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)