Hi Waqar,
thank you for your answer.
the problem, is that I'm creating the message inside the product with the popup form, and the relationship is created before the creation of the message.
With your solution implicates that i have to edit another time, the mesage and put the yes when the relationship is done.
I have another one similar yours.
I create the field "Sended", with no data.
And once I created the message, and Update/save my product (I know that is not necessary, but all the users done it).
I check all the relationship with the Sended ==null, and fill with the datetime, for end only 1 time.
I attach my new version:
Alvaro
add_action( 'save_post', 'envia_mensaje_inversores',30,2 );
function envia_mensaje_inversores( $post_id, $post ) {
// if ( 'mensaje' == $post->post_type ) {
if ('product' == $post->post_type) {
// If this is just a revision, don't send the email.
if ( get_post_status( $post_id )!='publish' )
return;
// $proyecto_id = toolset_get_related_post( $post_id, 'producto-mensaje', 'child');
//debug_to_console( "E:".$post_id."-".$proyecto_id);
$mensajes = toolset_get_related_posts( $post_id, 'producto-mensaje', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );
if ($mensajes==0){
}else{
foreach ($mensajes as $mensaje) {
$enviado= types_render_field( "enviado-mensaje", array( "id"=> $mensaje->ID));
update_post_meta( $mensaje->ID, 'wpcf-enviado-mensaje', date('d-m-Y') );
if ( ($enviado==null)||($enviado=='') ){
//Listado de ordenes que han invertido en este proyecto
$order_list = WPNEOCF()->getCustomersByProductPost($post_id);
$emails=array();
foreach ($order_list as $order) {
$orden = wc_get_order( $order );
//echo"O:<pre>";print_r($order);echo"</pre>";
$user_id = $orden->get_user_id();
$headers[] ='BCC: '.get_user_meta( $user_id, 'billing_email', true );
}
//Clientes que han creado una orden filtrado por la orden (invertido en este proyecto)
//echo "E:".$proyecto_id."-<pre>";print_r($emails);echo"</pre>";die();
$titulo_inversion = get_the_title($proyecto_id);
$post_url = get_permalink( $proyecto_id );
$to = "info@crowdplan.es";
$subject = 'Nuevo mensaje de la inversión: '. $titulo_inversion;
$asunto= types_render_field( "asunto-mensaje", array( "id"=> $mensaje->ID));
$fecha = types_render_field( "fecha-mensaje", array( "id"=> $mensaje->ID));
$fecha = date_i18n( "d-m-Y", strtotime( $fecha ) );
$cuerpo= types_render_field( "contenido-mensaje", array( "id"=> $mensaje->ID));
$message = $asunto . "\n\n". $fecha . "\n\n".$cuerpo;
//echo "M:<br>".$mensaje; die();
// Send email to admin.
wp_mail( $to, $subject, $message, $headers );
}
}
}
}
}