How can I call product image, details, and price on the section page in Shopify? - schema

{% for block in section.blocks %}
{% for image in product.images %} {{ image.src | product_img_url: 'grande' }} {% endfor %}
{{block.settings.product.title}}
{{all_products[product].price | money_without_trailing_zeros }}
{% endfor %}
{% schema %}
{
"name": "Demo",
"blocks": [
{
"type": "new-arrival",
"name": "New Arrival",
"settings": [
{
"type": "product",
"id": "product",
"label": "Product"
}
]
}
]
}
{% endschema %}
Can anyone tell me how can I call multiple product images, product details, and product prices on the section page?
Here some code that I try but it's not correct.

Thank You for guide Onkar. I update a little bit now it properly works.
{% for block in section.blocks %}
{% assign product = all_products[block.settings.product] %}
{% for image in product.images %}
<img src="{{ image.src | product_img_url: 'grande' }}" >
{{product.title}}
{{ product.price | money_without_trailing_zeros }}
{{product.description}}
{% endfor %}
{% endfor %}

when using the input type product into Shopify schema, you need to use the all_products object to get the details of the selected product, you need to get the details and assign it to a variable, something like this one.
{% assign product = all_products[block.settings.product] %}
Then you can use the other properties of the same object. so your code is looking.
{% for block in section.blocks %}
{% assign product = all_products[block.settings.product] %}
{% for image in product.images %}
<!-- list images -->
{{ image.src | product_img_url: 'grande' }}
<!-- product title -->
{{product.title}}
<!-- product price -->
{{ product.price | money_without_trailing_zeros }}
<!-- product description -->
{{product.description}}
{% endfor %}
{% endfor %}
{% schema %}
{
"name": "Demo",
"blocks": [
{
"type": "new-arrival",
"name": "New Arrival",
"settings": [
{
"type": "product",
"id": "product",
"label": "Product"
}
]
}
]
}
{% endschema %}
You can check and read more about object all_products Link

Related

Concatenate string with html in liquid Shopify

{% assign price = product.price | money %}
{% capture dot_separator %} <span class='Button__SeparatorDot'></span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}
My goal is to show the "addToCartText" text this way
Goal: ADD TO CART . 287$
Where the dot is created using css
Right now it's showing like this
Current output: ADD TO CART < span class='Button__SeparatorDot'></ span> 287$
How would I tell liquid to read the html as it is?
Try to use strip_html filter https://shopify.github.io/liquid/filters/strip_html/
like so:
{% capture dot_separator %} < span class='Button__SeparatorDot'></ span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator | strip_html }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}

Can't add image via Liquid in Shopify section

I already read the tutorial, documentary and looked for similar questions. I'm following instructions as I understood them, but I'm missing something here. I'm trying to add the possibility to add an image into a section. I can add an image in the backend, but it won't show.
The position the image with the id badges1 should be loaded:
{%- when 'badges' -%}
<div class="footer-block__details-content footer-block-badges {{ block.settings.alignment }}">
{%- if block.settings.badges1 != blank -%}
{{ block.settings.badges1 | img_url: '160x160', scale: 2 | img_tag: block.settings.badges1.alt }}
{%- else -%}
{{ 'logo' | placeholder_svg_tag: 'placeholder-svg' }}
{%- endif -%}
</div>
{%- endcase -%}
The Setting:
{
"type": "badges",
"name": "Badges",
"settings": [
{
"type": "image_picker",
"id": "badges1",
"label": "Image"
},
{
"type": "image_picker",
"id": "badges2",
"label": "Image"
}
]
}
You are trying to call block in your template code and block is missing in your schema setting.
Use this code in your template code:-
{%- when 'badges' -%}
<div class="footer-block__details-content footer-block-badges {{ block.settings.alignment }}">
{%- if block.settings.badges1 != blank -%}
<img class="if1img" src="{{block.settings.badges1 | img_url: '160x160' }}" alt="{{block.settings.heading1}}" />
{%- else -%}
{{ 'logo' | placeholder_svg_tag: 'placeholder-svg' }}
{%- endif -%}
{%- if block.settings.badges2 != blank -%}
<img class="if1img" src="{{block.settings.badges2 | img_url: '160x160' }}" alt="{{block.settings.heading2}}" />
{%- else -%}
{{ 'logo' | placeholder_svg_tag: 'placeholder-svg' }}
{%- endif -%}
</div>

Shopify Theme Development - For loop that checks if article.tags contains a sections variable

I am trying to pull through related blog posts if the article.tags == the string given within the section settings.
I have tried a few different variations but to no effect. My code looks like this (don't worry about the content within the if loop, this is all fixed into shape by the CSS):
{% for article in blogs.news.articles limit:1 %}
{% if article.tags == section.settings.brand-news-tag | strip_html %}
{% assign image_src = article.image.src | img_url: 'large' %}
<div class="brand-page-featured-news-blogs">
<div class="brand-page-featured-news-article">
<div class="brand-page-featured-news-article-image" style="background-image: url({{ image_src }})">
<div class="brand-page-featured-news-article-image-contain">
<div class="brand-page-featured-news-article-image-overlay">
</div>
</div>
</div>
<div class="brand-page-featured-news-article-contain">
<h6 class="brand-page-featured-news-article-title">{{ article.title }}</h6>
<div class="brand-page-featured-news-article-content">
<p class="brand-page-featured-news-article-published">{{ article.published_at | date: "%d %B 20%y" }}</p>
<p class="brand-page-featured-news-article-text">{{ article.content }}</p>
<div class="brand-page-featured-news-article-button">
<div class="brand-page-featured-news-article-button-text">
Read More
</div>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% schema %}
{
"name": "Featured News",
"settings": [
{
"id": "brand-news-tag",
"type": "text",
"label": "Brand News Tag",
"default": "brandnametag"
}
]
}
{% endschema %}
{% stylesheet %}
{% endstylesheet %}
{% javascript %}
{% endjavascript %}
This is the line in question:
{% if article.tags == section.settings.brand-news-tag | strip_html %}
I have tried to use a few other variations like without the '| strip_html'. I have tried to put it inside quotes like this {% if article.tags == "'" and section.settings.brand-news-tag and "'" %}. I have also tried to use 'contains' opposed to an '=='.
How can I use a variable within the if statement?
I have tried using -
{% if article.tags contains section.settings.brand-news-tag %}
I have also tried without any if statement narrowing down the blogs.news. this works as expected. Meaning it is something to do with this if statement not comparing to the blog tags. Although I have directly copied the blog tag in from the blog post to go into the variable within the section.
This also doesn't work -
{% for article in blogs.news.articles limit:1 %}
{% if section.settings.brand-news-tag != '' %}
{% assign blogfilter = section.settings.brand-news-tag | strip %}
{% endif %}
{% if article.tags contains blogfilter %}
This also doesn't work (Goodfellow is the tag copied) -
{% for article in blogs.news.articles limit:1 %}
{% if article.tags contains 'Goodfellow' %}
Here are some images of the blog side of things:
If you look at the article object in Shopify documentation then article.tags return an array. So an array cannot be compared with a string using == equals operator. You are looking for contains as explained in the logical and comparison operators documentation.
So your code will become
{% if article.tags contains section.settings.brand-news-tag %}
Moreover, you don't need strip_html as the field type is text. So Shopify will take care of it. You can use strip filter to remove any space and tabs from the start and end of string just to be extra sure.
String Filters
For your particular scenario, you don't need the limit: 1 inside for loop because you don't know that first object will contain the tag. So you need to iterate over all the objects and break out of loop if condition is satisfied. Sample code
{% for article in blogs.news.articles %}
{% if article.tags contains 'Goodfellow' %}
{{article.tags}}
{% break %}
{% endif %}
{% endfor %}

Using CartJS with Multiple Variant Dropdowns

I'm stuck getting CartJS to work with multiple variant dropdowns in Shopify. It's currently giving a 400 (Bad Request) whenever I try to add a product to cart. Which I'm guessing is because my Variant is empty. My current code is:
{% for product_option in product.options_with_values %}
<label>
{{ product_option.name }}
<select>
{% for value in product_option.values %}
<option {% if product_option.selected_value == value %}selected{% endif %}>
{{ value }}
</option>
{% endfor %}
</select>
</label>
{% endfor %}
And the option_selector code.
<script>
var selectCallback = function(variant, selector) {
timber.productPage({
money_format: "{{ shop.money_format }}",
variant: variant,
selector: selector
});
};
(function($) {
new Shopify.OptionSelectors('productSelect', {
product: {{ product | json }},
onVariantSelected: selectCallback,
enableHistoryState: true
});
// Add label if only one product option and it isn't 'Title'. Could be 'Size'.
{% if product.options.size == 1 and product.options.first != 'Title' %}
$('.selector-wrapper:eq(0)').prepend('<label for="productSelect-option-0">{{ product.options.first | escape }}</label>');
{% endif %}
// Hide selectors if we only have 1 variant and its title contains 'Default'.
{% if product.variants.size == 1 and product.variants.first.title contains 'Default' %}
$('.selector-wrapper').hide();
{% endif %}
});
</script>
Linked options might be the way to go.
It matches the values from the selectors(dropdowns) in order to get the right variant.
https://help.shopify.com/en/themes/customization/products/variants/link-product-options-in-menus

Variant Id in search.json.liquid file in shopify

I need to pass variant id in search.json file in shopify as I am using to add products through variant id in search.autocomplete.liquid file. I have checked they are using variables to call the product 'title, url & thumbnail' in loop as mentioned in the below code. Is there any way to create variable for 'variant id'.
{% layout none %}
{% assign product_cunt = search.results_count %}
{% paginate search.results by product_cunt %}
{% capture results %}
{% for item in search.results %}
{% assign product = item %}
{ "title" : {{ product.title | json }},
"url" : {{ product.url | within: product.collections.last | json }},
"thumbnail": {{ product.featured_image.src | product_img_url: 'thumb' | json }},
"variant" : (I need to add variant id variable here)
}
{% unless forloop.last %},
{% endunless %}
{% endfor %}
{% endcapture %}
{% endpaginate %}
{ "results_count": {{ search.results_count }},
"results": [{{ results }}]
}
Thanks very much for looking into this.
I have found the solution of it mentioned below If anybody has same concern.
just need to get the first available variant's ID, you should be able to use
product.selected_or_first_available_variant.id
there to get the ID of the first available variant for that product.