I'm making a sidebar for a Shopify store for a client. It will will display all of the items from a specific collection. The collection is called featured, and my client will use the CMS to specify what items are to display here. It could display 1 item, 100, or however many are in the Featured collection.
I have some code that I thought would do this, but it displays nothing. Here's the code:
{% for product in collections.featureditems.products %}
{% if product.title == '<insert a title-id>' %}
<a href="{{product.url}}" onclick="return Showroom.showProduct('{{product.handle}}');"><img src="{{ product.featured_image | product_img_url: 'medium' }}"/></a>
{% endif %}
{% endfor %}
<script type="text/javascript">
var featuredProductDisplay = "";
var productCount = {{ collections.featureditems.products.size }};
window.onload = function() {
var product=new Array(productCount)
{% for product in collections.featureditems.products %}
product[{{forloop.index0}}]='<div class="featuredItem"><a href="{{product.url}}" onclick="return Showroom.showProduct("{{product.handle}}");\"><img src="{{ product.featured_image | product_img_url: "small" }}" /></a></div>';
featuredProductDisplay = featuredProductDisplay + product[{{forloop.index0}}];
{% endfor %}
document.getElementById('featuredContainer').innerHTML = featuredProductDisplay;
}
</script>
<div class="leftSideBar">
<h2>Featured</h2>
<div id="featuredContainer">
</div>
</div>
So this code SHOULD display images of every item in the Featured catagory, and the image should be a link to the item's page. But I am stumped. Anyone have any idea what I'm doing wrong?
So in theory, it runs the loop, and builds the HTML code into the "product[{{forloop.index0}}]" variable, which is then appended to the end of featuredProductDisplay.
Once you are finished with the loop, the value of featuredProductDisplay should be placed into the div called featuredContainer.
When I run it now, it puts nothing inside the div.
So somewhere, something is not working, and I can't seem to figure out what's missing.