I’ve been trying to filter my products as per the country by tweaking the liquid code of my Shopify theme. But, I’m facing a problem, I want to access the visitor’s country in my Liquid code but unable to do that.
I can use AJAX/JQuery to fetch the location but that runs on client side and till then, liquid is already processed on the server side which makes filtering impossible.
I have successfully tried fetching the location using AJAX but don’t know how to use it in Liquid code.
$.getJSON("https://ipinfo.io", function(data) {
console.log(data.country);
});
2
Answers
You need to use the customer object and the customer_address object.
You will also need to check that a customer is logged in first:
You cannot use Liquid to determine a country. First off, in this era of VPN it is not reliable and secondly Shopify limits the recording to the IP address of the customer, assigned to an order.
The suggestion of using the customer is your best bet. If they are not logged in, too bad, you show generic stuff. If they are logged in, you then get to decide what to show them based on an address. And note that even that will likely be cause for some pain for you.