Price Comparison Use Case

  • pdbemail
    Participant
    4 years, 8 months ago #12869

    Dear WPSolr Team,

    I have successfully setup Solr and used and older version of your plugin on a local install to index blog posts, but I really want to use it to power the product search on the website I am working on. I am customising a price comparison theme for a client and I am considering whether we should buy a WPSolr license, I am concerned that the built in JQuery product filters running on MYSQL will get slow when we add many thousands of products. The theme I am customising does the following:

    – Stores master product details as a custom post type in the default WP tables
    – Stores pricing and links to the 3rd party stores in a custom table called wp_feed_list
    – Uses 4 filters in functions.php to apply price filtering to the list of posts returned by WP_Query

    Example of filter run with price range is applied

    if ( ! function_exists( 'compare_groupby_price_range' ) ) {
    	function compare_groupby_price_range( $groupby ) {
    		global $wpdb;
    		$groupby             = " {$wpdb->posts}.ID ";
    		$product_price_range = isset( $_GET['price'] ) ? (array) $_GET['price'] : '';
    
    		$price_range_list = '';
    		for ( $i = 0; $i < sizeof( $product_price_range ); $i ++ ) {
    			$price_range = $product_price_range[ $i ];
    			if ( empty( $price_range ) ) {
    				$price_range = 0;
    			}
    
    			if ( ! empty( $price_range_list ) ) {
    				$price_range_list .= " OR";
    			}
    
    			if ( stristr( $price_range, '-' ) !== false ) {
    				$temp = explode( '-', $price_range );
    
    				$price_range_list .= $wpdb->prepare( " ( MIN( feed_list.price ) >= %f AND MIN( feed_list.price ) <= %f ) ", $temp[0], $temp[1] );
    			} else {
    				$groupby .= $wpdb->prepare( " HAVING MIN(feed_list.price) >= %f ", $price_range );
    
    				return $groupby;
    			}
    		}
    
    		$groupby .= " HAVING " . $price_range_list . " ";
    
    		return $groupby;
    	}
    }

    The above is applied to the active query via:

    add_filter( 'posts_groupby', 'compare_groupby_price_range', 10, 2 );

    How would you suggest I can apply WPSolr to this use case? You must have plenty of websites running Solr for price comparison.

    Best regards

    Paul

    wpsolr
    Keymaster
    4 years, 8 months ago #12870

    Hi,

    WPSOLR can index and search your master product custom post type and all its custom fields.

    As your prices are in another custom table, you’ll just have to copy the price in each master product document just before it is sent to Elasticsearch/Solr. Then define the price field as a facet, so you can filter by price (with a slider, or a range)

    pdbemail
    Participant
    4 years, 8 months ago #12871

    As your prices are in another custom table, you’ll just have to copy the price in each master product document just before it is sent to Elasticsearch/Solr.

    That sounds positive, but how would it be achieved? Do you mean add it to wp_postmeta?

    Sorry if I am not following you correctly

    Paul

    wpsolr
    Keymaster
    4 years, 8 months ago #12902

    You could do it by yourself (SQL in wp_postmeta, WP All Import, …). Or use our hooks to fill your product documents with the price just before updating the index. For instance, https://www.wpsolr.com/guide/actions-and-filters/index-modify-a-document/

    pdbemail
    Participant
    4 years, 8 months ago #12910

    Ah that’s a good idea, do you have any old code snippets or examples anywhere of how to update a solarium document using an action hook. I understand how I can trigger a action hook, but I am unsure how to update the solarium document entity. Do you have info in this website or should I be looking at the Apache site.

    I am sorry for all the questions, but I would really like to use this plugin and the Solr tech.

    wpsolr
    Keymaster
    4 years, 8 months ago #12913

    Of course. Here is an example: https://www.wpsolr.com/forums/topic/index-a-custom-field-with-the-values-of-an-acf-repeated-field/

    You can find also other examples in the code of the plugin.

    pdbemail
    Participant
    4 years, 8 months ago #12914

    Thank you for your reply, this is making things more clear.

    $document_for_update[ 'my_field_name' . $solr_dynamic_type] = 'whatever';

    Is ‘whatever’ the variable I will have established by writing my own join function?
    Is ‘my_field_name’ a blank custom field added to the actual database once, then eligible to be written to by WPSolr? In the example you mention setting the field up in the interface screen 2.2.

    wpsolr
    Keymaster
    4 years, 8 months ago #12915

    A typical scenario:

    1) With ACF, add the custom field ‘my_product_price’ to your product post type
    2) Activate the WPSOLR ACF extension
    3) In WPSOLR screen 2.2, select the field ‘my_product_price’, with type ‘Float’
    4) Add your hook

    $solr_dynamic_type = WpSolrSchema::_SOLR_DYNAMIC_TYPE_FLOAT; // Depends on the type selected on your field on screen 2.2
    $current_price = (...) // Here query your custom table to retrieve the current post type price
     $document_for_update[ 'my_product_price' . $solr_dynamic_type] = $current_price;

    5) Select ‘my_product_price’ in screen 2.4 to create your facet (slider, range)
    6) Index all your data again

    pdbemail
    Participant
    4 years, 8 months ago #12916

    Thank you for all your replies today, I will see if I can get this to work. Do you prefer to be contacted about work by email or Fivr as surely Fivr takes 20%?

    wpsolr
    Keymaster
    4 years, 8 months ago #12917

    I updated the custom work page to include a link to a Zendesk form.

    • This reply was modified 4 years, 8 months ago by wpsolr.
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.