product_attribute¶
The product_attribute
macro is used to render a single product attribute on a page. This macro is used on a product card.
Definition¶
Input parameters¶
options¶
object
represents an object of product attribute options
Option key | Type | Default | Required | Description |
---|---|---|---|---|
options.value | string |
"" | yes | Value of the attribute |
options.name | string |
"" | yes | Name of the attribute |
options.isCheckbox | boolean |
false | no | If set to true the attribute will be displayed as a checkbox |
Example¶
In this example we render attributes for each attribute group from a given product using our macro. To get a product with attribute groups we use the getProduct() method from ObjectApi.
{% from "@macros/product_attribute.twig" import product_attribute %}
{% set product = ObjectApi.getProduct(product_id) %}
{% for attributeGroup in product.attributeGroups %}
<div class="product-attributes__group">
<div class="product-attributes__attributes">
{% for attribute in attributeGroup.attributes %}
{{ product_attribute(attribute) }}
{% endfor %}
</div>
</div>
{% endfor %}
Macro source code¶
{% macro product_attribute(options) %}
{% from "@macros/icon.twig" import icon %}
{% if options.value != '' %}
<div class="product-attributes__attribute">
<span class="product-attributes__attribute-name">{{ options.name }}</span>
<span class="product-attributes__attribute-value">
{% if options.isCheckbox %}
{% if options.value %}
{{ icon('icon-check-circle', {
variant: 'success',
ariaLabel: 'yes'
}) }}
{% else %}
{{ icon('icon-x-circle', {
variant: 'error',
ariaLabel: 'no'
}) }}
{% endif %}
{% else %}
{{ options.value }}
{% endif %}
</span>
</div>
{% endif %}
{% endmacro %}