Re: .size on custom product metafield returns '0'

Solved

.size on custom product metafield returns '0'

epicnotion
Tourist
3 1 1

I'm building a component which checks the character count of a custom product metafield; if it's above 24 characters, the data is output as an accordion, otherwise the metafield content is printed in its entirety. The conditional below fails as the metafield size always returns `0`, but I can see the content printing via the `else` statement so I'm certain the path is valid:

 

 

{% if product.metafields.custom.product_note.size >= 24 %}
    <div class="product-note has--dropdown">
        <span class="item__heading item__trigger">Product information</span>
        <div class="item__content">
            {{ product.metafields.custom.product_note }}
        </div>
    </div>
{% else %}
    <div class="product-note">
        <div class="item__content">
            {{ product.metafields.custom.product_note }}
        </div>
    </div>
{% endif %}

 

 

I'm not sure it's relevant, but the metafield is a multi-line text field. If anyone could point me in the right direction as to why `.size` is failing, I'd appreciate it massively.

 

Accepted Solution (1)

epicnotion
Tourist
3 1 1

This is an accepted solution.

It was pointed out that I should just run `size` on the value, so the working component is as follows:

{% assign data_product_note = product.metafields.custom.product_note.value %}

{% if data_product_note.size >= 24 %}
    <div class="product-note has--dropdown">
        <span class="item__heading item__trigger">Product information</span>
        <div class="item__content">
            {{ data_product_note }}
        </div>
    </div>
{% else %}
    <div class="product-note">
        <div class="item__content">
            {{ data_product_note }}
        </div>
    </div>
{% endif %}

View solution in original post

Replies 3 (3)

made4Uo
Shopify Partner
3845 717 1187

Hi @epicnotion 

 

I believe the .size goes with an array. In your case you are passing a string. You can use the code below to split the string into array. 

 

NOTE: This will count the spaces

{% assign string_array = product.metafields.custom.product_note | split: "" %}

{% if string_array.size >= 24 %}
    <div class="product-note has--dropdown">
        <span class="item__heading item__trigger">Product information</span>
        <div class="item__content">
            {{ product.metafields.custom.product_note }}
        </div>
    </div>
{% else %}
    <div class="product-note">
        <div class="item__content">
            {{ product.metafields.custom.product_note }}
        </div>
    </div>
{% endif %}

 

If this fixed your issue Likes and Accept as Solution is highly appreciated. Coffee tips fuels my dedication.
Get EXPERIENCED Shopify developers at affordable rates—visit Made4Uo.com for quick quote!
Do not lost your Shopify store! Get FREE trial with ✔️ Rewind Backup: Automatic, reliable, stress-free
epicnotion
Tourist
3 1 1

@made4UoThanks for the answer — posted the solution I've used below

epicnotion
Tourist
3 1 1

This is an accepted solution.

It was pointed out that I should just run `size` on the value, so the working component is as follows:

{% assign data_product_note = product.metafields.custom.product_note.value %}

{% if data_product_note.size >= 24 %}
    <div class="product-note has--dropdown">
        <span class="item__heading item__trigger">Product information</span>
        <div class="item__content">
            {{ data_product_note }}
        </div>
    </div>
{% else %}
    <div class="product-note">
        <div class="item__content">
            {{ data_product_note }}
        </div>
    </div>
{% endif %}