Skip Navigation

[Resolved] Add 12% to post meta value before searching based on this custom field

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

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 8 replies, has 2 voices.

Last updated by Clifford 5 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1186557
Image 2019-01-17 at 11.51.06 PM.png

I have "rate_min" and "rate_max" post meta for "Sitter" post type.

I have a custom shortcode that (given a Sitter ID) displays the set amount plus 12% because that's what the rate is after fees.

So the actual database value of "rate_min" might be "10", but the front-end displays "11.2".

When a site visitor wants to find Sitters by their rate, such as rates between 11-15, the 11.2 one should show up. But if they search 8-11, the 11.2 one should not show up (even though the real value is 10).

The attached screenshot is what I have so far for the custom field filter.

Should I instead select the "Shortcode attribute" dropdown option instead of the "URL parameter"? I'm unclear what "Shortcode attribute" really is / how it would work.

If not, I'm okay filtering 'wpv_filter_query', but I don't want to duplicate what Views is already filtering (see screenshot). Is there an easy way to "add 12% to the value", even if via custom code, or is the only way to remove this custom field filter (from screenshot) and instead add the meta_query via custom code?

Thank you.

#1186575

Hello,

Q1) Should I instead select the "Shortcode attribute" dropdown option instead of the "URL parameter"?
No, in your case, it should not use "Shortcode attribute", that is for pass parameter using shortcode attribute, see our document:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes
Controlling the filter with shortcode attributes

Q2) I'm okay filtering 'wpv_filter_query', but I don't want to duplicate what Views is already filtering (see screenshot). Is there an easy way to "add 12% to the value",

Yes, it needs custom codes using filter hook "wpv_filter_query", for example:

add_filter('wpv_filter_query', function($args, $settings, $view_id){
	if($view_id == 123 && isset($args['meta_query'])){
		foreach($args['meta_query'] as $k => $v){
			if(isset($v['key']) && $v['key'] == 'wpcf-rate'){
				$arr = explode(',', $v['value']);
				$arr[0] = $arr[0] * ( 1+ 0.12);
				$str = implode(',', $arr);
				$args['meta_query'][$k]['value'] = $str;
			}
		}
	}
	return $args;
}, 10, 3);

Please replace 123 with your view's ID

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#1186986

Thank you very much.

A)
This is the actual code in the "Search and Pagination" section of the View:

<div class="form-group">
	<label>[wpml-string context="wpv-views"]Sitter Min Hour Rate[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-sitter-hour-rate" type="textfield" url_param="wpv-wpcf-sitter-hour-rate_min"]
</div>
<div class="form-group">
	<label>[wpml-string context="wpv-views"]Sitter Max Hour Rate[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-sitter-hour-rate" type="textfield" url_param="wpv-wpcf-sitter-hour-rate_max"]
</div>

However,

$query['meta_query']

is never set even when I search with these fields.

B)
I really appreciate your snippet, but it looks like it doesn't take into account the min and max fields and their BETWEEN comparison (see initial screenshot).

Is there something wrong that $query['meta_query'] is never in use? Or should I just manually check $_REQUEST and add the meta query logic (including BETWEEN) myself, in which case, should I remove this field's rules from the "Custom field filter" settings?

#1187919

Above codes works fine in my localhost, since in your screenshot:hidden link
It is using field slug "wpcf-rate", so I have only tested with custom numeric field "rate" in my localhost.

In your new post the field slug is "wpcf-sitter-hour-rate":
https://toolset.com/forums/topic/add-12-to-post-meta-value-before-searching-based-on-this-custom-field/#post-1186986
you can replace "wpcf-rate" with "wpcf-sitter-hour-rate".

For the new question:

Is there something wrong that $query['meta_query'] is never in use? 

$query['meta_query'] will be in use only submit the form and pass URL parameter "wpv-wpcf-sitter-hour-rate_min" to view.

I suggest you debug your codes manually, line by line.

#1187953
Image 2019-01-21 at 12.06.41 AM.png

Ah, the issue was using the wpv_filter_query hook's priority 10... meta_query wasn't yet set... priority 50 worked.

However, it's not working as expected. From the attached screenshot... we're wanting to make sure a front-end search for a rate between "10" and "10" would match a sitter profile with a raw meta value of "8.93" -- so then why are there zero results? Using "type=NUMERIC" and "compare=BETWEEN" should make things match a raw value of 8.93

Thanks for any pointers.

#1187960

Since it is a custom PHP codes problem, if you need more assistance for it, please provide a test site with the same problem, also point out the problem page URL and view URL, I need to test it in a live website

#1188397

Figured it out... Views was set to "numeric" but using Query Monitor I realized it was CAST() as hidden link">SIGNED -- which is integers... and I was trying to compare decimals, so I changed to "decimal" in Views and it works now.

Thanks for your help, especially the starting code snippet.

#1188400

Full code snippet for reference:

/**
	 * Toolset View #45 (Sitter Search): Filter the results by the hourly rate, adjusting from Sitter's raw value
	 * to the actual rate Parents will pay (adding 12%).
	 *
	 * @param $query
	 * @param $view_settings
	 * @param $id
	 *
	 * @return mixed
	 */
	public function sitter_search_adjust_rate_for_booking_fees( $query, $view_settings, $id ) {
		if (
			45 !== absint( $id )
			|| ! isset( $query['meta_query'] )
		) {
			return $query;
		}

		foreach ( $query['meta_query'] as $k => $v ) {
			if (
				isset( $v['key'] )
				&& $v['key'] == 'wpcf-sitter-hour-rate'
			) {
				$new = [];

				$arr = explode( ',', $v['value'] );

				foreach ( $arr as $a_key => $a_value ) {
					$becomes = $a_value / 1.12;

					if (
						// BETWEEN Min to Max range and we're at Max
						(
							0 < count( $new )
							&& 1 === $a_key
						)
						// Max without a Min
						|| (
							0 === count( $new )
							&& ! empty( $query['meta_query'][$k]['compare'] )
							&& '<=' === $query['meta_query'][$k]['compare']
						)
					) {
						$becomes = $becomes + 0.01;
					} else {
						$becomes = $becomes - 0.01;
					}
					$new[] = $becomes;
				}

				$str = implode( ',', $new );

				$query['meta_query'][$k]['value'] = $str;
			}
		}

		return $query;
	}
#1188401

My issue is resolved now. Thank you!