The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

storeDataInJSON function - what calls it? What does it do?

I've got a custom datepicker on my site that allows customers to put orders on hold. On my product page (pre-FoxyCart), the customer can pick a date before they add their order to the cart. When they pick a data, this gets executed:
FC.client.request('https://'+FC.settings.storedomain+'/cart?h:DeliveryDate=' + $("#delivery-date-picker").val());

That way, the date is set in FC.json.custom_fields.DeliveryDate when they get to the cart (right?), and it populates the date picker on my checkout page so customers can change it there if they want.

Here's the thing, though. Customers are having problems with dates being set when they didn't mean to or the incorrect date is being set. I stepped through the code as the cart loads and noticed that a function named storeDataInJSON() runs. This function winds up setting FC.json.DeliveryDate. I think this is the root of the problem I'm having. I've been using and manipulating FC.json.custom_fields.DeliveryDate, but FC.json.DeliveryDate is also set. So, questions:
  • My call from the product page IS setting FC.json.custom_fields.DeliveryDate, right? It's not setting FC.json.DeliveryDate too, is it?
  • Assuming the previous point is correct, what is the storeDataInJSON function doing? How do I keep DeliveryDate from being set there?
  • SHOULD I be preventing DeliveryDate from being set in FC.json.DeliveryDate, or should I clear FC.json.custom_fields.DeliveryDate on load and use FC.json.DeliveryDate instead?
  • brettbrett FoxyCart Team
    Great questions, @summersab. We'll take a look and dig in, but my initial thought is that nothing should be getting set at the `FC.json` level, so that seems weird.
  • After digging a bit more, this may not be the cause of my issue. The storeDataInJSON funciton IS setting the field at the FC.json level, but it consistently mirrors the field set in custom_fields.

    I think my issue is on my product page. It looks like if a customer picks a date in their cart, if they go back to a product page, the value remains set but isn't populated to the datepicker on my product page or when they return to the cart. It just kinda remains set in the background.

    So . . . no real need to follow up here unless you think that having the values set at the FC.json level is a problem.
  • fc_adamfc_adam FoxyCart Team

    Good questions.

    Currently the custom session attributes (those added to the cart with h: are considered different/separate than custom checkout fields. Custom session attributes are stored inside FC.json.custom_fields, but custom checkout fields do currently get saved into the JSON as FC.json. There isn't any crossover between the two in terms of where it's saved in the JSON.

    The storeDataInJSON() function is just used to ensure that the values entered by the customer on the checkout are updated in the JSON so that any client-side rendering has the right value.

    That does make it difficult to manage if you're doing both - setting from the product page, but then also allowing updates on the checkout. It would be possible to have the custom session attribute from the product populate the custom checkout field if it's not already set - but it's a little more involved in updating the custom session attribute with the checkout data if the customer goes back. You would need to perform a cart request whenever the field is updated on the checkout - which can work, but can make things a little interesting. Another option - which may not be desirable, would be to just manage the date on one side - either just on the products, or just on the checkout.

    For the product page - you can get that to use the existing custom value too - you'd use javascript to pull that out from the JSON object and update the calendar.
Sign In or Register to comment.