Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
I've created a metaobject type called Territory which holds some basic information about which sales territory a customer belongs to. It has the following configuration (truncated to the important bits):
territory.name, territory.rep_name, ... territory.assigned_states // (List of String containing US State names [Alabama, Alaska...])
When an customer is added or updated, my flow will detect if the customer is assigned to a territory and tag associated orders accordingly. If there isn't a territory assigned, it'll attempt to assign one to the customer and then tag any associated orders as usual.
I used the new Run Code action type and have manually recreated the important bits in Javascript so I can match the customer's state (province) to the appropriate territory. I've successfully matched customers to my fake territory object, returned the appropriate metaobject ID and used downstream actions to assign the correct metaobject to the customer. While this works, I'd obviously like to avoid managing which states are attached to territories in multiple places. I'd prefer to reference the list of available metaobjects, loop through though and then loop through their associated states.
Here's the Run Code setup:
input:
query{ customer{ defaultAddress {province}, addresses {province} } }
outputs:
"The output of Run Code" type Output { "The territory ID linked by the territory Metaobject gid returned by the script" territoryId: ID "Success boolean" success: Boolean! "Debug messaging from the runtime" debugMessaging: [String] }
JS:
let messaging = []; let isSuccess = false; export default function main(input) { return { territoryId: getTerritoryId(input.customer), success: isSuccess, debugMessaging: messaging } } function getTerritoryId(customer){ const territoryMapping = [ {id:"gid://shopify/Metaobject/12345", // Territory 1 states:[ "Alabama", "Arkansas", // ... other states ]}, {id:"gid://shopify/Metaobject/23456", // Territory 2 states:[ "Connecticut", "Delaware", // ... other states ]}, {id:"gid://shopify/Metaobject/34567", // Territory 3 states:[ "Alaska", "Arizona", // ... other states ]}, {id:"gid://shopify/Metaobject/45678", // Default Territory states:[ "National" ]} ]; for ( let i = 0;i < territoryMapping.length; i++){ const territory = territoryMapping[i]; messaging.push("Found territoryMapping: " + territory.id); for ( let j = 0; j < territory.states.length; j++){ messaging.push("Checking customer state " + customer.defaultAddress.province + " against territory[" + i + "] state " + territory.states[j]); if ( customer.defaultAddress.province == territory.states[j] ){ messaging.push("Found match, returning: " + territory.id); isSuccess = true; return territory.id; } } } messaging.push("No match found...returning blank"); return ""; }
Has anyone successfully accessed a list of metaobjects usable in the Run Code action object whether referencing from an upstream action or directly in the Run Code action?
Any help is appreciated. Thanks
You currently can't get a metaobject on Flow that is unattached to a resource. We do have a project underway to add improvement Metaobject support.
If the metaobject is attached to a shop, order, customer metafield, then you can get the values. You can also access shop metafields in Flow currently.
If it's a list, the references are on "<resource>.metafields.references.fields"
If it's a single value, use "reference" instead
Paul, thanks again. Should I be able to reference this in a liquid code block or in the Run Code action? When I attempt to use what you suggested in the Run Code query section for metaobject references, I get an error. The only thing it lets me return is the type. If you mean use liquid and then a loop action downstream, I can explore that route. Thanks, Brant
Either, but the syntax would be different. I forgot you need the typename at that level. So you would input "Metaobject" and then fields.
The syntax is a bit tricky:
query{
customer {
metafields {
references {
...on Metaobject {
fields {
key
}
}
}
}
}
}
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024