Forums › Forums › Search & Filter Pro › Prices – change ranges to custom
Tagged: V3
- This topic has 24 replies, 3 voices, and was last updated 3 years, 10 months ago by Anonymous.
-
Anonymous(Private) August 14, 2020 at 10:53 am #256062
Split by @trevorsf from https://support.searchandfilter.com/forums/topic/price-range-define-max-as-more-than-250-000-instead-of-show-1-000-000/
I need to modify the price range to :
0-100 kr
101-250 kr
250 kr and upIs there any way ?
Trevor(Private) August 14, 2020 at 11:17 am #256069This will likely become a lot easier in V3 (due in a few months), but for now the only way to achieve this is to have a second price field, where the original price field is as is, but the second field has these range choices in. It is possible, with custom coding (requires good PHP skills) to autosave the second value from the first as a post is saved.
Trevor(Private) August 20, 2020 at 11:52 am #256711I think you could maybe do this yourself.
Let me guide you.
First, are you using a child theme? If not, install this plugin, activate it, and follow the simple steps to make a child theme. Activate the child theme and make sure all is well. If it isn’t, go back to themes and reactivate your parent theme:
https://wordpress.org/plugins/orbisius-child-theme-creator/
If you don’t have a plugin that makes custom fields, install this one:
https://wordpress.org/plugins/advanced-custom-fields/
Then get back to me.
Trevor(Private) August 21, 2020 at 12:27 pm #256901Code like this would have to be added to your child theme functions.php file:
function price_range_on_save( $post_id ) { $post_type = 'share'; //custom post type for events //Check if we are saving correct post type if( get_post_type( $post_id ) != $post_type) return; //Check it's not an auto save routine if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; // get the price of the post. This assumes that the price is a number and has no currency symbol or thousands separator $post_price = get_post_field('price', $post_id); // set an empty default string $post_price_text = ""; // set the price text label to match the price if ($post_price) { switch($post_price) { case ( $post_price <= 100 ): $post_price_text = "0-100 kr"; break; case ( ($post_price > 100) && ($post_price <= 250) ): $post_price_text = "101-250 kr"; break; default: $post_price_text = "250 kr and up"; } } // update the price choice update_field( 'post_price_text', $post_price_text ); } add_action('save_post', 'price_range_on_save');
Assuming what you sent me. and, the problem is always around the breakpoints. Fr example, is exactly 100 in the first or second term? I have assumed the first, otherwise you may need to change the operators a little, like this:
function price_range_on_save( $post_id ) { $post_type = 'share'; //custom post type for events //Check if we are saving correct post type if( get_post_type( $post_id ) != $post_type) return; //Check it's not an auto save routine if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; // get the price of the post. This assumes that the price is a number and has no currency symbol or thousands separator $post_price = get_post_field('price', $post_id); // set an empty default string $post_price_text = ""; // set the price text label to match the price if ($post_price) { switch($post_price) { case ( $post_price < 100 ): $post_price_text = "0-100 kr"; break; case ( ($post_price >= 100) && ($post_price < 250) ): $post_price_text = "101-250 kr"; break; default: $post_price_text = "250 kr and up"; } } // update the price choice update_field( 'post_price_text', $post_price_text ); } add_action('save_post', 'price_range_on_save');
NOTE: I have NOT tested this. To test, go to post and simply save it. Does it add the correct label?
You would need to edit current posts and simply save them to add the value.
-
AuthorPosts