Skip Navigation

[Resolved] CRED field in woocommerce email

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 7 replies, has 2 voices.

Last updated by Nigel 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1118572

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?

#1119007

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;

}
#1119314

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;
		}
 
}
#1119927

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.

#1120015

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.

#1120629

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...

#1120707

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;  
        } 
   
}
#1121544

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;  
  }    
}