I wonder if this would be achievable using Shopify Flow. And how to set it up if so:
When an order is created, I need to populate an order metafield ‘custom.deliverytime’ with the latest date from the associated variants metafield ‘custom.nextdelivery’.
Alternatively - if latest date just is not achievable - the flow should populate as a list metafield with all the dates from associated variants.
What do you think - it is achievable? I’ve spend a few hours myself trying to figure something out, but failed so far.
Thanks for pointing me in the right direction. I’ve given the run code a go, but somehow it isnt returning the proper value, but instead the fall back value that I set. Any bright ideas what the reason is? This is the code. The input seems to be presented correct as I’m able to see the value in the input part in flow.
export default function main(payload) {
// Variant metafield information
const metaFieldNamespace = “custom”;
const metaFieldKey = “nextdelivery”; // Ensuring we’re looking for the right field
// Ensure we correctly access line items
const lineItems = payload?.lineItems || ;
if (!metaField) return null; // If the metafield doesn’t exist, skip it
// Ensure the value is treated as a date
const dateValue = new Date(metaField.value);
return isNaN(dateValue.getTime()) ? null : dateValue;
})
.filter(date => date !== null); // Remove null values
// Find the latest date
const latestDate = dates.length > 0
? new Date(Math.max(…dates.map(date => date.getTime())))
: null;
// If no valid date is found, use fallback “2024-01-01”
const formattedDate = latestDate
? latestDate.toISOString().split(“T”)[0] // Convert to YYYY-MM-DD format
: “2024-01-01”; // Default fallback
// Return the latest date in proper format
return {
latest_delivery_date: formattedDate
};
}
Can I ask you a follow up on this matter. Would it be possible having flow create and send forward a weekly list with all open orders where the new order meta field is a date within the next 10 days?
You can’t yet query orders by date metafields. So you would have to use Get order data to get open orders and then check that metafield via Run code in Flow. Run code is needed because of the date math required. Flow has a limit of 100 order for Get order action, so if you have more than 100 open orders at any time, it will only get 100 of them.