-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept-a-maximum-number-of-orders-per-day.json
25 lines (25 loc) · 14 KB
/
accept-a-maximum-number-of-orders-per-day.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"docs": "This task works by setting your inventory to zero when the order limit is reached. (Specifically, this means setting inventory levels to 0 for all items that have a greater-than-zero inventory level.) When the limit is reached, your inventory will be dropped to zero. If your store is configured to stop selling out-of-stock products, your customers will be prevented from making additional purchases.\n\nAnd, this task can restore inventory to its original levels at midnight the next day, or on demand. (Restore levels on demand by enabling this option, then using the \"Run task\" button.).\n\n### Important notes\n\n* This task only works for products that have inventory tracking enabled, which are configured to be unavailable when out of stock. This means that the \"Track quantity\" product option needs to be enabled, and the \"Continue selling when out of stock\" option needs to be disabled, for all products.\n* The \"Only clear inventory for products with this tag\" does _not_ change which orders are considered for the daily maximum. Use the \"Only count orders matching this query\" option to filter orders for counting, using the same order search syntax as the Shopify admin.\n* The \"Only clear inventory for products with this tag\" setting is case-sensitive - it must match product tags exactly.\n* **This task may be not behave as intended if you receive multiple orders per minute. We don't recommend using it for high-volume stores.**",
"halt_action_run_sequence_on_error": false,
"name": "Accept a maximum number of orders per day",
"online_store_javascript": null,
"options": {
"maximum_daily_orders__number_required": "10",
"only_count_orders_matching_this_query": "-status:cancelled",
"only_clear_inventory_for_products_with_this_tag": "",
"restore_inventory_levels_the_next_day__boolean": true,
"restore_inventory_levels_on_demand__boolean": false
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Options order:\n\n {{ options.maximum_daily_orders__number_required }}\n {{ options.only_count_orders_matching_this_query }}\n {{ options.only_clear_inventory_for_products_with_this_tag }}\n {{ options.restore_inventory_levels_the_next_day__boolean }}\n {{ options.restore_inventory_levels_on_demand__boolean }}\n{% endcomment %}\n\n{% if options.maximum_daily_orders__number_required <= 0 %}\n {% error \"'Maximum daily orders' must be at least 1. :)\" %}\n{% endif %}\n\n{% assign inventory_is_zeroed_cache_key = \"inventory_is_zeroed:\" | append: task.id %}\n{% assign inventory_is_zeroed = cache[inventory_is_zeroed_cache_key] | default: false %}\n\n{% if event.topic contains \"shopify/orders\" %}\n {% assign cursor = nil %}\n {% assign orders_today = 0 %}\n {% assign previous_midnight = \"now\" | date: \"%Y-%m-%dT00:00:00%z\" %}\n {% assign previous_midnight_s = previous_midnight | date: \"%s\" %}\n {% assign base_cache_key = \"inventory_to_restore:\" | append: previous_midnight_s %}\n\n {% capture orders_query %}\n created_at:>={{ previous_midnight | json }} {{ options.only_count_orders_matching_this_query }}\n {% endcapture %}\n\n {% assign orders_query = orders_query | strip %}\n\n {% for n in (0..100) %}\n {% capture query %}\n query {\n orders(\n first: 250\n after: {{ cursor | json }}\n query: {{ orders_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% assign inventory_is_zeroed = false %}\n\n {% capture result_json %}\n {\n \"data\": {\n \"orders\": {\n \"nodes\": [\n {% for n in (1..options.maximum_daily_orders__number_required) %}\n {\n \"id\": \"gid://shopify/Order/1234567890\"\n }{% unless forloop.last %},{% endunless %}\n {% endfor %}\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign orders_today = orders_today | plus: result.data.orders.nodes.size %}\n\n {% if result.data.orders.pageInfo.hasNextPage %}\n {% assign cursor = result.data.orders.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% log orders_today_thus_far: orders_today, orders_query: orders_query, inventory_is_already_zeroed: inventory_is_zeroed %}\n\n {% if orders_today >= options.maximum_daily_orders__number_required and inventory_is_zeroed == false %}\n {% action \"cache\", \"set\", inventory_is_zeroed_cache_key, true %}\n\n {% assign cursor = nil %}\n {% assign inventory_adjustments = array %}\n\n {% for n in (0..2000) %}\n {% capture query %}\n query {\n inventoryItems(\n first: 3\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n variant {\n product {\n tags\n }\n }\n inventoryLevels(first: 100) {\n nodes {\n id\n quantities(names: \"available\") {\n name\n quantity\n }\n location {\n id\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"inventoryItems\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/InventoryItem/1234567890\",\n \"variant\": {\n \"product\": {\n \"tags\": [\n {% if options.only_clear_inventory_for_products_with_this_tag != blank %}\n {{ options.only_clear_inventory_for_products_with_this_tag | json }}\n {% endif %}\n ]\n }\n },\n \"inventoryLevels\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890\",\n \"quantities\": [\n {\n \"name\": \"available\",\n \"quantity\": 20\n }\n ],\n \"location\": {\n \"id\": \"gid://shopify/Location/1234567890\"\n }\n }\n ]\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for inventory_item in result.data.inventoryItems.nodes %}\n {% if options.only_clear_inventory_for_products_with_this_tag != blank %}\n {% unless inventory_item.variant.product.tags contains options.only_clear_inventory_for_products_with_this_tag %}\n {% continue %}\n {% endunless %}\n {% endif %}\n\n {% for inventory_level in inventory_item.inventoryLevels.nodes %}\n {% if inventory_level.quantities.first.quantity > 0 %}\n {% comment %}\n NOTE: use availble quantity in the hash to be stored in cache... however, a negative delta will be made in the mutation to zero out the inventory\n {% endcomment %}\n\n {% assign inventory_adjustment = hash %}\n {% assign inventory_adjustment[\"inventoryItemId\"] = inventory_item.id %}\n {% assign inventory_adjustment[\"locationId\"] = inventory_level.location.id %}\n {% assign inventory_adjustment[\"delta\"] = inventory_level.quantities.first.quantity %}\n {% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% if result.data.inventoryItems.pageInfo.hasNextPage %}\n {% assign cursor = result.data.inventoryItems.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if inventory_adjustments == blank %}\n {% break %}\n {% endif %}\n\n {% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_inventory_adjustments in groups_of_inventory_adjustments %}\n {% action \"shopify\" %}\n mutation {\n inventoryAdjustQuantities(\n input: {\n reason: \"correction\"\n name: \"available\"\n changes: [\n {% for inventory_adjustment in group_of_inventory_adjustments -%}\n {\n inventoryItemId: {{ inventory_adjustment.inventoryItemId | json }}\n locationId: {{ inventory_adjustment.locationId | json }}\n delta: {{ inventory_adjustment.delta | times: -1 }}\n }\n {%- endfor %}\n ]\n }\n ) {\n inventoryAdjustmentGroup {\n reason\n changes {\n name\n delta\n quantityAfterChange\n item {\n id\n sku\n }\n location {\n name\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% comment %}\n -- store the inventory to restore in cache even if neither restore option is selected... just in case :)\n {% endcomment %}\n\n {% assign cache_key = base_cache_key | append: \"_group\" | append: forloop.index0 %}\n {% assign cache_value = hash %}\n {% assign cache_value[\"value\"] = group_of_inventory_adjustments %}\n\n {% unless forloop.last %}\n {% assign next_cache_key = base_cache_key | append: \"_group\" | append: forloop.index %}\n {% assign cache_value[\"next_key\"] = next_cache_key %}\n {% endunless %}\n\n {% action \"cache\", \"set\", cache_key, cache_value %}\n {% endfor %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/scheduler/daily\" or event.topic == \"mechanic/user/trigger\" %}\n {% action \"cache\", \"del\", inventory_is_zeroed_cache_key %}\n {% assign cache_keys = array %}\n\n {% assign day_in_s = 60 | times: 60 | times: 24 %}\n {% assign midnight = \"now\" | date: \"%s\" | minus: day_in_s | date: \"%Y-%m-%dT00:00:00%z\" %}\n {% assign midnight_s = midnight | date: \"%s\" %}\n {% assign cache_key = \"inventory_to_restore:\" | append: midnight_s | append: \"_group0\" %}\n {% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}\n\n {% if inventory_adjustments_cache_group == blank %}\n {% assign midnight = \"now\" | date: \"%Y-%m-%dT00:00:00%z\" %}\n {% assign midnight_s = midnight | date: \"%s\" %}\n {% assign cache_key = \"inventory_to_restore:\" | append: midnight_s | append: \"_group0\" %}\n {% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}\n {% endif %}\n\n {% if event.preview %}\n {% assign inventory_adjustment = hash %}\n {% assign inventory_adjustment[\"inventoryItemId\"] = \"gid://shopify/InventoryItem/1234567890\" %}\n {% assign inventory_adjustment[\"locationId\"] = \"gid://shopify/Location/1234567890\" %}\n {% assign inventory_adjustment[\"delta\"] = 20 %}\n {% assign inventory_adjustments_cache_group[\"value\"] = array | push: inventory_adjustment %}\n {% endif %}\n\n {% if inventory_adjustments_cache_group == blank %}\n {% error\n message: \"No cached inventory data was found!\",\n cache_key: cache_key\n %}\n {% break %}\n\n {% else %}\n {% for n in (0..1000) %}\n {% action \"shopify\" %}\n mutation {\n inventoryAdjustQuantities(\n input: {\n reason: \"correction\"\n name: \"available\"\n changes: {{ inventory_adjustments_cache_group.value | graphql_arguments }}\n }\n ) {\n inventoryAdjustmentGroup {\n reason\n changes {\n name\n delta\n quantityAfterChange\n item {\n id\n sku\n }\n location {\n name\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% assign cache_keys = cache_keys | push: cache_key %}\n\n {% if inventory_adjustments_cache_group.next_key %}\n {% assign cache_key = inventory_adjustments_cache_group.next_key %}\n {% assign inventory_adjustments_cache_group = cache[cache_key] %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% for cache_key in cache_keys %}\n {% action \"cache\", \"del\", cache_key %}\n {% endfor %}\n {% endif %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create",
"mechanic/scheduler/daily"
],
"subscriptions_template": "shopify/orders/create\n{% if options.restore_inventory_levels_the_next_day__boolean %}\n mechanic/scheduler/daily\n{% endif %}\n{% if options.restore_inventory_levels_on_demand__boolean %}\n mechanic/user/trigger\n{% endif %}",
"tags": [
"Max Orders",
"Orders"
]
}