I’m in a need of current UTC timestamp in my Cart & Checkout validation function but can’t seem to get it. I see that the docs has LocalTime to get the Date but not the time itself.
Does anyone know how to get the UTC timestamp?
My usecase: We lock customer accounts for certain period and we store the currentTimestap + period in a metafield.
Hi @Jacob_Mellor the problem here is that Shopify Functions (including the Cart and Checkout validation functions) do not expose the current time. They are designed to be deterministic/pure functions, meaning the output should depend solely on the input, not on external data like system time.
If you need the current UTC timestamp, you’ll have to pass it into the function as part of the input payload or derive it in some other way (from your storefront JS, a separate app, etc.).
@daniel how would you pass in the current time to the graphql input? I had a look at metafields but they seem to only take static json as input.
my use-case is slightly different to Jacob’s, I want to reserve stock for n minutes for a customer once they have placed it into their cart. This means I return stock to circulation and invalidate the lineItem after that time has elapsed.
For now, I have decided to pass in the current UTC timestamp in cart attribute and use that in the function.
cart {
utc: attribute(key: "_utc"){
value
}
}
And while using it in the function, to be extra safe, checked the timestamp value so it’s not older than shop’s date timestamp and it’s not later than today.
Interesting, how are you adding the attribute to the cart and is the time updated when the query is run? or are you constantly updating the attribute with setInterval or similar?
I am writing an app that includes a validation function for store owners to include on their store, so I do not have access to their theme which might be different to you usecase.