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 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器