I’m trying to add a unique tabs dropdown section on the blockshop theme to add “key ingredients” to a skincare site. Each tab will have different information. I’m using the following code but it doesn’t seem to be working. Any help would be great!
/** Unique tabs using H6 as an anchor **/
document.addEventListener(“DOMContentLoaded”, function(event) {
$($(‘.product-single__content-text’).find(‘h6’).get().reverse()).each(function(i) {
var tabName = $(this).text();
tabContent = ‘’;
//check if h6 has text inside
if ($(this).text() != ‘’) {
//hide h6
$(this).hide();
//combine content between H6 elements
$(this).nextUntil(‘h6’).each(function() {
tabContent = tabContent + this.outerHTML;
//hide content
$(this).hide();
});
//generate tab HTML
$(‘.js-accordion’).prepend(‘
May I know from where did you get this code? Also, this code is using JQuery so make sure that your theme has JQuery files loaded either from self-hosted or from JQuery CDN.
A way that you can store unique information like that is with product metafields. Think of them as little folders attached to your products that store information that work like key value pairs. You’ll want to install a metafield app like Metafields Guru ← This is the one I use. It’s free so don’t let the pricing things trip you up, that’s just for additional functionality. After installed you can go to your product and click the More Actions dropdown in the admin and you’ll see the Metafields Guru app logo come up with an option to edit the metafields.
There may already be metafields created from some other apps you’ve installed, but dont worry about it. Just click the Create Metafield button at the top right. A new little space will popup, and you’ll see a folder icon and it’ll say “global”. That’s the name of your metafield. You can change that to whatever you want. Say you wanted to list ingredients under the product, you could rename “global” to “product_info” , and then in the text area next to the key you could type “ingredients”. So “product_info” is your folder name, “ingredients” is your key, and the text area below that is where you put your information. It’s a string object type by default so you can just type whatever in there. Then in your product-template.liquid, you would check to see if that metafield exists with an if statement, and if it does you could output a tab like that in your html. Here’s some simple code on how that would work:
Yeah link lists are good for storing smaller amounts of data, and definitely a little less complicated. I would use them for shorter lists and links, but not for larger descriptive information. In this situation I think metafields are more suitable as you can tie them directly to products with the same namespace and key across the board, but store dynamic information associated only with that specific product instead of making checks to see what product you’re on and if you’re on that product output the info from a particular linklist.
Yeah there’s a lot and it takes a lot of time. Spending time on here trying to figure out other people’s problems for them is a great way to start accumulating that knowledge. Feel free to send me a message if you need help with anything, I’m generally on here Mon - Fri checking pretty regularly.