Skip Navigation

[Resolved] hook relationship

This support ticket is created 6 years, 3 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 6 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1151240

I have a product woocomerce post-type that has a relationship 1-N with a custom post-type message.
When I edit the porduct and add 1 message, I want to send an email to all the client that has ordered this product.

I'm using the new types 3.0, and I can obtain the result if I use the add message popup form inside the product.

It seems that first create the post, second publish the post and 3th atacch the post, and I want to hook, this event, when the message is attached to the product.

Could you help me?

function envia_mensaje_inversores( $post_id, $post ) {

if ( 'mensaje' == $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', 'parent');

//debug_to_console( "E:".$post_id."-".$proyecto_id);

if (($proyecto_id==null)||($proyecto_id==0)){

}else{
//Listado de ordenes que han invertido en este proyecto

$order_list = WPNEOCF()->getCustomersByProductPost($proyecto_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 );

//$emails[] = $user->get_billing_email();
//echo "O:".$order;
}

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

//$message = types_render_field( 'asunto-mensaje') ."'\n\n'";
//$message .= types_render_field( 'contenido-mensaje');
$message = "HOLA";
//echo "M:<br>".$mensaje; die();
// Send email to admin.

wp_mail( $to, $subject, $message, $headers );

}

}
}

add_action( 'save_post', 'envia_mensaje_inversores',30,2 );

#1151264

Hi Alvaro,

Thank you for contacting us and I'll be happy to assist.

There is no direct hook available that can be used to execute a function at the time of two posts are linked in a relationship.

But you can use a workaround which is safer and can avoid accidental executions and email messages.

1. For your post type "mensaje" you can add a new single line custom field, e.g. "Send message now" ( slug: send-message-now )

2. You can update your function to include the check for this new field's value, i.e. you can make it fire only when the "mensaje" post type is saved or updated and that field's value is "yes":


function process_message_post( $post_id, $post ) {
   
	if ( get_post_type( $post_id ) == 'mensaje' ) {
    
		// get the value of the message field
		$message_status  = get_post_meta($post_id, 'wpcf-send-message-now', true);

		if ($message_status == 'yes') {

			// include code to send the email here

			// reset/empty value of the message field
			update_post_meta( $post_id, 'wpcf-send-message-now', '', 'yes');

		}
		
	}
        
}
add_action( 'save_post', 'process_message_post', 99 );

Note: Please replace the comment line "// include code to send the email here" with your actual code to send the email message.

As a result, whenever you'd like to send a message attached to any product, you'll go to that message's edit screen and type "yes" in its custom field and the code will execute.

I hope this helps.

regards,
Waqar

#1151370

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

}
}

}
}

#1151869

Hi Alvaro,

Thank you for sharing the details and the updated code.

I'm sure this will prove useful for other users, with a similar requirement.

Glad this is sorted out now and you're welcome to open a new ticket for a different question or concern.

regards,
Waqar