Skip Navigation

[Resolved] Attempting to make math calculation using numbers output by views

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 3 replies, has 1 voice.

Last updated by davidL-7 2 weeks, 6 days ago.

Assisted by: Minesh.

Author
Posts
#2800792
Screenshot 2025-03-14 at 11.59.09 AM.png

Tell us what you are trying to do?

I am trying to calculate the "Amount Remaining" for an Aid Account, which is the result of subtracting the sum of a series of amounts output by the view (the amounts of Aid Requests assigned to the Aid Account), subtracted from the dollar amount allocated for that Aid Account.

Here's a link to the page showing the work in progress:
hidden link

So this particular example displays a view ("Account Summary by Year") looping through all of the Aid Requests assigned to the Aid Account named "4 Roundup: Resource Stewardship & Science Projects" for the year 2023.

For each Aid Request, I'm calculating the amount in a separate view an outputting it like so [wpv-view name="sum-cost-of-aid-items-for-specified-aid-request" postid="[wpv-post-id]"].

Because the numbers output are formatted, which I thought might interfere with the calculation, I'm also ouputting a second view [wpv-view name='sum-cost-of-aid-items-for-specified-aid-request-raw-number' postid='[wpv-post-id]'] which returns the unformatted number (visible in the fourth column of the table on the page linked to above).

From there, I was trying to feed the number into another shortcode, produced in Settings > Custom Code that sums up the numbers:

[add-to-total2 name='item-cost2' qty="1" price="10"][/add-to-total2]

...but adding my view that outputs the number into the price attribute breaks the page:

[add-to-total2 name='item-cost2' qty="1" price="wpv-view name='sum-cost-of-aid-items-for-specified-aid-request-raw-number' postid='[wpv-post-id]']"][/add-to-total2]

Is there any documentation that you are following?
Is there a similar example that we can see?

Minesh helped me a few weeks ago to solve a similar sort of problem, and I was trying to build from what he wrote. He wrote a custom code snippet, "calculate_sum", that successfully added up the totals (qty x cost) for a loop of Aid Items. (Successful result seen at hidden link, under "Requested Items").

I duplicated the custom code as calculate_sum_for_each_aid_request and modified the shortcodes [add-to-total2] and [show-total 2] which will be seen in the Account Summary by Year view.

But, as noted, when I try to feed the number output by the "sum-cost-of-aid-items-for-specified-aid-request-raw-number" view into the price attribute of the add-to-total2 shortcode, it breaks.

What is the link to your site?

hidden link

The attached screenshot shows the correct calculated amount ($7,795.46) that we should end up with for the account/year shown at hidden link (but note that the year "2025" is incorrect).

I did not remove Minesh's access to our site from the last support request, but am happy to provide fresh login information if he no longer has it.

Thanks!

#2801073

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I'm bit confuse where exacty you need help.

Your relationship strcuture is quite challenging toe understand. Is the qty (quantity) is always be 1?

I checked the following view:
=> hidden link
Where you added the following to view's loop:

   <td>[wpv-view name="sum-cost-of-aid-items-for-specified-aid-request" postid="[wpv-post-id]"]</td>
              <td><!--Tried to input the view as the value of the price attribute for the add-to-total2 shortcode, but that broke the view -->
              tttt====[wpv-view name='sum-cost-of-aid-items-for-specified-aid-request-raw-number' postid='[wpv-post-id]']<!-- No succes in populating price attribute: [add-to-total2 name='item-cost2' qty="1" price=""][/add-to-total2] -->
              </td>

As I understand - the total calculated by view [wpv-view name="sum-cost-of-aid-items-for-specified-aid-request" postid="[wpv-post-id]"] is corrrect? am I right?

But you want to pass this total to the newly added shortcode add-to-total2? if yes:

As I undertand - you want to pass the total return by view [wpv-view name="sum-cost-of-aid-items-for-specified-aid-request" postid="[wpv-post-id]"] to the "price" attribute of your custom shortcode "add-to-total2" and what about the qty? it will be always the same as 1?

I also would like to to from what amount you want to deduct the total?

#2801180
Screenshot 2025-03-17 at 11.08.56 AM.png

Hi Minesh,

Thanks for the reply! I'll attempt to answer your questions and clarify:

"I'm bit confuse where exacty you need help."

I think the first challenge is how to successfully feed the amount to the add-to-total2 shortcode.

"Is the qty (quantity) is always be 1?"

Yes. In this case, we actually don't need a quantity. That attribute is there because I duplicated another snippet of custom code that you wrote for me a while back, named "calculate_sum". In calculate_sum, a shortcode is used in the view to loop through a series of Aid Items, multiply their quantity times cost to get the total cost, then add them up. Like this:

Aid Request (one Aid Request has many Aid Items)
---
Aid Item qty x cost
Aid Item qty x cost
Aid Item qty x cost
---
Total Cost (= sum total of each qty x cost)

But in this case, we're adding up the total cost for each Aid Request (in other words, the total cost that was calculated by "calculate_sum"), like this:

Aid Yearly Total (one Aid Yearly Total has many Aid Requests)
---
Aid Request Total Cost
Aid Request Total Cost
Aid Request Total Cost

So practically speaking, an Aid Request has one total cost (i.e., always a quantity of 1; or we could just remove all reference to quantity from the custom code "calculate_sum_for_each_aid_request").

"As I understand - the total calculated by view [wpv-view name="sum-cost-of-aid-items-for-specified-aid-request" postid="[wpv-post-id]"] is corrrect? am I right?"

Yes, that number is correct. That said, I noticed that the view is returning a formatted number, so I duplicated it and created the view "sum-cost-of-aid-items-for-specified-aid-request-raw-number" to output a raw, unformatted number (thinking that the formatting might be part of the problem).

"As I undertand - you want to pass the total return by view [wpv-view name="sum-cost-of-aid-items-for-specified-aid-request" postid="[wpv-post-id]"] to the "price" attribute of your custom shortcode "add-to-total2" and what about the qty? it will be always the same as 1?"

Yes, correct, and as described above, the quantity is always 1.

"I also would like to to from what amount you want to deduct the total?"

Yes, the total will be deducted from the Allocation for that Aid Account for that Year (see screenshot; in the view, this number is returned from another view: [wpv-view name="display-aid-allocation-amount-for-account-by-year" output='raw'] ), like so (using hidden link as our example):

2023 Allocation: for account named "4 Roundup: Resource Stewardship & Science Projects": $181,737.00
minus Aid Request Total Cost
minus Aid Request Total Cost
minus Aid Request Total Cost
---
Amount Remaining (= Aid Yearly Totals.aid-amount minus the sum of Total Cost for each Aid Request)

So I suppose that we could say the end goal is to calculate that "Amount Remaining" number, which would be returned at the bottom of the table.

I hope that clarifies all your questions. Thanks for the assist, and let me know if anything's not clear.

Best,
David

#2801501

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've adjusted the code added to "Custom code" section with code snippet "calculate_sum_for_each_aid_request" as given under:
=> hidden link

global $aid_yearly_grand_total;
global $total;

function add_total_shortcode2($atts, $content = '') {
global $total;

    $qty_mul_price = $atts['qty']*$atts['price'];
    $total[$atts['name']] += $qty_mul_price;
}
   
add_shortcode('add-to-total2', 'add_total_shortcode2');
   
function show_total_shortcode2($atts) {
global $total;
global $aid_yearly_grand_total;
  
$totalNew = $total[$atts['name']];
$aid_yearly_grand_total = $aid_yearly_grand_total +$totalNew;
$total[$atts['name']] = 0;
if(isset($atts['show_grand_total']) and $atts['show_grand_total']==1){
  return $aid_yearly_grand_total;
}

if(isset($atts['show_difference']) and $atts['show_difference']==1){
   global $wpdb;
  
  $args = array(
    'post_type' => 'aid-yearly-total',
    'posts_per_page'    =>  -1,
    'fields'=>'ids',
    'post_status'       =>  'publish',
    'meta_query' => array(
      	array(
        'key' => 'wpcf-accound-id',
        'value' => $_GET['accountid']
    	),
      	array(
        'key' => 'wpcf-aid-year-for-totals',
        'value' => $_GET['accountyear']
    	)
   )
);

$result =get_posts($args);
  if(!empty($result)){
  		$post_id = $result[0];
        $allocation_amount =   get_post_meta($post_id,'wpcf-aid-amount',true);
        $difference = $allocation_amount - $aid_yearly_grand_total;
        return $difference;
  }

}

return $totalNew;

}
   
add_shortcode('show-total2', 'show_total_shortcode2');

And to display the Grand Total and Difference I've added the following shortcode to your following view:
=> hidden link

<tr>	<td> Grand Total: [show-total2 show_grand_total='1'][/show-total2] </td>
                <td> Difference: [show-total2 show_difference='1'][/show-total2] </td>
          </tr>

Can you please confirm it works as expected.

#2801574

Minesh, you're amazing... that did it! I'll need to study your custom code to figure out how you did it, but I've tested multiple Account by Year pages, and the calculation is accurate.