passing user agent to the transaction datafeed

GeoffreyGeoffrey Member
I am trying to get the user agent at checkout and pass it to the datafeed as a hidden custom field. For some reason it does not seem to work. I've tried all kinds of variations on the following idea:

Using the custom checkout field:
<input type="hidden" id="user_agent" name="h:user_agent" />

And the script:
function getAgent() {

var userAgentString = navigator.userAgent;
console.log("data: "+userAgentString);

jQuery( "#user_agent" ).val(userAgentString);

var result = jQuery( "#user_agent" ).val();
console.log("#user_agent value: "+result);
}

FC.client.on("ready.done", getAgent);


My console log returns the following results:
data: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
#user_agent value: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36


According to the console log, it seems that the value has been set correctly, yet nothing is passed to the datafeed for this field. The field is only passed to the datafeed when I manually enter a value attribute for it, like:

<input type="hidden" id="user_agent" name="h:user_agent" value="something" />

What am I doing wrong or missing?
Comments
  • fc_adamfc_adam FoxyCart Team
    @Geoffrey,

    I'd say the value of your custom field is being lost on the re-render of the checkout. A quick fix for that is to switch from running the event on ready.done to do it on render.done instead. The other way to approach it would be to trigger a blur event on the hidden input after setting the value, and updating the HTML to output the value from the twig. The blur event ensures the value is saved into the JSON - and then the twig value outputs it when rendered. The HTML would look like this (because twig won't like the colon character in a normal output style):
    <input type="hidden" id="user_agent" name="h:user_agent" value="{{ _context['h:user_agent'] }}" />
  • @fc_adam, changing to render.done did it. Thanks so much!
  • fc_adamfc_adam FoxyCart Team
    @Geoffrey,

    Awesome - glad it worked!
Sign In or Register to comment.