Skip Navigation

[Resolved] Adding a Numeric Monitary Value of a Filter

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to filter the addition of numeric values in a search

Solution:

Make sure that you put the sum of the numeric values after the view.

Sample code to get started on how to create a shortcode for that:

/**
 * Register shortcode to add field values for each item in loop to give total (after loop)
 */
add_shortcode('sum-item', function ($atts=[],$content = null) {
   
    $atts = shortcode_atts( 
        array(
            'total'     =>   null
        ), 
        $atts
    );
    static $running = 0;
    $output = "";
      
    if ( ! is_null( $content ) )
    {
        $content = do_shortcode( ltrim($content) );
        if ( is_numeric( $content ) )
        {
            $running += $content;
        }
    }
   
    if ( ! is_null( $atts['total'] ) )
    {
        $output = $running;
    }
  
    return $output;
});

How to use the shortcode:

/** An example of usage in the output of a View: */
 
[wpv-items-found]
     
     
           
[wpv-post-link]
 
          [sum-item] [types field="number-field"][/types][/sum-item]
     
     
[sum-item total=true]
 
     
    [/wpv-items-found]

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

This topic contains 5 replies, has 2 voices.

Last updated by garyK-5 2 years, 10 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2331933

Tell us what you are trying to do?
Add up a numeric field with a monetary output from the filter and search.

Is there any documentation that you are following?
None that helped.

Is there a similar example that we can see?
I've managed to massage the code to get a total value of all the data from the field "current-value".

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
function calc_func( $atts ){

// get all Posts of your type
$all_posts = get_posts(array(
'current-value' => -1,
'post_type' => 'inventory',
)
);

//if it returns some posts
if( $all_posts ){

//Start the count on 0
$single_posts_value_sum = 0;

//now get the single posts fields values
foreach( $all_posts as $single_post ){

//get each Posts post data
$single_post_data = get_post($single_post);
//get each ID
$single_post_id = $single_post_data->ID;
//get each posts field value
$single_post_value = get_post_meta($single_post_id, 'wpcf-current-value', true);

//we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
//Sum the values all posts fields
$single_posts_value_sum+= $single_post_value;

}
}
return "$".number_format($single_posts_value_sum, 0, '.', ','); //return summed value
}

add_shortcode( 'calc-total', 'calc_func' );

What is the link to your site?
Under construction

#2332933

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Toolset does not have a price format out of the box and you need cusotm development for that. You can use the money_format PHP function:

hidden link

Here you can find more information:

https://toolset.com/forums/topic/transforming-content-in-number-field-as-price/#post-606304

We do our best to give guidance on how to do customization but putting actual code together is outside of our support scope. If you are interested you can hire a developer:

https://toolset.com/contractors/

Thank you.

#2332949

Hi Cristopher...

I've already been able to add the monetary format to the field. The question is how do I add that numeric field total from a filter? As in my example code, I can add the unfiltered total, but not a filtered one.

Regards,
Gary

#2333473

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

If you explain what your code is about and what you want to achieve I will understand the issue. At the moment I do not understand the issue.

Is the problem with the return?

return "$".number_format($single_posts_value_sum, 0, '.', ','); //return summed value

It shows unformatted even when your return it that way?

Maybe if you add the login information, by setting the next reply as private and show me where to check and the result I understand the issue better.

Thank you.

#2335217

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Thank you for the information. I asked for a second opinion and now I have an answer.

The key point is that you need to use the shortcode in question after the view. If you use it before the loop the result will not be generated.

So the idea is as follows: (it will need a rethinking on your part and following this method)

A View outputs posts, and these posts have a numeric custom field. The intention is, at the end of the list of posts, to display the total value from the custom field.

For this you’ll need to register a custom shortcode (“sum-item”) which is used twice. Inside the loop it is used to add another value to the running total. After the loop it is used to output the total itself. Within the loop the custom field value (generated by a types shortcode) is wrapped by the shortcode. After the loop you use the “total” attribute to specify that the shortcode should output the total.

The first part of the code sample is the code to register the shorcode itself, while the second part if an example of how it would be used in the output of a View:

/**
 * Register shortcode to add field values for each item in loop to give total (after loop)
 */
add_shortcode('sum-item', function ($atts=[],$content = null) {
  
    $atts = shortcode_atts( 
        array(
            'total'     =>   null
        ), 
        $atts
    );
    static $running = 0;
    $output = "";
     
    if ( ! is_null( $content ) )
    {
        $content = do_shortcode( ltrim($content) );
        if ( is_numeric( $content ) )
        {
            $running += $content;
        }
    }
  
    if ( ! is_null( $atts['total'] ) )
    {
        $output = $running;
    }
 
    return $output;
});

And here is the usage of the shortcode:

/** An example of usage in the output of a View: */

[wpv-items-found]
	
	
          
[wpv-post-link]

          [sum-item] [types field="number-field"][/types][/sum-item]
	
	
[sum-item total=true]

	
	[/wpv-items-found]

This is only to give you an idea of how to go forward. We are unable to give custom code, it is just to give you a starting point.

An alternative might be to use the wpv_filter_query_post_process API filter which has the results of the View (including an array of all the post objects), and that could be used to generate the required output.

For more information:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

Thank you.

#2335357

Thanks for pointing me in the right direction on this. I hope this will be of help to others looking for a similar solution.