Hi,
I've created custom field with CRED module. I use this form to buy a product.
I need to add these fields to woocommerce email.
I tried to use this code, but fields are note displayed
function add_content() {
$order_id = get_the_ID();
$cred_meta = get_post_meta( $order_id, '_cred_meta');
if(isset($cred_meta[0]['cred_post_id'])){
$adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
$child_num = get_post_meta($post_id, 'wpcf-children-number', true);
$room_num = get_post_meta($post_id, 'wpcf-room-number', true);
if($child_num < 1 ) {$child_num = 0; }
}
echo "
<table style='margin-top: 20px;'>
<body class='cart-list'>
<tr><td><strong> Adult:</strong></td> <td>$adult_num </td></tr>
<tr><td><strong> Children:</strong></td> <td>$child_num</td> </tr>
<tr><td><strong> Rooms:</strong></td> <td>$room_num </td></tr>
</body>
</table>
";
add_action( 'woocommerce_email_before_order_table', 'add_content', 10, 3);
}
Can you help me?
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
Hi Domenico
Extracting the ID of the post created by the Toolset Form from the protected WC order object is a little tricky, I spent a while getting to the bottom of this, and below is the code I got working on my test site.
My custom fields are different than yours but it should be clear from this what you need to change in your own code to get it working.
add_action( 'woocommerce_email_before_order_table', 'tssupp_mod_wc_mail', 20, 4 );
function tssupp_mod_wc_mail( $order, $sent_to_admin, $plain_text, $email ) {
$meta_data = $order->get_meta_data();
foreach ($meta_data as $key => $meta_datum) {
$meta_key = $meta_datum->key;
if ( $meta_key == '_cred_post_id' ){
$post_id = $meta_datum->value;
break;
}
}
$adults = get_post_meta( $post_id, 'wpcf-adults', true );
$short_message = get_post_meta( $post_id, 'wpcf-short-message', true );
echo '<h2>MODIFYING</h2><br>Adults: ' . $adults . '<br>Short message:' . $short_message;
}
Hi Nigel, thanks a lot!
Just i little question to fix it.
I use you code to display the field, and it works fine.
But how is possible to display it in base of a condition of another field $price_calculation?
if $price_calculation is 1 should be displayed else not.
I tried this follwing code but maybe it does not take the value of $price_calculation and it does not display me anything
add_action( 'woocommerce_email_before_order_table', 'tssupp_mod_wc_mail', 20, 4 );
function tssupp_mod_wc_mail( $order, $sent_to_admin, $plain_text, $email ) {
$meta_data = $order->get_meta_data();
foreach ($meta_data as $key => $meta_datum) {
$meta_key = $meta_datum->key;
if ( $meta_key == '_cred_post_id' ){
$post_id = $meta_datum->value;
break;
}
}
$adults = get_post_meta( $post_id, 'wpcf-adults', true );
$short_message = get_post_meta( $post_id, 'wpcf-short-message', true );
$price_calculation = get_post_meta($product_id, 'wpcf-price-calculation', true);
$email_tag = '<h2>MODIFYING</h2><br>Adults: ' . $adults . '<br>Short message:' . $short_message;
if ($price_calculation == 1 ) {
return $email_tag;
}
}
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
Hi Domenico
The woocommerce_email_before_order_table hook is an action, not a filter, so there is nothing to return.
It works by letting you echo custom content in the relevant place.
So the echo statement should reside within your if block that tests the price-comparison field.
Hi Nigel,
I tried this but does not work however:
add_action( 'woocommerce_email_before_order_table', 'tssupp_mod_wc_mail', 20, 4 );
function tssupp_mod_wc_mail( $order, $sent_to_admin, $plain_text, $email ) {
$meta_data = $order->get_meta_data();
foreach ($meta_data as $key => $meta_datum) {
$meta_key = $meta_datum->key;
if ( $meta_key == '_cred_post_id' ){
$post_id = $meta_datum->value;
break;
}
}
$adults = get_post_meta( $post_id, 'wpcf-adults', true );
$short_message = get_post_meta( $post_id, 'wpcf-short-message', true );
$price_calculation = get_post_meta($product_id, 'wpcf-price-calculation', true);
$email_tag = '<h2>MODIFYING</h2><br>Adults: ' . $adults . '<br>Short message:' . $short_message;
if($price_calculation == 1 ) {
echo email_per_person;
} else if($price_calculation == 2 ) {
echo email_per_room;
}
}
If I echo "email_per_person" without any condition it works, if I set the condition with "price-calculation" it doen not works.
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
What is email_per_person? What is email_per_room?
Variables in PHP begin with a $ sign, and I would expect the code above to give a fatal error...
Sorry this is the right code whcih not works.
It is just an example, my code is mor complexed. But however this does not works because it doen not "take" the condition
add_action( 'woocommerce_email_before_order_table', 'tssupp_mod_wc_mail', 20, 4 );
function tssupp_mod_wc_mail( $order, $sent_to_admin, $plain_text, $email ) {
$meta_data = $order->get_meta_data();
foreach ($meta_data as $key => $meta_datum) {
$meta_key = $meta_datum->key;
if ( $meta_key == '_cred_post_id' ){
$post_id = $meta_datum->value;
break;
}
}
$adults = get_post_meta( $post_id, 'wpcf-adults', true );
$short_message_person = get_post_meta( $post_id, 'wpcf-person', true );
$short_message_room = get_post_meta( $post_id, 'wpcf-room', true );
$price_calculation = get_post_meta($product_id, 'wpcf-price-calculation', true);
$email_per_person = '<h2>MODIFYING</h2><br>option 1: ' . $adults . '<br>Short message:' . $short_message_person;
$ email_per_room = '<h2>MODIFYING</h2><br>option 2: ' . $adults . '<br>Short message:' . $short_message_room;
if($price_calculation == 1 ) {
echo email_per_person;
} else if($price_calculation == 2 ) {
echo email_per_room;
}
}
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
You are still echoing the variables without the $.
Also, you are using the product_id, but where does that come from, it is not defined anywhere.
I updated your code so that the product id is extracted from the order object as well as the post id:
<?php
add_action( 'woocommerce_email_before_order_table', 'tssupp_mod_wc_mail', 20, 4 );
function tssupp_mod_wc_mail( $order, $sent_to_admin, $plain_text, $email ) {
$meta_data = $order->get_meta_data();
foreach ($meta_data as $key => $meta_datum) {
$meta_key = $meta_datum->key;
if ($meta_key == '_cred_meta') {
$cred_meta = $meta_datum->value;
$cred_meta_array = maybe_unserialize($cred_meta);
$post_id = $cred_meta_array[0]['cred_post_id'];
$product_id = $cred_meta_array[0]['cred_product_id'];
break;
}
}
$adults = get_post_meta( $post_id, 'wpcf-adults', true );
$short_message_person = get_post_meta( $post_id, 'wpcf-person', true );
$short_message_room = get_post_meta( $post_id, 'wpcf-room', true );
$price_calculation = get_post_meta( $product_id, 'wpcf-price-calculation', true);
$email_per_person = '<h2>MODIFYING</h2><br>option 1: ' . $adults . '<br>Short message:' . $short_message_person;
$email_per_room = '<h2>MODIFYING</h2><br>option 2: ' . $adults . '<br>Short message:' . $short_message_room;
if ( $price_calculation == 1 ) {
echo $email_per_person;
} else if( $price_calculation == 2 ) {
echo $email_per_room;
}
}