Support Forums

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Prices – change ranges to custom

Tagged: 

Viewing 10 posts - 1 through 10 (of 25 total)
  • Thomas
    #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 up

    Is there any way ?

    Trevor Moderator
    #256069

    This 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.

    Thomas
    #256653

    Do you know a developer who could do this for me ?

    Trevor Moderator
    #256711

    I 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.

    Thomas
    #256717
    This reply has been marked as private.
    Trevor Moderator
    #256727

    Make a new field for that Post Type for these price choices, and let me know the slug name of this new field and the name of the original price field (for example, in WooCommerce it is usually _price).

    The new field can be a simple Text field.

    Thomas
    #256816
    This reply has been marked as private.
    Trevor Moderator
    #256901

    Code 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.

    Thomas
    #257011
    This reply has been marked as private.
    Trevor Moderator
    #257015
    This reply has been marked as private.
Viewing 10 posts - 1 through 10 (of 25 total)

You must be logged in to reply to this topic.