Skip Navigation

[Resolved] Result of calculation in search field

This support ticket is created 2 years, 1 month 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 7 replies, has 2 voices.

Last updated by markP-19 2 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2533061

Hi

I have a custom code that calculates and outputs the result in a view loop.
It calculates m2 entered manually in the custom search by the user and multiplies it with a predefined price value for the post to get the results in the view loop.

The listet posts in the view loop has a predefined m2 taxonomy value. Instead of the user choosing form a dropdown, which m2 they would search/filter on, i would like the calculator to parse/paste the result to the m2 taxonomy (m2-pvc-dug) since it already calculates this and there is no need for the user to choose the m2 manually.

How is this possible?

The code is:

add_filter( 'wpv_filter_query', 'func_remove_unwanted_arguments',10,3 );
function func_remove_unwanted_arguments( $query_args ,$view_settings, $view_id ) {
if ($view_id == 224 ) {
$all_args = $query_args['meta_query'];
$unwanted_fields = array('wpcf-folieprint-hojde','wpcf-folieprint-bredde');
foreach($all_args as $k=>$v):
if(in_array($v['key'],$unwanted_fields)){
unset($query_args['meta_query'][$k]);
}
endforeach;

}
return $query_args;
}

// Calculate shortcode
add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
if((isset($_REQUEST['wpv-wpcf-folieprint-hojde']) and $_REQUEST['wpv-wpcf-folieprint-hojde']!='')
and
(isset($_REQUEST['wpv-wpcf-folieprint-bredde']) and $_REQUEST['wpv-wpcf-folieprint-bredde']!='')
) {
global $post;
$hojde_val = str_replace(",",".",$_REQUEST['wpv-wpcf-folieprint-hojde']);
$bredde_val = str_replace(",",".",$_REQUEST['wpv-wpcf-folieprint-bredde']);
$pris = get_post_meta($post->ID,'wpcf-pris-folieprint',true);
$result = $hojde_val * $bredde_val * $pris;

return str_replace(".",",",$result);

}
return 0;
}

#2533483

Hello,

Please elaborate the questions with more details:
How and where do you setup the "m2"? is it a custom field or custom post type?

Herer is a test site, you can reproduce the same problem in it, also provide for what you are going to achieve, take some screenshots for it.
Test site login URL:
hidden link

#2533535

The m2 is a taxonomy for a custom post, and each post contains a value between 1-50 in this taxonomy.

So as an example, the user manually enters the height (wpv-wpcf-folieprint-hojde) and width (wpv-wpcf-folieprint-bredde) in the search fields which gets multiplied with the custom price field for the custom post. The results are calculated for each posts in the view loop. The calculation could be as following: 1,2 * 1,5 * 'price' = 754,2.

Right now, all the posts are shown in the view loop results.
Since the m2 is already calculated, when the height and width gets multiplied, i would like the search to automatically filter the posts. As an example, if the m2 calculation equals 1,8 the view loop search results should only produce results from custom posts having a taxonomy value between 0-9,99. If the m2 calculation result is 12,3 the view loop search results should only show results from custom posts containing taxonomy value between 10-49,99.

I have a test site, where i can provide you direct link and credentials.

#2535227

Please provide your test website credentials in below private message box, also point out:
- The view URL
- Where I can edit your custom PHP codes
- Where I can test the result in frontend

#2535313

Your website isn't in English, it is hard to find the related settings, which is is the price field?

Are you going to do these:
When user submit the form, calculate value like this:
value = Height * Width * Price

Use above value to filter view result by taxonomy "m2 folieprint" slug values:
- 1-199
- 10-19,99
...

If it is, there isn't such kind of built-in feature within Toolset plugins, Toolset Blocks plugin is WordPress built-in class WP_Query to query posts, see WP document:
https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
There isn't such a feature to filter the results using between term slugs.

#2535345

I've changed the WP language to english now.

Height * Width * Price is the calculation for the view loop price which outputs the results in column "Beregnet pris".

Since the m2 is already calculated with Height * Width, it should parse the value to the taxonomy search field 'wpv-m2-folieprint" when pressing search.

I've now updated two products to both having the value 4, instead of 4-4,99, so it doesn't use between term slugs.
So if you enter 4 in height and 1,2 in width, and hit search, this calculation result is 4,8, which means that the taxonomy search field should be updated to be 4 and therefor filter the results to only showing post results with the m2 taxonomy value of '4'.

#2535941

Thanks for the details, for the issue:

if you enter 4 in height and 1,2 in width, and hit search, this calculation result is 4,8, which means that the taxonomy search field should be updated to be 4 and therefor filter the results to only showing post results with the m2 taxonomy value of '4'.

It is possible to change the URL parameter and select taxonomy search field with custom PHP codes, I have done below modifications in your website:
Edit the custom codes snippet "toolset-custom-code", add lines 123 ~ 138:

add_filter( 'wpv_filter_query', 'calculation_m2_param',1,3 );
function calculation_m2_param( $query_args ,$view_settings, $view_id ) {
 if ($view_id == 224 ) {
         if((isset($_REQUEST['wpv-wpcf-folieprint-hojde']) and $_REQUEST['wpv-wpcf-folieprint-hojde']!='')
            and
           (isset($_REQUEST['wpv-wpcf-folieprint-bredde']) and $_REQUEST['wpv-wpcf-folieprint-bredde']!='')
           ) {
           		$hojde_val = str_replace(",",".",$_REQUEST['wpv-wpcf-folieprint-hojde']);
           		$bredde_val = str_replace(",",".",$_REQUEST['wpv-wpcf-folieprint-bredde']);
           		$result = $hojde_val * $bredde_val;
			$arr = explode('.', $result);
           		$_GET['wpv-m2-folieprint'] = $arr[0];
        }
  }
  return $query_args;
}

Test it in frontend:
hidden link

I can see option "4" is selected in search field "m2", it is only a demo for your reference, you will need to customize it as what you want.

#2536469

My issue is resolved now. Thank you!