How-to: Use custom fields with ASA 2

Timo ASA 2 Pro Leave a Comment

With ASA 2 it is easy to access the custom fields of the currently loaded post and use the value inside a product template. Just use the following syntax and replace the * with the name of the custom field:

{{ post_custom_field_* }}

What can it be used for?

Custom fields give you a great flexibility to dynamically change the contents of a template from the outside.

Example: Show ratings dynamically

Let’s say you only want to show the product ratings in certain posts. You do not have to design two separate templates. All you need to do is to create a condition inside the template which depends on the presence and / or content of a custom field.

{% if post_custom_field_show_ratings is not empty %}
    Let's show the rating:
    {{ CustomerReviewsImgTag }} ({{ CustomerReviewsAverageRating }})
{% endif %}

To show the ratings inside the if/else condition, all you have to do is define the custom field with a value that validates to true, like “1”, on the post where you want to show product ratings:

placeholders_post_custom_field_form

Notice: To use the custom field “show_rating” from the example inside an ASA 2 template, just append the name to “post_custom_field_” which results in:
{{ post_custom_field_show_ratings }}

The result could look like this:

placeholders_post_custom_field_result1

Example: Add a personal rating

Another use case is to add a personal rating on a product review page. This could be included inside an ASA 2 template and it would only be shown if you define your personal rating in form of a custom field. For the following example I choose the custom field name “my_rating” which is meant to be a number from 1 to 10.
{% if post_custom_field_my_rating is not empty %}
<div class="my_rating_box">
  My personal rating is:
  <span class="my_rating">{{ post_custom_field_my_rating }} / 10</span>
</div>
{% endif %}

If the custom field “my_rating” would be filled with “7” …

placeholders_post_custom_field_form2

… it would display like so (in addition to the example above):

placeholders_post_custom_field_result2

These are just a few examples how to use custom fields with ASA 2. If you have other ideas how to use this feature or any kind of question, please use the comments.

Timo
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments