Last active
September 21, 2015 08:00
-
-
Save stoiky/5c0638c67ccc0ae56e02 to your computer and use it in GitHub Desktop.
Get all unique products of a specific catalogue and its children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
Name: Hierarchical catalogue iterator | |
Note: This will not run as a standalone, please see get-all-children-of-catalogue.liquid | |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | |
<!-- Let's get the catalog ID, we will use it to find the childrens children ... --> | |
{% capture itemID %}{{item.id}}{% endcapture -%} | |
<!-- Create the boolean variable that will tell us if the snippet should iterate again --> | |
{% assign hasChildren = false -%} | |
<!-- Add the IDs to the string --> | |
{% capture stringCatalog -%}{{item.id}},{{stringCatalog}}{% endcapture -%} | |
<!-- Get the childrens children --> | |
{module_data resource="catalogs" version="v3" fields="id,name,parentId,url" skip="0" limit="100" where="\{'parentId':{{itemID}}\}" order="id" collection="children"} | |
<!-- If we find children that have the parentId as itemID --> | |
{% if children.items != null -%} | |
<!-- Set the variable as true --> | |
{% assign hasChildren = true -%} | |
{% endif %} | |
<!-- Now just a matter of checking the flag to see if it's true, meaning it has children, this will make sure we don't hit an infinite loop --> | |
{% if hasChildren == true -%} | |
{% for item in children.items %} | |
<!-- Run again the snippet until the flag is false --> | |
{% include '/_System/_Includes/catalogue-hierarchical-iteration.liquid' %} | |
{% endfor %} | |
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
Name: Gets all children IDs of a parent catalog and makes an array out of them | |
Note: Important that catalogue-hierarchical-iteration.liquid is really an include file | |
Without it, the code will not iterate hierarchical into the catalogs childrens children | |
and so on. | |
- catalogue-hierarchical-iteration.liquid needed. | |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | |
{% assign stringCatalog = "" -%} <!-- String var that will hold all catalog IDs --> | |
<!-- Get children of primary catalogue, globals.get.CatalogueID can be used if on a catalogue page --> | |
{module_data resource="catalogs" version="v3" skip="0" limit="100" where="\{'parentId':{{primaryCatalogue}}\}" order="id" collection="children"} | |
<!-- Let's make sure the catalogue really has children --> | |
{% if children.totalItemsCount > 0 -%} | |
<!-- Loop through the items --> | |
{% for item in children.items %} | |
<!-- Include the snippet that will do all the heavy lifting --> | |
{% include '/_System/_Includes/catalogue-hierarchical-iteration.liquid' %} | |
{% endfor -%} | |
{% endif -%} | |
<!-- The array that will have our catalogue IDs | |
e.g [ "176691","176854","176855","315673","315674","315675","315676"] | |
--> | |
{% assign catalogIdsArray = stringCatalog | split: "," | sort -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
Name: Get all products from array of catalog IDs | |
Note: Run as a standalone, this snippet will not take in account duplicate products | |
if they are found in 2 ore more catalogs. | |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | |
<!-- The array name catalogIdsArray | |
e.g ["176691","176854","176855","315673","315674","315675","315676"] | |
--> | |
{% if catalogIdsArray == null -%} | |
{% assign catalogIdsArray = '176691,176854,176855,315673,315674,315675,315676' | split: "," -%} | |
{% endif -%} | |
{% assign stringProduct = "" -%} <!-- String var that will hold all our product IDs --> | |
<!-- Loop through the array --> | |
{% for catalogId in catalogIdsArray -%} | |
<!-- Get products of every catalogue --> | |
{module_data resource="catalogproducts" version="v3" fields="product" skip="0" limit="100" where="\{'catalogueId':{{catalogId}}\}" order="catalogueId" collection="catProducts"} | |
<!-- Let's make sure the catalogue really has products --> | |
{% if catProducts.items -%} | |
<!-- Loop through the items --> | |
{% for item in catProducts.items -%} | |
<!-- Add the IDs to stringProduct variable --> | |
{% capture stringProduct -%}{{item.product.id}},{{stringProduct}}{% endcapture -%} | |
{% endfor -%} | |
{% endif -%} | |
{% endfor -%} | |
<!-- The array that will have our product IDs, duplicates may appear like in the example below | |
Don't forget about the sort filter, it will help us get only unique IDs if you use the following | |
snippet: print-unique-products-from-array-of-product-ids.liquid | |
e.g ["9683555","9683556","9683557","9683558","9683559","9683560","9683561","9683562","9683563","9692748","9750323","9750323","9750324","9750324","9750325","9750325","9750326","9750326"] | |
--> | |
{% assign productIdsArray = stringProduct | split: "," | sort -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
Name: Get all unique products of a specific catalogue and its children | |
Story: We want to show all the products from a specific catalogue but we also want the | |
products of the catalogs children. It may be possible that you would have the same | |
product in two or more categories so for that, we need to be sure the products that | |
we print at the end are unique. | |
Notes: - this tutorial has been split into separate files as you could use each included | |
file as a standalone liquid snippet/helper. | |
- the location and file extension does not matter, just be sure the path is correct | |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | |
<!-- Our main catalogue, this ID can be taken from globals.get.CatalogueID if need be --> | |
{% assign primaryCatalogue = "-1" -%} | |
<!-- | |
Get all children catalogs of a specific catalogue | |
returns an array of IDs | |
--> | |
{% include "/_System/_Includes/get-all-children-of-catalogue.liquid" -%} | |
<!-- | |
Get all products from the given array of catalog IDs | |
returns an array of IDs | |
--> | |
{% include "/_System/_Includes/get-all-products-from-array-of-catalogue-ids.liquid" -%} | |
<!-- | |
Print uniques products from an array with duplicate products | |
returns html | |
--> | |
{% include "/_System/_Includes/print-unique-products-from-array-of-product-ids.liquid" -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
Name: Print unique products from an array of product IDs | |
Note: Run as a standalone, this snippet will not take in account duplicate products | |
if they are found in 2 ore more catalogs. | |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | |
<!-- The array that will have our product IDs named productIdsArray | |
e.g ["9683555","9683556","9683557","9683558","9683559","9683560","9683561","9683562","9683563","9692748","9750323","9750323","9750324","9750324","9750325","9750325","9750326","9750326"] | |
--> | |
{% if productIdsArray == null -%} | |
{% assign productIdsArray = '9683558,9683557,9750325,9750325,9750325,9750326,9750326' | split: "," | sort -%} | |
{% endif -%} | |
{% assign index = 0 -%} <!-- Assigned an integer variable to help us get unique products --> | |
{% assign counter = 0 -%} <!-- Unique products counter --> | |
<!-- Loop through the array --> | |
{% for productId in productIdsArray -%} | |
{% assign allCounter = forloop.length -%}<!-- All products counter --> | |
<!-- Convert the IDs to numbers --> | |
{% assign resourceId = productId | convert: "number" -%} | |
<!-- If the previous index was the same as the current product ID it means it was the same product, so let's skip it --> | |
{% if resourceId != index -%} | |
<!-- Just in case everything ok with our productIdsArray --> | |
{% unless resourceId == null -%} | |
<!-- Get product collection --> | |
{module_data resource="products" version="v3" resourceId="{{resourceId}}" skip="0" limit="100" order="id" collection="product"} | |
<!-- Let's make sure we have the collection --> | |
{% if product -%} | |
<p><a href="{{product.canonicalUrl}}">{{product.id}} - {{product.name}}</a></p> | |
{% assign counter = counter | plus: 1 %} <!-- Add to counter (counter++) --> | |
{% endif -%} | |
{% endunless -%} | |
{% endif -%} | |
<!-- Let's change the index to the last used product ID --> | |
{% assign index = resourceId -%} | |
{% endfor -%} | |
<p>We had <strong>{{allCounter}}</strong> products.</p> | |
<p>But only <strong>{{counter}}</strong> are unique.</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment