2
votes

Is there any way that I can add some Product custom field I create to include in WooCommerce Completed Order email.

I have created come custom fields as below :

Product custom field

And I have added some code to my theme's functions.php file, to display those custom fields values as follow:

add_action( 'woocommerce_email_after_order_table', 'add_content_on_specific_email', 20, 4 );
  
function add_content_on_specific_email( $order, $sent_to_admin, $plain_text, $email )
{
   if ( $email->id == 'customer_completed_order' ) {
      echo '<h3>Informasi Pengambilan Barang</h3><p class="email-upsell-p">Terima Kasih telah mengkonfirmasi pembayaran Anda, Silahkan tunjukan email ini pada saat pengambilan barang dan berikut informasi dan alamat pengambilan barang:</p>';
      echo '<ul><li>Alamat Pengambilan:</strong> ' . get_post_meta( $product_id, 'kontak_pemberi', true) . '</li>
      <li>No. Telepon :</strong> ' . get_post_meta( $product_id, 'no_hp_pemberi', true) . '</li>
      <li>Nama Pemberi Barang :</strong> ' . get_post_meta( $product_id, 'nama_pemberi', true) . '</li>
      </ul>';
   }
}

But I never get those custom fields values. What am I doing wrong?

1
get_post_meta( $product_id.. where is $product_id defined? also know that there are a lot of related or even duplicate questions out there. So first use the search function before asking your question7uc1f3r

1 Answers

3
votes

To get product custom fields from an order, you need to loop through order items first and then you will be able to access and display some product custom fields as follows:

add_action( 'woocommerce_email_after_order_table', 'add_custom_field_on_completed_order_email', 20, 4 );
function add_custom_field_on_completed_order_email( $order, $sent_to_admin, $plain_text, $email ) {

    if ( 'customer_completed_order' === $email->id ) :

    echo '<h3>' . __("Informasi Pengambilan Barang") . '</h3>
    <p class="email-upsell-p">' . __("Terima Kasih telah mengkonfirmasi pembayaran Anda, Silahkan tunjukan email ini pada saat pengambilan barang dan berikut informasi dan alamat pengambilan barang:") . '</p>';

    // Loop through order items
    foreach ( $order->get_items() as $item ) :

    // Get the main WC_Product Object
    $product = $item->get_variation_id() > 0 ? wc_get_product( $item->get_product_id() ) : $item->get_product();
    
    // Get product custom field values
    $kontak_pemberi = $product->get_meta('kontak_pemberi');
    $no_hp_pemberi  = $product->get_meta('no_hp_pemberi');
    $nama_pemberi   = $product->get_meta('nama_pemberi');
    
    if( ! empty($kontak_pemberi) || ! empty($no_hp_pemberi) || ! empty($nama_pemberi) ) :

    echo '<ul class="item ' . esc_html( $item->get_name() ) . '" style="list-style:none; margin:0 0 3em;">';
    
    if( ! empty($kontak_pemberi) )
        echo '<li>' . __("Alamat Pengambilan:") . '</strong> ' . $kontak_pemberi . '</li>';
    
    if( ! empty($no_hp_pemberi) )
        echo '<li>' . __("No. Telepon :") . '</strong> ' . $no_hp_pemberi . '</li>';
        
    if( ! empty($nama_pemberi) )
        echo '<li>' . __("Nama Pemberi Barang :") . '</strong> ' . $nama_pemberi . '</li>';
        
    echo '</ul>';
    
    endif;
    endforeach;
    endif;
}

Code goes in functions.php file of the active child theme (or active theme). It should works.

Note: since WooCommerce 3, you can use WC_Data method get_meta() on the WC_Product Object to get the custom field value(s) from its(their) meta key(s)…