Shopify themes, liquid, logos, and UX
Hi everyone,
I have been attempting to add a badge based on tags. I had previously done this in the Debut theme, but have begun upgrading to Dawn for Shopify 2.0 and cant seem to get the code to work correctly on Dawn theme.
This was to add a badge based on a tag given to a product, in my case "NEW" would be added to the product card for products that are tagged with NEW.
Within Dawn Theme Code editor I have tried.
In > product-card.liquid
Within the div : <div class="card.inner">
<div class="card__badge--new">
{% for tag in product.tags %}
{%- if current_tags contains 'NEW' -%}
<span> NEW! </span>
{% endif %}
{% endfor %}
</div>
With CSS within component-card.css
.card__badge--new {
background: black;
color: white;
border: 2px solid var( — color-base-background-1);
margin-top: 1em;
padding: 0.3em 0.5em;
z-index: 9;
position: relative;
font-size: 0.875em;
}
I did notice within the product-card.liquid there was no link to the component-card.css stylesheet - im not sure if this was required, but I also added along side the current stylesheet code, the component-card.css stylesheet link at the top.
{{ 'component-rating.css' | asset_url | stylesheet_tag }}
{{ 'component-card.css' | asset_url | stylesheet_tag }}
To further develop the "NEW" badge - I would of liked to of done it with a timer and publishing date, so the product would contain new for 2 - 3 weeks after publishing but disappear after that. That would reduce maintain of removing badges on now, old stock. Though I haven't gotten to that point just yet, I cant even get the current tag to appear in the browser yet!
Any help or guidance would be appreciated.
Solved! Go to the solution
This is an accepted solution.
Hey, I think i have figured it out as its now working for me on the product collection/grid now.
In product.card.liquid
On line 89 after the sold out / sale badge logic.
I wrote:
<div>
{%- if product_card_product.tags contains "NEW" -%}
<span class="card__badge--new">NEW</span>
{%- endif -%}
</div>
and for the css in component-card.css at the bottom of the file.
.card__badge--new {
position: absolute;
display: inline-block;
line-height: 1;
text-align:center;
font-size: 1.2rem;
left: 1rem;
top: .5rem;
background-color: black;
color:white;
padding: .6rem 1.3rem;
}
Remember that i also linked in css file in product-card.liquid using the below at the top of the file underneath the other stylesheet link.
{{ 'component-card.css' | asset_url | stylesheet_tag }}
Let me know if this works for you!
This is an accepted solution.
For the Dawn theme, we need the following code replacement.
<div>
{%- if card_product.tags contains "NEW" -%}
<span class="card__badge--new badge badge--top-left">NEW</span>
{% endif %}
</div>
This is an accepted solution.
Hi there,
A great question and I've been working on this v recently to find a revised solution for the most recent update to Dawn 2.0.
Here we go:
{%- elsif card_product.tags contains 'framed' -%}
<span class="card__badge--framed">{{ 'Framed' }}</span>
<div class="card__badge {{ settings.badge_position }}">
{%- if card_product.available == false -%}
<span class="badge badge--bottom-left color-{{ settings.sold_out_badge_color_scheme }}">{{ 'products.product.sold_out' | t }}</span>
{%- elsif card_product.compare_at_price > card_product.price and card_product.available -%}
<span class="badge badge--bottom-left color-{{ settings.sale_badge_color_scheme }}">{{ 'products.product.on_sale' | t }}</span>
{%- elsif card_product.tags contains 'framed' -%}
<span class="card__badge--framed">{{ 'Framed' }}</span>
{%- endif -%}
</div>
Add the following code to the bottom of the file. Be sure to make sure that the class is the same as in the liquid code you entered above (in this case it is .card__badge--framed). This code is based on what you originally wrote 🙂
.card__badge--framed {
position: absolute;
display: inline-block;
line-height: 1;
text-align:center;
font-size: 1.2rem;
left: .5rem;
top: .5rem;
background-color: #9fb18e ;
color:white;
padding: .6rem 1.3rem;
}
Hope this helps
Monty
I'm in the same boat. I can't figure out to have it on top of the image.
hey,
Did you manage to get the badge to appear based on a tag? Could you share your logic code info...? Thanks!
This is an accepted solution.
Hey, I think i have figured it out as its now working for me on the product collection/grid now.
In product.card.liquid
On line 89 after the sold out / sale badge logic.
I wrote:
<div>
{%- if product_card_product.tags contains "NEW" -%}
<span class="card__badge--new">NEW</span>
{%- endif -%}
</div>
and for the css in component-card.css at the bottom of the file.
.card__badge--new {
position: absolute;
display: inline-block;
line-height: 1;
text-align:center;
font-size: 1.2rem;
left: 1rem;
top: .5rem;
background-color: black;
color:white;
padding: .6rem 1.3rem;
}
Remember that i also linked in css file in product-card.liquid using the below at the top of the file underneath the other stylesheet link.
{{ 'component-card.css' | asset_url | stylesheet_tag }}
Let me know if this works for you!
Hi @MrGrif,
thank you so much for your code!
It works perfectly fine. But there only on the desktop. When I switch to mobile, there is no badge, but a “new” underneath the picture. You can have a look at my online shop:
Do you have a solution for this problem, so the badge looks the same on any device?
Many thanks!
Best,
Thorsten
Hi Thorsten, looks like you resolved that.
possible tutorials step by step ?
This is an accepted solution.
For the Dawn theme, we need the following code replacement.
<div>
{%- if card_product.tags contains "NEW" -%}
<span class="card__badge--new badge badge--top-left">NEW</span>
{% endif %}
</div>
Thanks a lot, this works!
see @VUMODigital solution on badges for Dawn themes using the most recent version (v.5 at time of this post released on April 19th 2022)
You may not receive an option to update your theme if you have used custom code in your theme. You will need to "Add New Theme" and start from scratch to work on the latest version of DAWN
Looks like it's broken again with v.6. I can't style the badge 😞 The badge displays with plain text only.
Hi, have not tried 6.0 but do ensure that the style sheet is linked and labelled corectly in the header of the page and the css style is within that file that you have linked . Let us know if that helps!
Hi again,
I recently updated a Dawn theme to v6.0.2 and transposed the same code I used above - all working as expected.
Kindest
Monty
Can the first solution be implemented for the Supply Theme and if so, which files? I tried but had a hard time figuring out which file needed to be changed. Thanks.
Hi, I tried entering this. and had no luck. perhaps it is because I entered the first part in card-product.liquid which is slightly different form what you had, but the closest I could find.
I'm having similar issues. The badge is working and showing up but only on the first product on the page with "More Options" but I have about 10 other products on the page where the badge doesn't show up. Can anyone help? Thank you!
Hey,
So the above worked based on a tag the the product would have .
In my case, I would tag a product with NEW (Case Sensitive) and any product with that tag would display the badge.
Does your other products have the tag you have chosen and are the tags correctly cased? i.e. new is now the same as NEW in my case.
This is an accepted solution.
Hi there,
A great question and I've been working on this v recently to find a revised solution for the most recent update to Dawn 2.0.
Here we go:
{%- elsif card_product.tags contains 'framed' -%}
<span class="card__badge--framed">{{ 'Framed' }}</span>
<div class="card__badge {{ settings.badge_position }}">
{%- if card_product.available == false -%}
<span class="badge badge--bottom-left color-{{ settings.sold_out_badge_color_scheme }}">{{ 'products.product.sold_out' | t }}</span>
{%- elsif card_product.compare_at_price > card_product.price and card_product.available -%}
<span class="badge badge--bottom-left color-{{ settings.sale_badge_color_scheme }}">{{ 'products.product.on_sale' | t }}</span>
{%- elsif card_product.tags contains 'framed' -%}
<span class="card__badge--framed">{{ 'Framed' }}</span>
{%- endif -%}
</div>
Add the following code to the bottom of the file. Be sure to make sure that the class is the same as in the liquid code you entered above (in this case it is .card__badge--framed). This code is based on what you originally wrote 🙂
.card__badge--framed {
position: absolute;
display: inline-block;
line-height: 1;
text-align:center;
font-size: 1.2rem;
left: .5rem;
top: .5rem;
background-color: #9fb18e ;
color:white;
padding: .6rem 1.3rem;
}
Hope this helps
Monty
Big thanks to @VUMODigital ! Finally got it working again after upgrading to Dawn 5.0!! The older version is not working with it some how. I added your last code to component-card.css instead of base.css and it works fine as well.
Hi John,
Delighted to hear that it worked for you!
Kindest regards
Monty - VUMO Digital
Hi !! I tried your proposed solution and had to make a few changes because it wasn't properly working for me. Now, I have a few problems I don't know if you can help me with:
1- The badge isn't at the top left like I wanted
2 - It says ''translation missing''
Here's the code I used :
<div class="card__badge">
{%- if product_card_product.available == false -%}
<span class="badge badge--bottom-left color-{{ settings.sold_out_badge_color_scheme }}">{{ 'products.product.sold_out' | t }}</span>
{%- elsif product_card_product.compare_at_price > product_card_product.price and product_card_product.available -%}
<span class="badge badge--bottom-left color-{{ settings.sale_badge_color_scheme }}">{{ 'products.product.on_sale' | t }}</span>
{%- elsif product_card_product.tags contains 'Nouveauté' -%}
<span class="badge badge--top-left color-{{ 000000 }}">{{ 'Nouveauté' | t }}</span>
{%- endif -%}
</div>
Can you advise on what the issues might be?
thank you!
Hi. How are you?
I did the same as you wrote here, now its showing in my products the badge, but do you know how I can change the back colour? I add one photo, for you see how it is showing, but I can't see very well.
If you can help me I appreciate.
Thanks
Very helpful, thanks Monty!!
Is there a way to add badges on the image on the individual product page (not on the collection page)? I've managed to add text into the product image thumbnail, but ideally I would like for a badge to sit within a the top-lefthand corner of the image so I can format this badge. I'm using theme Dawn 5.0. Thanks!
Thank you so much for this input. You saved me!!!!
Hey VUMODigital!
I've been trying to solve this issue for a while and your solution has done it! I'm ecstatic. Thank you. I'm wondering though. How do you adjust its position? Is there a way to match the spacing of the built-in badges in terms of the location on the image from the top and left edges of the product card?
Hi Attractswimwear,
Thanks for your kind reply and yes, you will be able to move the location of the badges by making adjustments to the css code in the solution above.
Let me know how you get on!
Kindest regards
Iain
Hey VUMODigital, I figured out how to move it around. I'm now trying to figure out how you can add the badge to the Product page as well. The default Sale or Sold Out Badge does this in the default theme.
Hi there,
Really pleased to hear that you were able to reposition the badge.
Regarding adding badges to the product page, I'm afraid that this solution only covers the collection grid and it would require custom code to add badges to individual images on a product page.
If that is something you need help with, please email me via the link in this footer and we can get a quote over to you for the work.
Kindest regards
Iain
How do you add the badge to the product page as well? Thanks for your help in advance!
This worked, but it just shows up as text. How do I put the text in a label? like a white colour label background? Thank you
Hi Waseem,
To style the badge, you'll need to jump into your css files and change the background-colour of the element to #ffffff.
Kindest regards
Iain
Thank you but this doesn't work. It stays black, even the font size will not change and line height does nothing, I am not updated to the latest dawn as my landing page breaks if I update
Hii code works great, but when I have a product with multiple badges (like on sale that is a standard Dawn badge and a badge bases on tags) only one will appear, the standard dawn badge.
You can check this video https://youtu.be/xGVtvlJT8Y4?si=bphndAZeAoiJTU7a to add new tag in the product cards.
I have the same problem however, we are using a custom theme. Any insights? I can't find the product.card.liquid on the theme.
I ended up giving up and installing a badges app to support my needs
Thank you so much! Will give it a try.
Hi @MrGrif ,
We came with another easy solution for adding multiple tags without editing too much code in the theme.
Hope this will help others too.
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024