TL;DR: Create a snippet called product-badge.liquid that checks a product tag or metafield and outputs a styled span, then render it inside your product card or product template with {% render ‘product-badge’, product: product %}. This keeps badge logic reusable across collections, search results, and the product page without duplicating code.
Step 1: Define Your Badge Trigger
Decide what marks a product as badge-worthy. The simplest approach is a product tag like “badge-new” or “badge-sale”. Alternatively, use a metafield for more control, such as a single-line text field holding the badge label. Tags are faster to set up; metafields scale better when marketing wants to change wording without touching code.
If you want to dig deeper, check out our guide on Wearable Biosensors: The Key to Preventive Healthcare.
Step 2: Create the Snippet
In your theme’s Snippets folder, add a new file named product-badge.liquid. Start with a guard clause so nothing renders when there’s no badge. Check product.tags contains ‘badge-sale’ or read product.metafields.custom.badge. Assign the label and a modifier class to variables, then output a single span element. Keep markup minimal so it inherits your theme’s typography.
Step 3: Render It in Templates
Open your product card snippet (often card-product.liquid) and your main product template. Place the render call inside the media wrapper so the badge overlays the image. Pass the product object explicitly. If your card snippet receives a different variable name, adjust the render parameters accordingly.
Step 4: Style the Badge
Add CSS in your theme’s base stylesheet, not inline. Use absolute positioning on the badge and relative positioning on the media container. Provide modifier classes for color variants, and add a media query to shrink font size on mobile. Test contrast against light and dark product photos.
Tips
Cache-friendly: snippets with pure Liquid logic render fast, so avoid loops inside them. Accessibility: include aria-hidden only if the badge text repeats information available elsewhere. Performance: never query collections inside a card snippet. Debugging: use {{ product.tags | json }} temporarily to inspect available tags.
FAQ
Q: Can I show multiple badges on one product?
A: Yes. Loop over an array of badge rules in the snippet and output one span per match, wrapping them in a flex container.
Q: Why is my badge not appearing on collection pages?
A: Collection grids usually render a card snippet, not the product template. Add the render call to card-product.liquid and confirm the product object is passed in.
Q: Do metafield badges require an app?
A: No. Create the metafield definition in Shopify admin settings, populate values per product, and read it in Liquid with product.metafields.custom.your_key.
Leave a Reply