Product Attribute Layouts

 Last Edited: Oct 22, 2019

Prerequisites

  • - You have created Products in the Admin
  • - You have created a Product Detail View

Getting Started

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

Product Layout files in Code Editor

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

  • layouts
    • modules
      • module_14
        • product
          • name_of_my_layout
            • list
              • wrapper.liquid
              • item.liquid
            • detail
              • wrapper.liquid
              • Item.liquid
          • product_attributes
            • my_attribute_layout.liquid

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.

Steps:

  1. Create a new liquid variable and assign it to equal an empty array
  2. Loop over all Attribute Options
  3. For each iteration, find the Attribute linked to that option and add it to the array.
  4. Use the uniq filter to remove duplicates from your array.
  5. (Optional) Use an if statement to only show the container element if there are Attributes to display.
  6. Loop over your array of Attributes
  7. For each Attribute in your array, include the relevant Attribute layout, dynamically filling in the "name" parameter.

Full example:

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

<div class="row"> {% assign unique_attributes = "" | split: "," %} {% for attribute_option in this.product_attribute_options %} {% assign unique_attributes = unique_attributes | add_to_array: attribute_option[1].product_attribute.name %} {% endfor %} {% assign unique_attributes = unique_attributes | uniq %} {% if unique_attributes.size != 0 %} <div class="col-md-4"> <div class="form-group"> <div class="row"> {% for attribute in unique_attributes %} {% include 'ecommerce/product_attributes', name: attribute, layout: 'demo_site_attributes', attribute_name: attribute -%} {% endfor %} </div> </div> </div> </div>

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.

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

Output all Attribute options in the current Attribute layout.

{{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):

my_attribute_layout.liquid -- Looping over the options

{% 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:

my_attribute_layout.liquid -- Looping over the options and including JavaScript functionality

<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.


Related Docs and Reference

See the Product Layout Reference Doc for fields you can use throughout your Product Layouts.