Liquid's Ceil/Floor Filters on a Variable Not Working as Expected

31 Views Asked by At

I'm facing a weird issue that I can't get past.

Context: I have some metaobjects setup for product reviews and product metafields which reference reviews corresponding to that product. I'm trying to display the reviews on the front-end using a custom liquid section. I am able to access the data but am facing a very weird issue when working with the rating data. The rating field is set up using a rating type metafield, so its value if fetched using something like {{ review.rating.value }}.

I'm looping over all the reviews for a product and displaying the reviews' data, like so:

{% for review in product.metafields.something_reviews.reviews.value %}
    <article class="review-wrapper">
        <div class="review-rating">
            {% assign rating = review.rating.value %}
            {% assign ratingCeil = rating|ceil  %}
            {% for star in (1..ratingCeil) %}
                <span class="rating-star"></span>
            {% endfor %}
        </div>
        <p class="review-text">
            {{ review.review }}
        </p>
        <p class="author-text">
            {{ review.author  }}
        </p>
    </article>
{% endfor %}

I'm facing a problem with the following section of code:

{% assign rating = review.rating.value %}
{% assign ratingCeil = rating|ceil  %}
{% for star in (1..ratingCeil) %}
    <span class="rating-star"></span>
{% endfor %}

Let's assume that the rating for a review is 4.2 (I've kept the field user-editable so that value could be any decimal even though it doesn't make sense for a single review). In this case, the value of review.rating.value|ceil should be 5. However, for me, it is showing up as 0. If I directly output {{ 4.2|ceil }} or even {{ "4.2"|ceil }} to test, it gives the correct answer but not when I pass it into a variable like above.

I just cannot understand what is going on here. It's making me lose my mind a little.  Any help on this front will be greatly appreciated. Thank you.

PS: The same thing is happening if I use the floor filter.

1

There are 1 best solutions below

1
Alice Girard On

Possibly an issue with the field value not considered as an integer but a string.

You might try this:

{% assign rating = review.rating.value | plus:0 %}