Display prices of a particular variation in filter

  • 1 year, 8 months ago #30706

    Hi,
    How can I limit the price filter to index of prices related to a certain variation? Currently its indexing all the prices of the product variations, but on filtering the data with price filter, the data is mismatched as in search result , we are showing the price of the a certain variation type along with title of the product.

    wpsolr
    Keymaster
    1 year, 8 months ago #30707

    Themes only display products in search results, not product variations.
    WPSOLR is indexing products with all their variations prices, which enables filtering products (not product variations) on all variation prices.
    After getting the results from WPSOLR, the themes display the product with its price.

    • This reply was modified 1 year, 8 months ago by wpsolr.
    wpsolr
    Keymaster
    1 year, 8 months ago #30710

    To be a bit more precise, themes are showing product prices on results. Not WPSOLR.

    1 year, 8 months ago #30711

    I have a product which has variations in the range of 2000$ – 4000$, I am displaying the minimum variant price in search result, but as it falls into this range, if I select 2000-4000$ range, the item is showing up but the price of lowest variation is $2000.
    Image image.png of

    wpsolr
    Keymaster
    1 year, 8 months ago #30712

    Indeed, WPSOLR found that Product1 has at least one variation with a price in the filter range 4000-5000.
    But your theme is showing Product1 with a default price (lower variation price perhaps) not in the filter range.

    Some themes can show the price range of variations on products results, $2,000-$4,000 for your example.

    You could also customize your search results price to show the intersection of filter range and price range (in the url as parameters), $4,000 in your example (intersection of 2000-4000 and 4000-5000)

    1 year, 8 months ago #30713

    Your solution is okay, but our requirement is a bit different. We are limited to show only the lowest variation and also the price filter, should work on the basis of that lowest variation (we have a variation name for this, which is same across all the product). Any fix for this ? Can we restrict the price filter not to index the prices outside of this variation ?

    wpsolr
    Keymaster
    1 year, 8 months ago #30714

    You want to index all your products with a single variation price, the lowest?

    1 year, 8 months ago #30715

    yes, we have a variation called single, we want to index that only.

    wpsolr
    Keymaster
    1 year, 8 months ago #30716

    Here is the idea: replacing the product _price just before sending the product to the index.

    <?php
    use wpsolr\core\classes\WPSOLR_Events;
    
    add_filter( WPSOLR_Events::WPSOLR_FILTER_SOLARIUM_DOCUMENT_BEFORE_UPDATE, 'update_price_with_lowest_variation_price', 99, 5 );
    
    function update_price_with_lowest_variation_price( array $document_for_update, $solr_indexing_options, $post, $attachment_body, $search_engine_client ) {
    
    	switch ( $post->post_type ) {
    		case 'product':
    			$document_for_update['_price_f'] = /* calculate new  price here from wc_get_product( $post->ID ) and its variations */;
    			break;
    	}
    
    	return $document_for_update;
    }
    1 year, 8 months ago #30717

    As this code is fetching lowest price, Can we set it to get the price from a specific variation every time, instead of calculating lowest price?

    wpsolr
    Keymaster
    1 year, 8 months ago #30718

    calculate new price here from wc_get_product( $post->ID ) and its variations

    You can do whatever you want to calculate the price here.
    You will need to reindex your products to see the price facet updated.

    1 year, 8 months ago #30719

    Okay, thanks..

    1 year, 8 months ago #30722

    Can we use this code in functions.php or any specific file ?

    wpsolr
    Keymaster
    1 year, 8 months ago #30723

    Yes, in functions.php or a in custom plugin.

    1 year, 8 months ago #30724

    I added this code. For testing , I added a static value as 2000, when I print the array to see if the code is working, I can see the _price_str as 2000, but the facet filter still showing all price range, I am using integer type, to get checkbox with ranges option, I tried :
    $document_for_update[‘_price_str’] = 2000, I also checked with [‘_price_i’] and [‘_price_s’], still the filters arent changing.
    Also, checked with checkbox layout, prices arent reflecting.
    Image image.png of
    Image image.png of
    Image image.png of
    Any settings, I missed out in data tab, for this to work ?
    Can you pls advise.

Viewing 15 posts - 1 through 15 (of 29 total)

You must be logged in to reply to this topic.