Search Results attribute with decimal steps

Hi,

In our magento 1 store, we have an attribute that has decimal based steps, which is defined as such values in the attribute setup values:

however, when the attribute is shown as a slider in search, it only allows stepping in rounded numerics, and not by the decimal steps.

Is there a way to configure the slider to show the decimal based steps?

Selection_433

Hello @Lucas,

to do that, you’ll have to change the step parameter of slider widget.
To do so you can use algoliaHookBeforeWidgetInitialization custom method to change the configuration of the widget.

Would that work for you?

Hi @jan.petr

Thanks for that info, it is definitely what I was looking for.

Doing a bit of experimentation, I have managed to get teh stepping to work, which was simply to add a step value of 0.1

test code:

function algoliaHookBeforeWidgetInitialization(allWidgetConfiguration) {
    // modify default instantsearchOptions as you want
    var slider = allWidgetConfiguration.rangeSlider[4];
    slider.min = 1;
    slider.max = 3;
    slider.step = 0.1;
    slider.pips = true;
    return allWidgetConfiguration;
}

However, what I cannot figure out is how to adjust the tooltip to reflect the stepped value.
Any suggestions woudl be appreciated.

Selection_434

In the image, arrow designated 1: shows stepping is working.
arrow designated as 2: step tooltip does not reflec the stepped value

1 Like

@jan.petr Scratch that!

Working on code at 00:30, half asleep I was!

The obvious answer is that the result was rounded, so setting the template on a non rounding function is the obvious solution:

slider.tooltips = {format: function(rawValue) { return rawValue }}

Thank you for your help

1 Like

Great to hear your made it work Lucas! :100:
Thanks for posting the solution here.

Happy searching :mag:

@jan.petr

For any users who needed the complete solution.
Since the attribute can have values adjusted in admin, it is best to not hard code the min/max, but make it adjust accordingly.

For this I added a new template that gets loaded via layout xml (using local.xml of given theme), which then loads the given attribute values and generates the correct js.

This can easily be extended to be part of an installable module (not specific to theme), and also to handle an array of attributes. For my task, I only had the one attribute, so the code is thus for one only.

local.xml adjustment:

<algolia_search_handle>
<reference name="before_body_end">
            <block type="core/template"
                   template="algoliasearch/instantsearch/algoliaHookBeforeWidgetInitialization.phtml"
                   name="algolia-instantsearch-refine-widgets"/>
        </reference>
</algolia_search_handle>

Created template file: algoliasearch/instantsearch/algoliaHookBeforeWidgetInitialization.phtml

<?php
try {
    $attributeCode = 'a_gravity';
    $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
        ->setCodeFilter($attributeCode)->getFirstItem();
    $attributeId = $attributeInfo->getAttributeId();
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
    $attributeOptions = $attribute->getSource()->getAllOptions(false);
    $last = (int)array_pop($attributeOptions)['label'];
    $first = (int)array_shift($attributeOptions)['label'];
} catch (Exception $e) {
    Mage::logException($e);
    exit;
}
?>
<script>
    function algoliaHookBeforeWidgetInitialization(allWidgetConfiguration) {
        jQuery.each(allWidgetConfiguration.rangeSlider, function (index, value) {
            if (value['attributeName'] == '<?php echo $attributeCode; ?>') {
                var slider = allWidgetConfiguration.rangeSlider[index];
                slider.min = <?php echo $first; ?>;
                slider.max = <?php echo $last + 1; ?>;
                slider.step = 0.1;
                slider.tooltips = {
                    format: function (rawValue) {
                        return rawValue
                    }
                };
                return false;
            }
        });
        return allWidgetConfiguration;
    }
</script>
1 Like

@jan.petr

Hello, been a long time since this post.

Recently client updated from 1.6.0 to 1.14 (yep, big jump)
This solution still works, however, there seems to be a glitch.

The main (initial?) tooltips are not correctly rendered. Looks like floats are rounded up to decimals.

Example:

On 1.6, worked great:

Selection_581

but on 1.14. not so great:

Selection_580

Any suggestions on how to fix this?

1 Like