Accentuate Custom Fields | Get blog posts by reference handle - shopify

I have a new site I'm setting up whereby blog posts contain related products and the products contain related blog posts. I can make this happen easily enough by adding product references on blog articles and blog references on products.
My main issue with this approach is that the client will need to do double entry. What I'd like to do is simply add a product reference to the blog posts and then on the product page, loop all blog posts and display any that have a reference to the current product handle.
I have a variation of this working to some extent:
{% assign related_posts = "" %}
{% for article in blogs.news.articles %}
{% if blog.metafields.blog_info.linked_products contains product.handle %}
{% capture post %}
<li><p>{{ article.title }}</p></li>
{% endcapture %}
{% assign related_posts = related_posts | append:post %}
{% endif %}
{% endfor %}
{% if related_posts.size > 0 %}
<ul> {{ related_posts }} </ul>
{% else %}
No related posts!
{% endif %}
However I have 2 problems:
1.) Shopify limit the return of blogs to 50 articles, so it isn't searching all posts
2.) If it did work on all posts, would this significantly slow down the page?
Does anybody know how to access all blog articles in one go, or have a better idea for how to implement this functionality?

Related

Pulling Products From Multiple Collections - Shopify Liquid

I am having trouble trying to pull products from more than one collections that are associated with the product on its product page. I have coded the following:
{% for product in product.collections limit: 6 %}
** Products **
{% endfor %}
However, this just pulls all the list of collections that's associated with the product rather than the products in those collections. I have then tried the following:
{% for product in collections[product.collections].products limit: 6 %}
** Products **
{% endfor %}
Which came back with an "Liquid error: Expected handle to be a String but got Array" error message.
I am not sure how I should approach this. Does anybody know where I've gone wrong?
You are going to fast. Collections is an array so you must first define which collection you need before its products.
So, for example if you want only products from first collection of current product you might try this to access the collection itself inside product.collections array:
{% for product in product.collections.first.products %}
Do something with product object.
{% endfor %}
Then, regarding what you are trying to achieve, first loop through product.collections, then loop through each collection products. Like this:
{% for collection in product.collections %}
{% for product in collection.products limit:6 %}
Do something
{% endfor %}
{% endfor %}
Please note that double loops are greedy and might affect page load time. So you might need to limit primary loop too to avoid unnecessary load if product belongs to lot of collections.

Multiple linklists on shopify categories (sidebar)

Yet another shopify question. I have a sidebar on shop/collections page
settings.sidebar_categoryblock_linklist
Which shows links to my main collecions: men/woman/kinds ect.
Yet I have another collection list based on product tags:
Winter/Autumn/Casual/Business etc
How do I get to display them on the sidebar? Meaning how can I put several linklists as categories link lists?
Thanks in advance
Julia
In a collection page, you have access to all the tags (unique) that the products have. You can use these tags to see if the belong to any collection and then display them. The code is as follows:
{% for tag in collection.all_tags %}
{% assign c_collection = collections[tag] %} // c_collection is used so the current display page's collection doesn't get disturbed
{% if c_collection.title != '' %}
{{ tag | link_to: c_collection.url,c_collection.title }} // not sure about this, but you can use proper html as well here
{% endif %}
{% endfor %}

Get a list of collections for all products in current Shopify collection

We have a primary collection of t-shirts for the product type of clothing (product type is just in case, so, nobody would bring it up).
And all of them are also in a few other collections like "linen t-shirts", "silk t-shirts" etc.
Let's say i'm on the page of t-shirts collections, and I need to display a list of all the secondary collections like linen, silk and so on. How do I do that?
What I've got so far, is I can get a list of collections for each individual product with following code. Further, I'd pull them and sort with js. But I was really hoping for an easier way to get the list.
Here's the liquid code I'm using for each product's collection list:
{% for product in collection.products %}
{% for collection in product.collections %}
<div>{{collection.title}}</div>
{% endfor %}
{% endfor %}
p.s.
variants are not an option neither
On collection page, fetch all the collections. If your current collection handle is t-shirt and your sub collection handle is linen-t-shirt. Check for all the collections which contains current collection handle, then show the details.
{% assign currentCol = collection.handle %}
{% for collection in collections %}
{% if collection.handle contains currentCol %}
{{ collection.title }}
// further code
{% endif %}
{% endfor %}

Shopify trying to add all products to homepage

I am trying to add all my products to the front page and not only a selection of 3, i'm a major newby with shopify if someone could point me in the right direction that would be great.
{% for product in collections.frontpage.products %}
{% include 'product' with product %}
{% endfor %}
You could try:
{% for product in collections.all.products %}
{% include 'product' with product %}
{% endfor %}
Although it's probably better to alter your frontpage collection to include all products. Use a smart collection with the condition Product price is greater than 0.
If you have more than 50 products you'll need to use pagination.
Here's a similar question: How do I display all of my products on my home page using Shopify and Liquid?.

How to randomize related products in Shopify

I have the standard Shopify theme Minimal. Products are assigned to Collections.
Related Items on each product just show the first 4 items it finds in the related Collections. As there are many items in each collection, a lot of the time there related items are completely the same on 100s of products.
How do I edit the code to randomize the results on Related Products?
Steph's answer is better but there is also this non-javascript (and also not truly random, but I like it anyway) solution that hacks date:
{% assign relatedCollection = collections['related-products'] %}
{% assign index = 'now' | date: '%S' %}
{% assign index = index | times: relatedCollection.products.size %}
{% assign index = index | divided_by: 60 %}
{% for product in relatedCollection.products offset: index %}
...
{% endfor %}
Take a look at this article on the Shopify wiki: Recommend related products to your customers. The section "Find a relevant Collection to recommend products" provides a jQuery script for randomizing the related products shown.
You can output all products from the relevant collection and pick a limited number of products randomly using this jQuery plugin: https://github.com/carolineschnapp/jquery-pick/blob/master/jquery.pick.js
See also: Feature multiple random products on your home page
Check the below code for showing the related products using metafields -
{% if product.metafields.related_metafield != blank %}
{% assign metafieldArr = product.metafields.related_metafield.sku | split : ',' %} {% for singleMeta in metafieldArr %} {% assign prod = all_products[singleMeta] %}
{{ prod.title | escape }}
{% endfor %}
{% endif %}
Check Example - https://stellacove.com/collections/boys