forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-a-product-inventory-feed.json
23 lines (23 loc) · 3.49 KB
/
create-a-product-inventory-feed.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"docs": "This task, paired with the linked instructions, creates a feed of product inventory (optionally filtering by product type).\n\nThis task creates a regularly-updated feed, available at yourstore.com/pages/feed. Follow these required instructions to configure your store:\n\n[Tutorial: Creating scheduled CSV feeds](https://learn.mechanic.dev/resources/tutorials/creating-scheduled-csv-feeds)\n\nNote: This task fetches up to 30 variants per product.",
"halt_action_run_sequence_on_error": false,
"name": "Create a product inventory CSV feed",
"online_store_javascript": null,
"options": {
"filter_by_product_type": "Shirt"
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign csv = array %}\n{% assign csv[0] = \"Handle,Title,Option1 Name,Option1 Value,Option2 Name,Option2 Value,Option3 Name,Option3 Value,SKU,Quantity\" | split: \",\" %}\n\n{% assign cursor = nil %}\n{% for n in (0..1000) %}\n {% capture query %}\n query {\n products(\n first: 15\n after: {{ cursor | json }}\n sortKey: CREATED_AT\n query: {% if options.filter_by_product_type == blank %}null{% else %}{{ options.filter_by_product_type | json | prepend: \"product_type:\" | json }}{% endif %}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n handle\n title\n variants(first: 30) {\n edges {\n node {\n sku\n inventoryQuantity\n selectedOptions {\n name\n value\n }\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% for product_edge in result.data.products.edges %}\n {% assign product = product_edge.node %}\n\n {% for variant_edge in product.variants.edges %}\n {% assign variant = variant_edge.node %}\n\n {% assign row = array %}\n {% assign row[row.size] = product.handle %}\n {% assign row[row.size] = product.title %}\n {% assign row[row.size] = variant.selectedOptions[0].name %}\n {% assign row[row.size] = variant.selectedOptions[0].value %}\n {% assign row[row.size] = variant.selectedOptions[1].name %}\n {% assign row[row.size] = variant.selectedOptions[1].value %}\n {% assign row[row.size] = variant.selectedOptions[2].name %}\n {% assign row[row.size] = variant.selectedOptions[3].value %}\n {% assign row[row.size] = variant.sku %}\n {% assign row[row.size] = variant.inventoryQuantity %}\n\n {% assign csv[csv.size] = row %}\n {% endfor %}\n {% endfor %}\n\n {% if result.data.products.pageInfo.hasNextPage %}\n {% assign cursor = result.data.products.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n\n{\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"create\",\n \"metafield\",\n {\n \"namespace\": \"mechanic\",\n \"key\": \"feed\",\n \"type\": \"multi_line_text_field\",\n \"value\": {{ csv | csv | json }}\n }\n ]\n }\n}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/scheduler/daily",
"mechanic/scheduler/daily+12.hours"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/scheduler/daily\nmechanic/scheduler/daily+12.hours",
"tags": [
"Feeds",
"Inventory",
"Products"
]
}