Change the index template if the user is logged in Shopify

Solved

Change the index template if the user is logged in Shopify

max_max_max
Shopify Partner
35 0 3

Shopify, theme Expanse.

I have two index templates - index.json and index.logged.json. And I want the site user to be redirected to index.logged.json if that user is logged in. I know how to verify the user:

{% if customer %}
   ...
{%- endif -%}

But how do I display index.logged.json if that user is logged in? Where should I write this condition?

Please help.

Accepted Solution (1)

nidhipatel
Pathfinder
89 20 24

This is an accepted solution.

Hi @max_max_max 

 

Access two different index templates you just need to add below script in theme.liquid file.

 

image-20220714-083220.png

 

 

<script>
  {%- if customer -%}
  let template = '{{ template }}';
  let url = window.location.href;
  let shopurl = '{{ shop.url }}';
  if(template == 'index' && !(url.includes('?view=logged'))){
    document.location.href = shopurl + "?view=logged";
  }
  {%- endif -%}
</script>

 

 

You can also add/edit/remove section in new index template from CMS.

 

image-20220714-084047.png

 

I hope that this will help you to fulfil your requirements.

 

Happy coding!!

 
 
If our answer is helpful then please feel free to Like and Accept it as solution!
Hire our Shopify Developers for all your small & big needs:
New Store Development | Custom Theme Development | Site Speed Optimization
Email: hello@mintyourstore.com | Instagram: @mintyourstore

View solution in original post

Replies 2 (2)

nidhipatel
Pathfinder
89 20 24

This is an accepted solution.

Hi @max_max_max 

 

Access two different index templates you just need to add below script in theme.liquid file.

 

image-20220714-083220.png

 

 

<script>
  {%- if customer -%}
  let template = '{{ template }}';
  let url = window.location.href;
  let shopurl = '{{ shop.url }}';
  if(template == 'index' && !(url.includes('?view=logged'))){
    document.location.href = shopurl + "?view=logged";
  }
  {%- endif -%}
</script>

 

 

You can also add/edit/remove section in new index template from CMS.

 

image-20220714-084047.png

 

I hope that this will help you to fulfil your requirements.

 

Happy coding!!

 
 
If our answer is helpful then please feel free to Like and Accept it as solution!
Hire our Shopify Developers for all your small & big needs:
New Store Development | Custom Theme Development | Site Speed Optimization
Email: hello@mintyourstore.com | Instagram: @mintyourstore
max_max_max
Shopify Partner
35 0 3

I did it differently, but my solution is similar to yours!

 

Thanks you! 🙂