πŸ”ΉAttribute Layout - Presenting the Choice to the Customer

Product Attribute Layouts allow you to customise the way that you present users with a choice of which variation of a Product they want.

Product Attribute Layouts allow you to customise the way that you present users with a choice of which variation of a Product they want.

Prerequisites

Introduction

This Tutorial will show you how to use Attributes to let Users pick Variants on the Product's automatically-generated Detail Page.

It will cover how to:

  • Find where Product layouts are stored on Code Editor

  • Develop a my_attribute_layout.liquid file

  • Allow the User to select Attribute Options before adding to Cart

Folder Structure

In SITE MANAGER/Code Editor, the folder structure for product attributes layouts is as below:

marketplace_builder
└───views
    └───partials
        └───layouts
            └───modules
                └───module_14
                    β”œβ”€β”€β”€product
                    β”‚   collection.liquid
                    β”‚   └───custom_product_layout
                    β”‚       β”œβ”€β”€β”€detail
                    β”‚       β”‚   item.liquid
                    β”‚       β”‚   wrapper.liquid
                    β”‚       └───list
                    β”‚           item.liquid
                    β”‚           wrapper.liquid
                    └───product_attributes
                        └───custom_attributes_layout.liquid

See a more complete Cart & Checkout Folder Structure here:

🌳Cart and Checkout Folder Structure

Creating a new set of Product Layouts

To create a new set of Product layouts- create your folder at the level of "name_of_my_layout". Inside that, the folders and files should be created as shown above.

Including a Single Known Attribute on a Detail Page

If you are making a layout where you know exactly which Attribute a Product has, you can include an Attribute layout to display an Attribute with a given name: detail/item.liquid (including a single Attribute)

{% include 'ecommerce/product_attributes'
   name: attribute
   layout: 'demo_site_attributes'
   attribute_name: "Size" 
-%}

Looping Over Multiple Attributes

If your Products have multiple Attributes, or you want to write code which can dynamically display any Attribute given to the Product, you can use liquid to loop over all Attributes. We've recently updated this example to be much simpler and easier to use.

Steps:

  1. Loop over all Attribute Options

  2. Check if the Attribute is enabled

  3. If so, include the relevant Attribute layout, dynamically filling in the "name" parameter.

Full example:

detail/item.liquid -- (looping over all Attributes linked to this Product)

{% for attribute in this.product_attributes %}
  {% if attribute.properties.enabled == true %}
    {% include 'ecommerce/product_attributes'
       name: attribute.properties.name
       layout: 'demo_site_attributes' 
    -%} 
  {% endif %}
{% endfor %}



Attribute Layout Development

There is no need to create a wrapper file for Attributes, as they are already included inside an item.liquid file. Your Attribute layout can be given a name of your choice. Different Attributes for the same Product may use different Attribute layouts, e.g. "Colour" may include colour swatches.

You can output the name of the Attribute that the current Layout displays: {{this_attribute.properties.name}}

The following liquid outputs an array of the options that have been created for this Attribute: {{product_attribute_options}}

You can loop over this array with the following liquid code, (where the example has the variable "option", you could choose any variable name):

{% for option in product_attribute_options -%}
  {{option.name}} ({{this.price.currency_symbol}}{{option.price}})
{% endfor %}



To get the full benefits of Attribute functionality, including the user's choice of Attribute affecting what is added to the Cart, the data-attributes and function calls in the example should be included:

<select name="attr1" class="form-control" data-attribute-control="{{product_attribute_id}}" onchange="s_e_update_price()">
  
{% for option in product_attribute_options %}
    <option value="{{option.id}}" data-attribute-price-control="{{option.price_raw}}">
      {{option.name}} {{this.price.currency_symbol}}{{option.price}})
    </option>
  {% endfor %}
</select>

As you can see in the example, inside the loop it is possible to access the specific Attribute Option in this iteration via the "option" variable you created when setting up the loop, but you can also still access the "this" object specific to the Detail Layout that wraps around and includes the Attribute Layout. See the Product Layout Liquid Reference to see the fields available in the "this" object and those specific to Attributes and Attribute Options.

Last updated