How can I effectively hide blocks based on customer tags?

Hi All,

I am currently in the process of creating a “content gateway”, i.e. a page that my customers can come to and see dynamically unlocked content based on their tags. What I have currently is a modified image gallery section which will act as a content card that links out to another page. The idea is that I should be able to assign a tag to each card in the customisation editor - I’ve created a custom text-field within the block for this, and if the customer has the same tag they will see the block, if not, the block will be hidden.

I’ve written the following:

    {% unless customer.tags contains {{ block.settings.customer-tag }} %}
    .section-{{ section.id }} .gallery__item-1 {
      display: none !important;
    }
    {% endunless %}

Unfortunately, this doesn’t work as intended for a couple of reasons:

  • The {{ block.settings.customer-tag }} part does not work unless in quotations, and when in quotations it reads only as plain text and not liquid. Every time I save I just get a syntax error. Is there a way around this?
  • The “.gallery__item-1” part is only targeting the first block, not any block with the associated tag. It works well enough, but I will have to manually add each block here whenever I make changes as opposed to having it dynamically linked to whatever tag I assign in the editor.

Is it going to be more trouble than its worth to get this up as compared to going into the .liquid file each time I want to assign a new block to a set of tags?

Thoughts welcome.

Thanks,

Oren

hi @RayvoxLtd ,

The code’s you incorrect the syntax, so you can follow the code bellow and make sure the name class of target element you want hide is exactly.

{% for tag in customer.tags %}
   {% unless tag contains block.settings.customer-tag %}
    .section-{{ section.id }} .gallery__item-1 {
      display: none !important;
    }
   {% endunless %}   
{% endfor %}

@DitalTek thanks for this. I can’t quite seem to get it to behave correctly though. It seems like it only performs the action once, regardless of the login state of the customer. I.e. I write the reference tag in the text field in the editor and the action performs immediately, but it doesn’t seem to compare that field to the customer and so the block does not get affected when the customer is logged in/out.

Just to update incase this is useful to anyone else. I’ve played around some more with the code and this now works flawlessly for me:

{% unless customer.tags contains block.settings.customer-tag or block.settings.customer-tag == blank %}
    .section-{{ section.id }} .gallery__item-{{ forloop.index }} {
  display: none !important;
    }
   {% endunless %}

and I have this field in the block schema:

{
          "type": "text",
          "id": "customer-tag",
          "info": "Leave blank for public access",        
          "label": "Customer Tag"
        },

So basically, if the customer has the tag that assigned to the block in the editor, they will see it. If they do not, it will be hidden. Additionally, if there’s no assigned tag (i.e. for public access), then the block will always display.

I added the {{forloop.index}} part to the gallery in order to assign display to the correct block, no matter if I change the position in the list. This was so much better than manually assigning the block id each time.

There might be better solutions, but this works well for me at the moment, so quite happy.

I will look into adding options for more than one tag per block in the future. I’m sure that can be easily done with just adding more text fields. Although, having a way to add them all to one text box with comma or line separation might be nice.

Hey!

I’m wondering if that could be a solution for me. I want to add a lock so customers not logged in with wholesale tag can’t see prices for some items.

That said I also realized I have other price descriptors in the descriptions of the items I’d also need to hide if not logged in as wholesale.

To further explain - we have loose diamonds. We want all to see them but only wholesalers to see main price, and the “price per ct” which is in the item card description.

What do you think?

Hi,
I don’t familiar your business, so with your explain above i’m understanding you want hide price at somewhere with some type customer.
If that, you can assign to type customer that a tag, then at place showing the price, you can check if the customer has tag assigned, then you hide price.

Is there a way to set this up so that it stops customers with a tag from seeing a block? . And if so, where are you putting the code? My wholesale customers (tagged “wholesale”) can see my bundle blocks and it’s mucking things up for their pricing, but I don’t want to block anonymous users from seeing the bundles. Thank you!