Displaying a block within the product loop

New Member
7 0 0

I have a block with simple text and a number selection.

 

I would like to display the block text within the product for loop after the block number selection.

 

So for example, if the user has selected 3 in the block selection, it would display the block text after the 3rd product, similar to this:

 

{% if forloop.index == 3 %}

{{ block.settings.text }}

{% endif %}

 

Where 3 would be {{ block.settings.number }}

 

I’m struggling to get this to work, my understanding of liquid is limited but I think I need to run a block loop and assign the {{ block.settings.text }}, then run that through the product loop?

 

Any help is appreciated!

0 Likes
Shopify Partner
434 83 93

Hello James,

First you have to start a counter before loop 
{% assign product_counter = 0 %}
And after that in between loop use
{% if product_counter == block.settings.number  %}
   // Your block code 
{% endif %}

Thanks

Want to modify or custom changes on store hire me.
My Email: guleriathakur43@gmail.com
Skype: navrocks1
If helpful then please Like and Accept Solution.
If you are happy and want to donate small Click here (Its my paypal)
0 Likes
New Member
7 0 0

Hi Guleria, Thanks for your reply!

 

In your example, won't the product_counter always be equal to 0?

 

So the block code would only print if block.settings.number is set to 0?

 

Please excuse my ignorance.

0 Likes
Shopify Partner
434 83 93

Please share your code part, so i can implement solution in it. 

Want to modify or custom changes on store hire me.
My Email: guleriathakur43@gmail.com
Skype: navrocks1
If helpful then please Like and Accept Solution.
If you are happy and want to donate small Click here (Its my paypal)
0 Likes
New Member
7 0 0

This is what I am trying to achieve, so a user can create any number of blocks and choose where each block is placed within the product loop.

 

I am getting stuck on calling two loops.

 

{% for product in collection.products %}

     {% for block in section.blocks %}
          {% if forloop.index == block.settings.number %} <!-- product forloop, not block forloop -->
               {{ block.settings.text }}
          {% endif %}
     {% endfor %}



	<a href="{{ product.url | within: collection }}">
		<div class="item-wrapper">
  		<div class="item">
  		<div class="item-inner">
<span class="small-text-thing accent-text"><span>view</span></span> <span class="item-title small-title"><span>{{ product.title }}</span> <span class="item-title small-title"><span>{{ product.price | money_without_trailing_zeros }}</span></span> </div> </div> <div class="item-image"> {% if product.featured_image != blank %} {{ product.featured_image.src | img_url: '500x500', crop: 'bottom' | img_tag: product.featured_image.alt }} {% endif %} </div> </div> </a> {% endfor %}
0 Likes
Shopify Partner
434 83 93
{% assign product_counter = 0 %}

{% for product in collection.products %}
	
	{% assign product_counter = product_counter | plus: 1 %}
	
     {% for block in section.blocks %} 
	 
          {% if product_counter == block.settings.number %}
			{{ block.settings.text }}
		  {% endif %}
					
     {% endfor %}



	<a href="{{ product.url | within: collection }}">
		<div class="item-wrapper">
  		<div class="item">
  		<div class="item-inner">

  				<span class="small-text-thing accent-text"><span>view</span></span>
  				<span class="item-title small-title"><span>{{ product.title }}</span>
  				<span class="item-title small-title"><span>{{ product.price | money_without_trailing_zeros }}</span></span>

  		</div>
  		</div>

  		<div class="item-image">
			{% if product.featured_image != blank %}
  				{{ product.featured_image.src | img_url: '500x500', crop: 'bottom' | img_tag: product.featured_image.alt }}
			{% endif %}
		</div>
  </div>
  </a>

{% endfor %}
Want to modify or custom changes on store hire me.
My Email: guleriathakur43@gmail.com
Skype: navrocks1
If helpful then please Like and Accept Solution.
If you are happy and want to donate small Click here (Its my paypal)
0 Likes
New Member
7 0 0

Thanks for this but it doesn't seem to work, it is not displaying the block content. Are you sure {% if forloop.index == block.settings.advert_place %} doesn't need to be involved somewhere?

 

Thanks again!

0 Likes
New Member
7 0 0

Okay so after messing for a few hours I've realised your code is working fine! Sorry about that.

 

I think the problem is the block object I'm trying to call, this is what it's called:

 

block.settings.advert_text

 

But it is in a block type and I'm not sure if that makes a difference, I'm assuming it does.

 

This is the schema code:

 

"blocks": [
	{
		"type": "advert",
		"name": "Custom advert",
		"settings": [
		{
	    "id": "advert_text",
	    "type": "text",
	    "label": "Text for the advert",
	    "default": "Some text"
    }
]

How do I print that advert_text?

 

 

 

 

 

 

0 Likes
Shopify Partner
434 83 93

This is way to print
{{ block.settings.advert_text }}
If you still have issue, please share store url and admin through private message so i can check.
Thanks

Want to modify or custom changes on store hire me.
My Email: guleriathakur43@gmail.com
Skype: navrocks1
If helpful then please Like and Accept Solution.
If you are happy and want to donate small Click here (Its my paypal)
0 Likes
New Member
7 0 0

Thanks for this, I really appreciate all the help, so this is the current code, that does not output the blocks:

 

{% assign product_counter = 0 %}

{% for product in collection.products %}

     {% assign product_counter = product_counter | plus: 1 %}

     {% for block in section.blocks %}
          {% if product_counter == block.settings.advert_place %}

          <div class="item-wrapper advert {% if block.settings.advert_color %}dark{% endif %} {{ block.settings.advert_size }}">
          <div class="item">
          <div class="item-inner">

               {{ block.settings.advert_place }}
               {% if block.settings.advert_breaker %}<span class="breaker"></span>{% endif %}
               <span class="item-title small-title">{{ block.settings.advert_text }}</span>
               {% if block.settings.advert_breaker %}<span class="breaker"></span>{% endif %}

          </div>
          </div>
          </div>
          {% endif %}
     {% endfor %}

But if I change 

 

{% if product_counter == block.settings.advert_place %}

 

to

 

{% if product_counter == 9 %}

 

then it outputs the blocks after the 9th iteration, so I'm guessing its something to do with me calling block.settings.advert_place?

 

0 Likes