2016-09-30 60 views
0

我试图在发送给客户的woocommerce电子邮件中显示使用ACF创建的许多自定义字段,但我一直在处理多个产品订单的字段。在woocommerce订单中显示自定义字段多个产品的电子邮件

到目前为止,我已经实现了与代码的东西通过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 
     } 

    } 
} 
+0

我知道代码是一个烂摊子,到目前为止,我不停的,如果else语句的$ field1即使我知道格式化如何,我还没有为其余字段声明,但我刚开始编辑我在之前链接的对话中建议的代码 – ironicmoka

回答

1

解决编辑代码是这样的:

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 
     } 

    } 
    } 
} 
相关问题