Skip Navigation

[Resolved] Adding up multiple child types to populate a field

This support ticket is created 4 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/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 3 voices.

Last updated by Mordechai 4 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1819375

Hi! I'm building a peer-to-peer crowdfunding site, and I'd like to be able to display a total amount raised from all the donations to that campaign. There's a Campaign type, and a Donation type. Each Campaign can have multiple donations. How would I add up all of the donations of a single campaign to populate an 'Amount Raised' field in the campaign?

Thank you!

#1819829

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You'd need some coding to do that.

You could create a View to iterate over the child donations of the current campaign, adding a custom shortcode to handle the maths (there is no built-in calculation ability), but you would need to use the legacy Views builder and it involves writing custom code anyway, so you would probably be better off skipping that View and just writing the custom shortcode directly.

You would use the relationships API to get all of the child donation posts, do the calculation, and return the total.

The API function you will need is: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

You can read about writing custom shortcodes here: https://developer.wordpress.org/plugins/shortcodes/

#1819843

I created a shortcode, but it doesn't seem to be working..

The get_posts function doesn't seem to actually be populating the children array. Any ideas of what could be wrong?

Thank you!

add_shortcode('sum-of-donations', 'sum_of_donations_func');
function sum_of_donations_func(){
    $campaignID = get_the_ID(); 
	//echo $campaignID;
  	
    $children = get_posts(
        array(
            'post_type'=> 'llcdonation',
            'meta_query' => array(
                array(
                    'key'     => '_wpcf_belongs_llccampaign_id',
                    'value'   => $campaignID,
                ),
            ),
            'fields' => 'ids'
        )
    );
  
  	echo $children;
  echo json_encode($children);
 	echo "er?";
    $sum = 9;
    foreach($children as $id){
		echo "YAAAAAA";
      	$sum = 3;
        $sum += get_post_meta( $id, 'wpcf-total-llc-donation-amount', true);
    }
    return $sum;
}
#1821753

In the new Toolset post type relationship, we have introduced new relationship API function toolset_get_related_posts(), please check our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

And setup your custom PHP codes

#1822053

Gotcha. So I'm using the are new API, but it's still returning an empty array. Anything I'm doing wrong here?
Thanks!


	$children = toolset_get_related_posts(
      // Post representing a writer (or post ID), 
      // to which the results should be related.
      $campaignID,

      // Relationship slug.
      'campaign-donation',

      // Additional arguments.
      [
          // Specify the role to query by and to return. 
          // Notice that we can use "other" in this situation.
          'query_by_role' => 'child',
          'role_to_return' => 'all',
          // Ordering
          'orderby' => 'llc-donation-amount', 
          'order' => 'ASC'
      ]
  );

#1822081

How do you setup the custom field "llc-donation-amount"? if it is created with Toolset Types plugin, you will add prefix "wpcf-" before the field slug, for exmaple "wpcf-llc-donation-amount"

#1822087

Got it. By switching the 'role', I got results. Issue is now the loop isn't adding up the amount_raised field. Any thoughts? Thank you!!!!

	$children = toolset_get_related_posts(
      // Post representing a writer (or post ID), 
      // to which the results should be related.
      $campaignID,

      // Relationship slug.
      'campaign-donation',

      // Additional arguments.
      [
          // Specify the role to query by and to return. 
          // Notice that we can use "other" in this situation.
          'query_by_role' => 'parent',
          'role_to_return' => 'all'
      ]
  );
  	
 
    $sum = 0;
    foreach($children as $id){
        $sum += get_post_meta( $id, 'wpcf-llc-donation-amount', true);
    }
    return $sum;
#1822111

Lines 20
foreach($children as $id){

It should be like this:

  foreach($children as $child){
$id = $child->ID;

You can debug your codes easily, by output each PHP variable with function var_dump():
hidden link

#1822213

I switched that out, but it's still not returning anything. I did a var_dump on the child and I got the following:

 array(3) { ["parent"]=> int(125) ["child"]=> int(129) ["intermediary"]=> int(0) } array(3) { ["parent"]=> int(125) ["child"]=> int(131) ["intermediary"]=> int(0) } 

Doesn't seem like there's any info about the actual fields on the child type.

Any ideas? Thanks!!

#1824291

See the result you mentioned above:
["child"]=> int(129)

Number 129 is the child post ID, you can use it to get child post information, if you want to get custom field, you can try with WP function:
get_post_meta()

More help:
https://developer.wordpress.org/reference/functions/get_post_meta/

#1826051

My issue is resolved now. Thank you!