Ad

How Can I Get The Cart's Shipping Address In The Cart.liquid File?

- 1 answer

I'm trying to change the tax and shipping method prompt based on the user's current shipping address. Obviously, they are not yet at checkout so have not had the opportunity to input their shipping address. Is there anything else I can check at this stage to find out which country they are from?

{% if shipping_address.country_code = 'CA' %}
    <p class="cart-message meta">INCLUDING TAX AND SHIPPING (U.S. AND CANADA)</p>
{% else %}
    <p class="cart-message meta">{{ 'cart.general.tax_and_shipping' | t }} (INTERNATIONAL)</p>
{% endif %}
Ad

Answer

Shipping address at cart.liquid can only be accessed if visitor have already logged in. Else you can use third party tool to show a particular div specific to a particular country.

<script>
var txt = httpGet("https://ipapi.co/json/");
var obj = JSON.parse(txt);
var country = obj.country;

function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;
}
if(country == "CA"){
    // Show Div
}else{
  // Show Other Div
}        

</script>

Note: IP is used to find location of visitor.

Ad
source: stackoverflow.com
Ad