Setting the cart language only if a locale is not already set

PhilippePhilippe Member
Hi,

We set the template (and therefore the locale and language) in our add to cart buttons, via
<input name="template_set" type="hidden" value="xxxxxxx" />


We also have a button in the header, that just calls for viewing the cart - this works well if the template and language have already been set by adding a product.

We have a minor issue, however, if the customer tries to view the (empty cart) before adding the product - the cart defaults to English.
Here is our question:
Is it possible , when outputting the header, to query whether a locale is already set in the json object ; and store the answer in a PHP $variable?

[Then, based on the outcome, we would either:
- do nothing if a template was already present

or

- use the Geoip info to chose and set a locale using PHP and
echo ' <input name="template_set" type="hidden" value="xxxxxxx" /> '
added to the view cart form]


Thanks,

Site runs on word press and sample test page is at http://testml.isails.com/laser-store/
Comments
  • fc_adamfc_adam FoxyCart Team
    edited October 6
    @Philippe,

    Good question. Could I confirm with you - you don't use the default template set at all, instead you'll always use one of the other custom template sets you've created? If so - you could tackle this with javascript - hooking into the ready.done event and checking the session address to set it dynamically.

    As an example of what that could look like:
    var FC = FC || {};
    FC.onLoad = function() {
    FC.client.on("ready.done", function() {
    if (FC.json.shipping_address.country != "" && (FC.json.template_set == "DEFAULT" || FC.json.template_set == "")) {
    // Still on the default template and we know the country
    var country = FC.json.shipping_address.country;
    var template_set = "";
    if (country == "US") {
    template_set = "en-USD";
    } else if (country == "AU") {
    template_set = "en-AUD"
    }

    if (template_set != "") {
    // We have a template set to use
    FC.cart.request({"template_set": template_set});
    }
    }
    });
    };
    You would include that code on your own website - and it will run as soon as the FoxyCart javascript is initialised on your page. It should get a template set configured before the customer hits the view cart link. You'll just need to expand on it to include the other countries and template sets you have.
  • Thank,
    I seems that this code would rely on a country variable being present in the json object , based on the line below?
    var country = FC.json.shipping_address.country;

    My concern is when the customer visits for the very first time, and there is no content in the Json object to pull info from, and the customer somewhat calls an empty cart (do not ask me why - they do) - the cart would come in our default language automatically, rather than in their own.

    I think I can make this work though, as when one registers a js function in world press, one can localize it by passing variables - in this case I would pass the geoIP info to the js function , and the function, modified accordingly would be able to access the JSon object and feed it the relevant template, if the template were empty in the first place .

    I need to noodle on this a bit - and will report success here - or access additional questions....

  • fc_adamfc_adam FoxyCart Team
    @Philippe,

    If we can tell the customers country from their IP - it'll be there when the session starts, so you should be fine to take the approach shown in the javascript. If it doesn't do what you're after though - the approach you detailed could also work.
  • Additional connected question - in setting the templates, can I use locales that do not "exist", for example fr_US -- that one would be useful for customers from Quebec, who often purchase from our US store ?
  • fc_adamfc_adam FoxyCart Team
    @Philippe,

    You can certainly create a template set that would mimic that kind of locale - you'd set the locale to en_US, but then set the language to French. That would then result in a USD based store with French language strings.
  • Adam,

    I have customized the code above to our needs (posted it below in case you need to look at it).

    I am messing with foreign anonymous proxies to see how this behaves when loading our pages from abroad - I do need some clarification on how information is collected and fed into the cart: even if I empty the cart, then quit and restart the browser, then clear the cache, then load my page some json data seems to persist; and part seems to be FC.json.shipping_address.country - which would prevent the code below to set a template.....

    How are variables like FC.json.shipping_address.country, currency, language, etc set a the beginning of a session, before adding product to the cart, or conserved from previous sessions ?

    Home page we are trying to figure this out on is :
    testml.isails.com

    Cart button is in top right corner - and store subdomain is isailsteststore.foxycart.com


    var FC = FC || {};
    FC.onLoad = function() {
    FC.client.on("ready.done", function() {
    if (FC.json.shipping_address.country != "" && (FC.json.template_set == "DEFAULT" || FC.json.template_set == "")) {
    // Still on the default template and we know the country
    var fccountry = FC.json.shipping_address.country;
    var template_set = "";
    if (fccountry == "GB" || fccountry == "AT" || fccountry == "BE" || fccountry == "BG" || fccountry == "CY" || fccountry == "CZ" || fccountry == "DK" || fccountry == "DE" || fccountry == "EE" || fccountry == "IE" || fccountry == "GL" || fccountry == "HU" || fccountry == "IS" || fccountry == "LV" || fccountry == "LT" || fccountry == "LU" || fccountry == "MT" || fccountry == "NL" || fccountry == "NO" || fccountry == "PL" || fccountry == "PT" || fccountry == "RO" || fccountry == "SI" || fccountry == "SK" || fccountry == "SE" || fccountry == "CH" || fccountry == "GR") {
    template_set = "en-EUR";
    } else if (fccountry == "AU" || fccountry == "NZ") {
    template_set = "en-AUD"
    } else if (fccountry == "FR") {
    template_set = "fr-EUR"
    } else if (fccountry == "IT") {
    template_set = "it-EUR"
    } else if (fccountry == "ES") {
    template_set = "es-EUR"
    } else {
    template_set = "en-USD";
    }

    if (template_set != "") {
    // We have a template set to use
    FC.cart.request({
    "template_set": template_set
    });
    }
    }
    });
    };
  • Update -- I went withe alternative solutions discussed above and it works like champ (using geoip to set the cart when there is no "add to cart " to force a template.

    I am still interest, however, in knowing how are variables like FC.json.shipping_address.country, currency, language, etc are set a the beginning of a session, before adding product to the cart, or conserved from previous sessions ?

    Even after emptying the cart, emptying the cache and reloading the page, there still seems to be some data present like currency, etc...
  • brettbrett FoxyCart Team
    @Philippe, unless you're seeing something strange (and you could try doing an incognito window, or an empty=reset request, to be sure you're seeing a new session), the country should be set from our GeoIP data. Currency and language should be what your store's default template set is configured with. If you're seeing something other than that, let us know :)
Sign In or Register to comment.