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 如何制作永久二维码,最好是微信也可以扫开的。(相关搜索:管理系统)
  • ¥15 delphi indy cookie 有效期
  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞
  • ¥15 请帮我看一下这个简易化学配平器的逻辑有什么问题吗?