1

I have a custom field for a Woocommerce's product, and I want to display it's value in order emails.

As I'm using custom product submission form, I added this code for custom field below to create a custom field:

<?php 
    WCVendors_Pro_Form_Helper::select( array(  
        'post_id'       => $object_id,
        'class'         => 'select2',
        'id'            => 'wcv_custom_product_ingredients', 
        'label'         => __( 'What time?', 'wcvendors-pro' ), 
        'placeholder'   => __( 'Pick a time', 'wcvendors-pro' ),
        'wrapper_start' => '<div class="all-100">',
        'wrapper_end'   => '</div>',
        'desc_tip'      => 'true', 
        'description'   => __( 'Pick a time', 'wcvendors-pro' ),
        'options'       => array( '12:00 midnight' => __('12:00 midnight', 'wcv_custom_product_ingredients'), '12:15 midnight'=> __('12:15 midnight', 'wcv_custom_product_ingredients') )
) );
?>

I also tried adding code below to functions.php, but this only displays "What time?" without value in order emails...

add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email');
function wcv_ingredients_email() {
    $output = get_post_meta( get_the_ID(), 'wcv_custom_product_ingredients', true );
    echo 'What time? ' . $output . '<br>';
}

What could be the issue?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Marko I.
  • 542
  • 10
  • 38

1 Answers1

3

You are using the correct hook, but you just forgot the hook arguments in the hooked function.

Also as you are targeting a product custom field value, and as in an order you can have many items (products), the code below will display this value for each order item.

Try the code below, it works now:

// Tested on WooCommerce version 2.6.x and 3+ — For simple products only.
add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email', 10, 4);
function wcv_ingredients_email( $order,  $sent_to_admin,  $plain_text,  $email ){
    foreach($order->get_items() as $item_values){
        // Get the product ID for simple products (not variable ones)
        $product_id = $item_values['product_id'];
        $output = get_post_meta( $product_id, 'wcv_custom_product_ingredients', true );
        echo 'What time? ' . $output . '<br>';
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, thanks so much for this! Would you be kind to tell me how to include values from this custom field on woocommerce's order pages? – Marko I. Apr 13 '17 at 15:50
  • @MarkoI. Do you mean in order receive page and my account order view page (customer order pages)? – LoicTheAztec Apr 13 '17 at 16:08
  • @MarkoI. I have certainly answer this already more than once. I need to retrieve my answers. Where do you want to output that exactly? – LoicTheAztec Apr 13 '17 at 16:13
  • I want to output it in order details table at https://example.com/my-account/view-order/00000/ , that is, on single view order page. (Under the product name would be great.). And also in the order details table when order is received, on this page: https://example.com/checkout/order-received/00000/?key=wc_order_58ef9bf590bcf – Marko I. Apr 13 '17 at 16:20