Skip Navigation

[Resolved] Notification if field of any child post is more than X

This support ticket is created 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 21 replies, has 3 voices.

Last updated by Waqar 2 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2438171

I have built a requisition system. There are forms to create requisitions on the front end, and forms to add items to the requisition.

I need to setup a conditional notification that is sent if the price field of any of the items on a requisition is more than a certain value.

Can you help me figure out how to accomplish this, please?

It might be best to enable private replies on this one.

#2438273

So, what I have done so far is to create a view that lists the prices of all items on the current requisiton.

I have a form that is just a button that submits the requisition for approval. I currently have some php code that updates a status field on submission of that form. I could add some more code for that form that loops through the list of prices and checks if any is above the set value, and if so then the notification will fire.

How can I accomplish this? Thank you in advance.

#2438285

It appears this is similar to what I need:

https://toolset.com/forums/topic/loop-through-repeated-fields-with-php/

But how can I make it evaluate a field of the children of the current post?

#2438295

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

Thank you for getting in touch.

Based on what I see here, you will require some amount of custom coding to achieve this.

Unfortunately the custom code required to do this is not something we can provide as this would be out of the scope of what we can provide as support.

My recommendation here is that you contact one of the registered contractors to see if this is something that they can do for you.

You can get in touch with them by going to the link below.
https://toolset.com/contractors/

Thanks,
Shane

#2438297

Um.... This isn't going to take a bunch of custom coding.

$tests = get_post_meta('wpcf-test-upload-repeater');
foreach ((array)$tests as $test) {
  echo $test . '<br />';
}

That's the bulk of the code right there.

I just need to modify that ever so slightly to make it read the fields of the children of the current post.

Once that's done, I'm pretty sure I can have it update a hidden field in the post and send the notification based on that field value change.

Just need to know how to read values of fields in the children of current post in PHP.

Maybe another supporter can help me.

#2438333

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

It's actually a bit more complex that this.

You're setting the relationship on the form ? Or is it a case where you're editing the Child post and you want to send a notification if the field value changes.

Either way you're talking about working through the relationships which will involve the relationship API and then you're going to have to use this hook below to trigger that notification.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_event_type

The code you are looking at above will only loop through the current post being viewed fields, not the child post fields. Also it gets more complex if the post has many child posts.

Thanks,
Shane

#2438343

It might be easiest if you login and look at the site.

There may be more than one way to do this.

There is a parent post type and a child post type. The children post types are all created by a form embedded in the content of the parent. There is also a view that displays the children embedded in the template of the parent.

Then there is a form that 'finalizes' the parent. My thought is that the form that finalizes the parent could have a field that is set conditionally if a child item has a value higher than a certain number in a particular field.

I know this requires working with the API. I need help doing that. I can get there, but I need help doing so. I don't have a budget for hiring a contractor.

Here's where I'm at so far:

 	// get child posts of current post
    $args = array(
        'post_type'     =>   'req-item',
        'post_status'   =>   'publish',
        'numberposts'   =>   -1,
        'meta_key'      =>   '_wpcf_belongs_requisition_id',
        'meta_value'    =>   get_the_ID()
    );

    $child_posts = get_posts( $args );

    $tests = get_post_meta('wpcf-special-item-price');
		foreach ((array)$tests as $test) {
  			echo $test . '<br />';
		}

I know this is missing a few pieces, and i think the wpcf_belongs_ thing is deprecated but, again, this is why i need help.

#2438345

I'm not asking you to code a solution for me, i'm asking you to help me code a solution myself.

#2439053

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

No problem. As you're aware the wpcf_belongs_to is no longer being used for post relationship. The function that you should be using to retrieve the child post is below.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

But i'm assuming here that you will be editing the Child post and you only want to send the notification if a value for the repeating field is below/greater than X.

What i'm unsure of is if the form you're using is being used to edit the Child or the Parent post and are those fields that should trigger the notification on the form itself.

Thanks,
Shane

#2439075

The form that I want the notification to be triggered by is editing the parent, but the fields I'm checking the values of are in the children.

#2439609

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi James,

So when you edit the parent you're going to have to get the child posts with this function below.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

From there you will need to loop through the repeated fields to check each one. Then trigger the notification with the hook below.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_event_type

Please let me know if further clarification is needed.

Thanks,
Shane

#2443801

I'm a bit indisposed due to family concerns, but I'll come back to this asap. Please keep the ticket open for me. Thank you!

#2467057

Hi James,

Shane has been moved to a different department, so I'll follow up on this ticket.

Please take your time; this ticket will stay open for a couple of weeks. Even if it gets closed and you have some follow-up questions, you can start a new one and include the link to this ticket and we'll have the background info.

regards,
Waqar

#2467299

Hi Waqar,

I haven't had a ton of time to focus on this, but here's what I have put together so far.

I have not idea if the loop works correctly, or how to check if it is. I also need help with getting the notification to fire if 'wpcf-special-item-price' is greater than 999.

    elseif ($form_data['id']==116)//---------------------------------Actions on submit for approval
    {
		update_post_meta($post_id, 'wpcf-status', 'Pending');
		update_post_meta($post_id, 'wpcf-date-submitted', $now);
		
		$origin_id = $atts['child'];
		$relationship_slug = 'requisition-to-item';
		 
		$child_posts = toolset_get_related_posts(
		    $origin_id, // get posts connected to this one
		    $relationship_slug, // in this relationship
		    array(
		        'query_by_role' => 'parent', // origin post role
		        'role_to_return' => 'child', // role of posts to return
		        'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
		        'limit' => 999, // max number of results
		        'offset' => 0, // starting from
		        'orderby' => 'title', 
		        'order' => 'ASC',
		        'need_found_rows' => false, // also return count of results
		        'args' => null // for adding meta queries etc.
		    )
		);
		foreach ($child_posts as &$value) {
			$cost == 'wpcf-special-item-price';
		    if($cost >= 999){
		    	//fire price notification to business manager
		    };
		}
    }

Thank you for any help. I'm still in a place where this isn't my top focus, but it is something I need to get done at some point.

#2469229

Thank you for sharing this.

The logic of the code that you've shared and what you've discussed so far with Shane makes sense and it shouldn't be too complicated to achieve through some custom code.

When your form to submit the final requisition post (parent) is ready, can you please share temporary admin login details, along with the link to the page with that form?

I'll be in a better position to suggest some more specific pointers, accordingly.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.