Back to Catalog Support

octobro
octobro

Hi there,

Awesome plugin, been using it for a while now. Only thing I cant quite work out is the custom fields part. Is it possible to echo out all custom fields attached to the product with values using twig? At the moment I can only find a way to echo out that field like

{{ product.weight }}

But I would like something that would return every custom field name and value.

Tiipiik
Tiipiik

Hi octobro,

Sorry for the late, and thanks for using my plugin.

You can use

{{ product.customfields }}

to display all the custom fields, but you will need to order what you want to do with that, as you will have an array of template codes / values.

octobro
octobro

Thanks for getting back

{{ product.customfields }} lists all field ID and values. How do I list both field names and values?

Last updated

Tiipiik
Tiipiik

Did you try with a for loop ?

Last updated

octobro
octobro

Yep I have tried this

<table class="table table-striped table-hover table-responsive">
          <tbody>
          {% for customfield in product.customfields %}

           <tr><td>{{ customfield.custom_field_id }} </td> <td> {{ customfield.value }} </td></tr>

          {% endfor %}
          </tbody>
          </table>

But it echos out

1 stereo 2 black 3 110kg

How would I be able to echo out the display_name instead of the custom_field_id ?

e.g Color Black instead of 2 Black.

Tiipiik
Tiipiik

Hi octobro, Sorry for the late. In the 1.11.17 version I've added a product.customfields attribute so you can make a loop on it and it will give you the customfield name => custom field value as you need.

octobro
octobro

I can't seem to get this working. Here is my twig code

 {% for customfield in product.customfields %}
                <tr>
                  <td>
                    {{ customfield.display_name }}
                  </td>
                </tr>
                <tr>
                  <td>
                   {{ customfield.value }}
                  </td>
                </tr>
                 {% endfor %}
octobro
octobro

if I use {{ product }}

I can see in the json I get

    "colour":"RED",    
       "customfields":[  
          {  
             "id":"1",    
             "product_id":"10000",
             "store_id":null,
             "custom_field_id":"1",
             "value":"RED",
             "created_at":"2016-09-14 09:32:00",
             "updated_at":"2016-09-14 09:32:14"
          }

Now it has the display_name and value outside the customfields key. This isn't good because if I had multiple customfields added dynamically I won't know the name. For example if I added in a custom field called Size. Size would appear in the product info but it would be better if it was looped in the customfields key.

So something like

"customfields":[  
      {  
         "id":"1",
         "product_id":"10000",
         "store_id":null,
         "custom_field_id":"1",
         "display_name":"Colour"
         "value":"RED",
         "created_at":"2016-09-14 09:32:00",
         "updated_at":"2016-09-14 09:32:14"
      },

But how can I do this when display_name is in another table?

Last updated

Tiipiik
Tiipiik

Just use this :

{% for display_name, value in product.customfields %}
    {{ display_name }} : {{ value }} <br>
{% endfor %}

Last updated

1-9 of 9