Newbie Question : How to set initial value of a variable ?

Solved
Excursionist
27 0 2

Hi, sorry for such basic question.

 

To my logic work in product.liquid I need a variable that must keep his value while customer navigates on several product pages.

 

How can I make sure  to INITIAL value be defined only once for each user session ?

 

Best REgards 

William Alencar Beauty Bussiness Brasil
0 Likes
Shopify Staff
Shopify Staff
395 53 62

Hey @William_Brasili,

 

JavaScript + local storage might be the best way to go here. Can you elaborate on the problem you're trying to solve?

1 Like
Excursionist
27 0 2

Hi @scottydont thanks per contribution.

 

Passed this time the need I have is better described like this :

 

With a Pop Up at home page ( or any other way to get customer visitor input ) capture customer response about which region are him/her in Brasil ( 5 possibilities only ) .

 

With that value ... use in the code of Product and Cart page to calculate how much visitor should expend to get FREE SHIPPING .

 

Thats what I need. 

Best Regards 

William Alencar Beauty Bussiness Brasil
0 Likes

Success.

Shopify Staff
Shopify Staff
395 53 62

Thanks for the info. For that information to persist, one approach would be to store the value in local storage.

 

localStorage.setItem('region', 'some-region')

 

Then on the product/cart page you could retrieve the value using getItem:

var region = localStorage.getItem('region')
1 Like
Excursionist
27 0 2

Hey @scottydont  thanks again ...

 

This is Javascript correct ?

 

just one more question . CAn i insert javascript direct into liquid code ?  ( sorry if is a stupid question but ... I do prefer learn fast than try hide my doubts :-))))

 

 

William Alencar Beauty Bussiness Brasil
0 Likes
Shopify Staff
Shopify Staff
395 53 62

Hey @William_Brasili,

 

You can insert JavaScript into your theme, it might looks something like this (product.liquid):

 

{{ product.title }}

<script>
// Some JavaScript
</script>

 

 

1 Like
Highlighted
Excursionist
27 0 2

Thanks a lot @scottydont 

 

Best Regards

William Alencar Beauty Bussiness Brasil
0 Likes