Shopify Liquid - Related products as specific handles from a metafield - shopify

I am trying to use a Shopify metafield with a comma separated list of handles (ie: handle1,handle2) to call specific related products. These related products are displayed on individual product pages. My problem is: I cannot figure out how to get the products from the array to iterate and display.
I am using the Boundless theme, so I am trying to call/display the products in the same manner as a collection page. This may be part of my problem.
My current code calls the actual product on the page instead of the related products for some reason.
Here is my current code:
{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for product in shownwith %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{% endif %}

{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for relPro in shownwith %}
{% assign product = all_products[srelPro] %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{{ shownwith_items}}
{% endif %}

Related

Go through array elements in Liquid

I'm writing some liquid code for a shopify website in order to add products to any blog post based on tags that use product's handle. I've tried with one product and it works just fine so I was trying to iterate it through a loop but I couldn't get any information from the array I've created. This is the code I've written so far. Can you help me understand what I'm doing wrong? Thanks!
{% comment %}declare variables{% endcomment %}
{% assign related_prod_index = 0 %}
{% assign related_prod_array = "" | split: ',' %}
{% comment %}check for tags that contains products handles{% endcomment %}
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
{% assign related_prod_array = related_prod_array | push:blog_prod %}
{% assign related_prod_index = related_prod_index | plus:1 %}
{% endif %}
{% endfor%}
{% comment %}check how many product tags I found{% endcomment %}
<h1>{{ related_prod_index }} tags found</h1>
{% comment %}loop that create small preview for each product{% endcomment %}
{% if related_prod_array %}
{% for rel_pr in related_prod_array %}
<img src="{{ rel_pr.featured_image | img_url:'original' }}">
<p>{{rel_pr.title}}</p>
<p>{{rel_pr.price | money}}</p>
{% endfor %}
{% endif %}
This looks like it should be working, but if this post is true, then you can't add Product objects to an array.
The good news is you shouldn't need to. Put the display code in the same loop you use to search through the tags.
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
<img src="{{ blog_prod .featured_image | img_url:'original' }}">
<p>{{blog_prod .title}}</p>
<p>{{blog_prod .price | money}}</p>
{% endif %}
{% endfor %}

In shopify how to display wholesale top link if customer is login in desktop?

I want to display "wholesale" collection link on top menu if customer is login otherwise i do not want to display "wholesale" collection link.
I followed following link instruction but it is not working for desktop menu
https://www.envision.io/blogs/ecommerce-pulse/80312001-how-to-add-a-wholesale-area-to-your-shopify-store-without-an-app
and added bellow code in mobile menu "header.liquid" file
{% assign menu_handle = 'main-menu' %}
{% if customer %}
{% if customer.tags contains 'wholesale' %}
{% assign menu_handle = 'main-menu-wholesale' %}
{% endif %}
{% endif %}
{% for link in linklists[menu_handle].links %}
but when i try to put same code for desktop it is not working i am replacing above code with following code
{% include 'site-nav', linklist: section.settings.main_linklist %}
but it is not working after put following code for desktop nothing display on header before login and after login.
{% assign menu_handle = 'main-menu' %}
{% if customer %}
{% if customer.tags contains 'wholesale' %}
{% assign menu_handle = 'main-menu-wholesale' %}
{% endif %}
{% endif %}
{% for link in linklists[menu_handle].links %}
Thanks
I think you need to try something like this.
{% assign menu_handle = 'main-menu' %}
{% if customer %}
{% if customer.tags contains 'wholesale' %}
{% assign menu_handle = 'main-menu-wholesale' %}
{% endif %}
{% endif %}
{% include 'site-nav', linklist: menu_handle %}

Output Shopify Article by Article ID

The loop for Shopify Blog Articles is:
{% for article in blog.articles %}
{% endfor %}
I'm wondering if it's possible to output articles by different ID. Let's say 33, 65, 81. I have this custom field where I can write the ID's I want and is outputted by:
{% if article.metafields.c_f.recommended_post %}{% else %}
{% endif %}
Any ideas?
If the metafield contains a single article ID and is saved in Shopfiy as an integer, you can basically just combine the two pieces of code you already have:
{% for article in blog.articles %}
{% if article.id == article.metafields.c_f.recommended_post %}
{% comment %} Found recommended article {% endcomment %}
{% else %}
{% endif %}
{% endfor %}
If your metafield contains a single article ID but is saved in Shopify as a string you need one extra step to covert the metafield value to an integer:
{% for article in blog.articles %}
{% assign recommended_post = article.metafields.c_f.recommended_post | plus: 0 %}
{% if article.id == recommended_post %}
{% comment %} Found recommended article {% endcomment %}
{% else %}
{% endif %}
{% endfor %}
If your metafield contains multiple article IDs, separated by commas like "33, 65, 81" you can use the split filter and contains operator, and convert your article id to a string instead:
{% assign recommended_ids = article.metafields.c_f.recommended_post | split: ', ' %}
{% for article in blog.articles %}
{% assign article_id = article.id | append: '' %}
{% if recommended_ids contains article_id %}
{% comment %} Found one of many recommended articles {% endcomment %}
{% else %}
{% endif %}
{% endfor %}
For more information on saving metafields as either strings or integers see: https://docs.shopify.com/api/metafield

How to hide products in Shopify search results based on a vendor name

I'm in a situation where hiding a certain vendors products in the control panel isn't an options due to an outside POS. For a test in search.liquid, I used search.terms like below. This code works but not everyone will type thevendor exactly the same way and will see the products if they don't type thevendor.
{% for item in search.results %}
{% if search.terms == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
I tried to figure out how to write the code to hide these products in a better way. I tried product.vendor like below but when I search for those products individually they are not hidden. The code:
{% for item in search.results %}
{% if product.vendor == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
Can someone tell me what I'm missing here? It seems it doesn't know what product.vendor is but when I print out who the vendor is, it displays the vendor. I don't understand why it's not hiding the products that are associated with this vendor.
{% for item in search.results %}
{% if item.product.vendor == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
This should work.

Shopify How to output a product that doesn't have a collection

I am trying to output all the products in a menu up to a certain number than output the collection types when that number is reached. So far that works fine, however I don't know how to output a product if the user does not put the product in a collection, I would like the products not entered into a collection to be in a template that I can link to in the menu that outputs the products not in a collection.
Here is my code so far.
{% for collection in collections %}
{% if shop.products_count <= 5 %}
{% for product in collections.[collection.title].products %}
{% capture productLink %}{{ product.url }}{% endcapture %}
{{product.title}}
{% endfor %}
{% elsif shop.products_count > 5 %}
{% capture collectionLink %}{{ collection.url }}{% endcapture %}
{{collection.title}}
{% else %}
You have no Products
{% endif %}
{% endfor %}
I would probably start by making a collection that contains all products in your shop (a smart collection where Product price > 0).
Then on the page where you want to display the products that are not in any collection (except the one we just created), try something like this:
{% for product in collections.all.products %}
{% if product.collections.size <= 1 %}
This product is not in any collections other than collections.all
{% endif %}
{% endfor %}