MODULES
...
eCommerce
FAQ - eCommerce

How can I set up Product grouping and allow customers to switch views to another Product in the group?

20min
prerequisites in this tutorial we'll be using both categories and collections, if you're unfamiliar with either of these we'd recommend refreshing your memory categories introduction webapp collections introduction often in ecommerce, you will have products that are similar to one another e g you might have a brand of chocolate with certain different flavours available or you might have different sized packs what are product attributes? on siteglide, we provide the attributes feature which allows you to merge these multiple varieties into a single product with options users choose which attributes to select as they add the product to the cart, and the price can be adjusted accordingly follow these links to find out more about managing attributes in the admin , or outputting them as options front end what was product grouping? adobe business catalyst, on the other hand, also allowed users to "group" these similar products instead product grouping treated the product variations as separate entities which could show up in product lists independently meanwhile, the product detail page for one variation would display a list of alternate products in the group, allowing you to switch to view the different information many partners have expressed an interest in re creating this product grouping feature on their siteglide site there are several ways to achieve this use categories to group the products use datasources to build a relationship between a product and similar products in the group convert an existing product grouping setup to use single products with attributes instead in this article, we'll demonstrate how to group the products using categories how to use siteglide categories to build a product grouping ecommerce site step 1) create a category for the group first, create a category that will link the products together if you will also have some non grouped products, you may wish to create a "product groups" category as well as a parent to organise all your product grouping categories inside bellow i'll explain how you can ensure that only categories within the parent category are used here's how i've structured my categories, "chocolate" and "crisps" each represent separate groups of products, the parent category "product group" will be used to identify them as product grouping categories, allowing you to keep them separate from the other categories your products may have now let's look at how we can ensure only the categories within "product group" are used when outputting our related products first i assign an object containing all the categories on my site (this gives us access to all the categories fields, rather than just the id) {% assign categories = context exports categories items %} next loop over all of our products categories, at each iteration we'll store the "full slug" of the category (this will include the parent categories slug, which we can use to check the category is being used for product grouping) note "this" contains one of the category ids assigned to our product, we then use that id to find all of the categories details in " context exports categories items " {% for category in this properties category array %} {% assign full slug = categories\[category] full slug | split '/' %} {% endfor %} we've now created an array of all the individual "parameters" in our "full slug" field, next we'll loop over this and check none of the parameters equal "product group" if they do then we know that category is being used for product grouping and store its id add this code to the for loop above {% for category in this properties category array %} {% assign full slug = categories\[category] full slug | split '/' %} {% for p in param %} {% if p == "product group" %} {% assign group category = this %} {% endif %} {% endfor %} {% endfor %} this will check all of the parameters, make sure you replace "product group" with whichever slug your wrapping category is using if it is a product grouping category, we store the id in the variable "group category" we'll use this id later to output our related products step 2) add the group category to all products in the group now add all the related products to this category you can do this in the categories tab when editing your product in this example we'll be selecting the "crisps" category to add to our "ready salted" flavour product repeat step 2 for all the products you'd like to be linked together step 3) output the related products on the product detail page on the detail page of each product in the group, we'll next need to list the other products in the group there are now two ways we could output the related products you can choose one of the following methods include the other products in group by nesting a filtered product list layout inside the product detail layout keep all the code tidy inside the detail layout using a collection to fetch the same data we'll demonstrate how you can use method 3) b) option 3) a) output the related products within a list layout if you'd like to use a separate layout to output your products, add this include to the layout you're working in {% for category in this properties category array %} {% assign full slug = categories\[category] full slug | split '/' %} {% for p in param %} {% if p == "product group" %} {% assign group category = this %} {% endif %} {% endfor %} {% endfor %} {% include 'ecommerce/products' layout 'custom layout' per page '20' show pagination 'true' sort type 'properties name' sort order 'asc' category ids group category type 'list' %} where group category contains the id of the category containing the related products in your chosen layout, you would be able to add content regarding the related products option 3) b) output the related products within a collection now we know which products are related to one another, we can output them for this demo, i'll be outputting them within the detail layout to do this we'll use a collection (read more here collections ) which will be filtered by the parameter "category id", meaning only items within the specified category are included (please follow step 1 for instructions on how we can do this dynamically) {% include 'ecommerce/products' layout 'default' per page '20' show pagination 'true' sort type 'properties name' sort order 'asc' category ids 'category id' type 'list' collection 'true' %} if you're in a detail layout, make sure to include the type 'list' parameter also, ensure collection 'true' is added to the include, along with your category id create an object containing the included products the collection will call all the specified items into {{context exports}} , to save us writing the whole path to the item each time we output something, we'll store them in an object {% assign items = context exports\['module 14/product'] data result items %} now loop over the object, at each iteration we'll check that the id doesn't equal the id of the product being displayed on the detail page (this will stop the product on the detail page being displayed as related) we'll use {{this id}} to do this {% for item in items %} {% if item id != this id %} {% endif %} {% endfor %} step 4) optional output the related products within a dropdown many partners who have expressed interest in the product grouping feature have also requested a demonstration of how to include a dropdown menu which allows easy access to links to other products in the product group as always when using custom javascript, you may wish to adjust the simplified examples in order to add in improvements to seo and accessibility option 4) a) using a collection method if you followed step 3) a) above for this demo, i've chosen to output my related products using \<select> & \<option> , and some javascript that will redirect the page \<label for="options">related products\</label> \<select name="options" class="form control" onchange="handleoption(this)"> \<option> select alternative product \</option> {% for item in items %} {% if item id != this id %} \<option value="/{{item\['module slug']}}/{{item\['slug']}}"> {{item name}}\</option> {% endif %} {% endfor %} \</select> this will output select alternative product as the placeholder for the options, then will loop over all the items in the object we've just created, each iteration will output another \<option> where the value contains a relative link to that products detail page lastly, we'll need to add the javascript for the "handleoption" function above, this will simply redirect to the slug within the options value attribute \<script> function handleoption(elm) { window\ location = elm value; } \</script> option 4) b) including a separate product list layout if you followed step 3) b) above if you used the method 3) b) above, you'll need to move some of the code from 4) a) inside the products layout wrapper make sure your wrapper still includes the items inside the \<select> element \<label for="options">related products\</label> \<select name="options" class="form control" onchange="handleoption(this)"> \<option> select alternative product \</option> {% include 'modules/siteglide ecommerce/ecommerce/get/get products', item layout 'item' %} \</select> item the html and liquid for each option should then be included inside the item liquid file \<option value="/{{item\['module slug']}}/{{item\['slug']}}">{{item name}}\</option> add javascript as above, you'll need to include a javascript function to make the dropdown functional \<script> function handleoption(elm) { window\ location = elm value; } \</script> the solution alright, that's everything we need to link our products here is a screenshot of the solution