Skip Navigation

[Résolu] Pricing View filter in parametric search

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to add a filter to my custom search View that has options with different price ranges. For example, Less than 50, 50 - 100, and 100+.

Solution: There is not an easy way to accomplish this in Toolset using a standard price filter. You would have to create a separate custom field that holds a value that represents each price range, then filter based on that custom field. To automate the process, you could use a hook like save_post or pmxi_saved_post to check the current price and update the value automatically:

function post_saved( $post_id, $post, $update ) {
    $range = 0;
    $price = get_post_meta( $post_id, 'wpcf-price', true);
    if( $price < 50000 ) {
      $range = 1;
    }else if( $price >= 50000 && $price <= 100000 ) {
      $range = 2;
    } else if( $price > 100000 ) {
      $range = 3;
    }
    update_post_meta( $post_id, 'wpcf-sort-price', $range);
}
add_action( 'pmxi_saved_post', 'post_saved',10,1);

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://codex.wordpress.org/Function_Reference/update_post_meta

This support ticket is created Il y a 5 années et 9 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Marqué : 

This topic contains 3 réponses, has 2 voix.

Last updated by Paul Il y a 5 années et 9 mois.

Assisted by: Christian Cox.

Auteur
Publications
#953529

Hi there,

I have a View setup with various filters including a price filter. At the moment it offers the user 2 input boxes, one for min price one for max price. However, the client wants to make it a lot simpler by just having 1 select box with the following values in:

up to 50k, 50k - 100k / 100k +

Is this even possible? And if it is, how would I even get started setting this up? Any help would be much appreciated!.

Thanks

Paul

#953739

Hi, there's not a simple way to do this type of price range filter with predetermined ranges. One option is to create a new custom field that has options that correspond to each range, then filter by that custom field instead of the price. You could set the custom field value manually whenever you create or update a Product post, or you could write a custom script that automates setting the value somehow using the save_post hook.

In this example, there is a custom field called "sortprice" that has three options. The names of the options are "Less than 50, 50 - 100, More than 100". The values of the options are 1, 2, and 3. The custom field value is automatically selected when the post is saved, based on the regular price of the product.

add_action( 'save_post', 'autoupdate_product_price_range', 10, 3 );
function autoupdate_product_price_range( $post_id, $post, $update ) {
  if ( $post->post_type =='product' ) {
    $range = 0;
    $price = get_post_meta( $post_id, '_regular_price', true);
    if( $price < 50 ) {
      $range = 1;
    }else if( $price <= 100 ) {
      $range = 2;
    } else if( $price > 100 ) {
      $range = 3;
    }
     
    update_post_meta( $post_id, 'wpcf-sortprice', $range);
  }
}

You can see that this example is tightly coupled, meaning the field options and the code are dependent upon each other.
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

#954358

Hi there,

Here is what I have currently:

function autoupdate_product_price_range( $post_id, $post, $update ) {
  if ( $post->post_type =='used-boat' ) {
    $range = 0;
    $price = get_post_meta( $post_id, 'wpcf-price', true);
    if( $price < 50000 ) {
      $range = 1;
    }else if( $price >= 50000 && $price <= 100000 ) {
      $range = 2;
    } else if( $price > 100000 ) {
      $range = 3;
    }
      
    update_post_meta( $post_id, 'wpcf-sort-price', $range);
  }
}
add_action( 'save_post', 'autoupdate_product_price_range');

However, the posts are being imported and created by All Import plugin. It's currently not updating the field. Is there another action that may happen just before the post is saved? like a pre save thing?

Many thanks

Paul

#954369
Screenshot(56).png

Hi there Chris,

I have this sorted now, thanks in big part to the info you gave me. I've basically added the function in the function editor in ALL IMPORT, changed the action slightly and removed the post_type check - meaning it will run each time the import runs without any problems:

function post_saved( $post_id, $post, $update ) {
    $range = 0;
    $price = get_post_meta( $post_id, 'wpcf-price', true);
    if( $price < 50000 ) {
      $range = 1;
    }else if( $price >= 50000 && $price <= 100000 ) {
      $range = 2;
    } else if( $price > 100000 ) {
      $range = 3;
    }
    update_post_meta( $post_id, 'wpcf-sort-price', $range);
}
add_action( 'pmxi_saved_post', 'post_saved',10,1);

Thanks for your help!

Paul

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.