Filtering for star rating in magento 1

I’m trying to setup the star filter for product rating.

I have added rating_summary to facets / product attributes, which makes the values appear in the index, fine.

When I add the star filter to my widegets like this:

 search.addWidget(
                        algoliaBundle.instantsearch.widgets.starRating({
                            container: '#stars',
                            attributeName: 'rating_summary',
                            max: 100,
                            labels: {
                              andUp: '& Mehr'
                            }
                          })
        );

I produces what looks like 100 lines of star filter options. However magento rating is from 0 to 100, so where is my flaw in the logic? I’d expect the usual 5 options, 1-5 stars.
Also, I’m not getting the stars, I guess the css is not there out of the box?
regards
Alex

Hello Alex,

the starRating widget creates a line for each value in the rating_summary attribute. So it creates a 100 lines in filter. It doesn’t divide it’s values to have only 5 starts.
The best thing for you would be to divide rating_summary values to contain only values in range from 1 to 5. Then it’ll work OK.

That’s true the extension doesn’t contain the default CSS for the start widget. Can you take the CSS from the instantsearch.js repo?

Ok, this means the code at app/code/community/Algolia/Algoliasearch/Helper/Data.php:547 is not really sufficient? I’ll use an observer call to divide by 20 then.

Just realising that using an observer during the index would break the display of the rating in the results, as this does expect values frmo 0 to 100%. So I’ll try a transformData instead.

It’s sufficient - it fetches the rating_summary, however as you said below, the IS result page expects values from 1-100 range and the extension doesn’t support the star widget right now. So there is no such an attribute.
It might be a good idea to the future tho.

Let me know how it goes for you with transformData :slight_smile:

OK, this basically works fine:

 private function addRating($observer) {
        	$algoliaProductData = $observer->getProductData();
    	$rating = $algoliaObject->getData('rating_summary');
    	
    	if($rating){
    		$algoliaObject->setData('rating_summary',$rating / 20);
    	}
    	
    }

in an Observer on algolia_after_create_product_object
and

function addRatingStar(hit) {
	if(hit.rating_summary) {
		hit.rating_summary = hit.rating_summary * 20;
	}
}

in the transformData of the (infininte)result hit teimplate. A bit clumsy, but ok.
Also added CSS:

.ais-star-rating--star {
    display: inline-block;
    width: 1em;
    height: 1em;
}

.ais-star-rating--item {
    vertical-align: middle;
}

.ais-star-rating--star:before {
    content: '\2605';
    color: #fbae00;
}

.ais-star-rating--star__empty {
    display: inline-block;
    width: 1em;
    height: 1em;
}

.ais-star-rating--star__empty:before {
    content: '\2606';
    color: #fbae00;
}

.ais-star-rating--count {
    float: right;
    color: #666;
}

Only remaining thing: When selecting, e.g. 2 &up, the current fenimend widget will show four lines:
Rating 2, Rating 3, Rating 4, Rating 5
Any idea?