• Resolved arjunmurali1993

    (@arjunmurali1993)


    Hi,

    I’m very new to this and have recently installed the Algolia search plugin on my site. It does a fabulous job and I’m super happy about it. Great job guys!

    I just wanted to know how I can change the instantsearch search results page to show the hits as a grid with tiles similar to a shop page. The default behaviour, I think is designed for blog posts, but my site is primarily a woocommerce store.

    I know it has something to do with the instantsearch.php file and I’ve copied it into my child theme to try and play around. I’ve managed to replace the post content that it shows in the hits by default and replaced it with the product category. If I could change the hits to show up in a neat grid format, that would be great!

    Could you please point me in the right direction for this?

    Also, inside the algolia index, I see that the products don’t seem to have their price indexed. Is this because my products are all variable products?

    Can you also point me in the right direction so that I can include the product price as well in the search hits?

    Cheers!
    Arjun

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @arjunmurali1993
    I was just able to change the display to a grid on the InstantSearch results page. This is done via CSS. You will have to add the following CSS rules in your child theme’s style.css.

    .ais-hits {
    	display: grid;
    	grid-template-columns: auto auto auto auto;	
    	grid-gap: 10px;
    	padding: 10px;	
    }
    

    Hope that helps for the grid part. I was also able to add the “Price” field to the InstantSearch Results hit result. I will post that in the next reply.

    First off, you need to add the ‘price’ field to your index. You do that by adding the following code to your child theme’s functions.php

    function algolia_wc_post_shared_attributes( array $shared_attributes, WP_Post $post) {
        
    	//$shared_attributes['visits_count'] = vm_get_post_visit_count( $post->ID );
    	
    	$product = wc_get_product( $post );
    	// Extract prices.
    	$variations_count = 0;
    	if ( $product instanceof WC_Product_Variable ) {
    		$price = $product->get_variation_price( 'min', true );
    		$regular_price = $product->get_variation_regular_price( 'min', true );
    		$sale_price = $product->get_variation_sale_price( 'min', true );
    		$max_price = $product->get_variation_price( 'max', true );
    		$variations_count = count( $product->get_available_variations() );
    	} else {
    		$price = $max_price = $product->get_display_price();
    		$regular_price = $product->get_display_price( $product->get_regular_price() );
    		$sale_price = $product->get_display_price( $product->get_sale_price() );
    	}
    
    	$shared_attributes['product_type'] = (string) $product->get_type();
    	$shared_attributes['price'] = (float) $price;
    	$shared_attributes['regular_price'] = (float) $regular_price;
    	$shared_attributes['sale_price'] = (float) $sale_price;
    	
    
        return $shared_attributes;
    }
    add_filter( 'algolia_post_product_shared_attributes', 'algolia_wc_post_shared_attributes', 10, 2 );

    Once this is done, you will need to re-index your posts/products indices from within Algolia dashboard in your WordPress Admin. This is a very important step.

    The next step is to add the code to display this “price” field in the InstantSearch results. You will add the following code (around Line 33) of your child theme’s instantsearch.php

    			<div class="ais-hits--content">
    				<h2 itemprop="name headline"><a href="{{ data.permalink }}" title="{{ data.post_title }}" itemprop="url"><code>data._highlightResult.post_title.value</code></a></h2>
    				<div class="ais-hits--price">${{ data.price }}</div>
    			</div>

    There is one more step. This new “price” field has been added to your Products index, not the Searchable Posts Index. Therefore, you will need to switch to using your Products index in your child theme’s instantsearch.php (around Line 50) like so:

    				/* Instantiate instantsearch.js */
    				var search = instantsearch({
    					appId: algolia.application_id,
    					apiKey: algolia.search_api_key,
    					indexName: algolia.indices.posts_product.name,
    					urlSync: {
    						mapping: {'q': 's'},
    						trackedParameters: ['query']
    					},
    					searchParameters: {
    						facetingAfterDistinct: true,
    			highlightPreTag: '__ais-highlight__',
    			highlightPostTag: '__/ais-highlight__'
    					}
    				});
    Thread Starter arjunmurali1993

    (@arjunmurali1993)

    Hi @amitramani

    Thank you so much for the quick and detailed response. I’ve implemented all the above and the search results page looks really fantastic.

    I’ve also managed to edit the sidebar coding in the instantsearch.php file and added a couple of Refinement lists to filter by Product Category and size option. This is also working well.

    A couple of other filters that I would like to add in the sidebar are a range slider for Price and maybe a sort by relevance, price, etc. I tried copy pasting the example code in the algolia documentation for the above and even replaced the attributes with the ones from my algolia index but it doesn’t seem to be working though. Not sure where I’ve gone wrong.

    Could you please help me out? This is the code that I’m trying with

    
    instantsearch.widgets.sortBy({
      container: '#sort-by',
      items: [
        { label: 'Featured', value: 'instant_search' },
        { label: 'Price (asc)', value: 'instant_search_price_asc' },
        { label: 'Price (desc)', value: 'instant_search_price_desc' },
      ],
    });
    
    instantsearch.widgets.rangeSlider({
      container: '#range-slider',
      attribute: 'price',
    });
    

    And I’ve added the below in the list of facets at the top of the file:

    
    <section class="ais-facets" id="sort-by"></section>
    <section class="ais-facets" id="range-slider"></section>
    

    Could you please help me figure this out?

    Thanks and Regards,
    Arjun

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change Instantsearch page to grid to show products’ is closed to new replies.