2016-09-30 67 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 
     } 

    } 
    } 
} 
相關問題