Tags: Metafields, Products, Unpublish
Use this task to automatically set product inventory to 0, and the product's inventory policy to deny out-of-stock purchases, after a date/time that you specify in a product metafield.
- View in the task library: tasks.mechanic.dev/make-products-unavailable-after-the-date-time-stored-in-product-metafields
- Task JSON, for direct import: task.json
- Preview task code: script.liquid
{
"datetime_product_metafield_namespace_dot_key__required": null,
"run_hourly__boolean": null,
"run_daily__boolean": null,
"test_mode__boolean": null
}
Learn about task options in Mechanic
mechanic/user/trigger
{% if options.run_hourly__boolean %}
mechanic/scheduler/hourly
{% elsif options.run_daily__boolean %}
mechanic/scheduler/daily
{% endif %}
mechanic/shopify/bulk_operation
Learn about event subscriptions in Mechanic
Use this task to automatically set product inventory to 0, and the product's inventory policy to deny out-of-stock purchases, after a date/time that you specify in a product metafield.
Configure the "Datetime product metafield namespace dot key" field with the namespace and key (e.g. "custom.unpublish_after") for the product metafield that holds a Shopify date or date_time value. More information on setting up new Shopify metafields can be found here.
When you run this task (or as it runs hourly/daily, per your configuration), the task will look for products who have a metafield date/time value that's in the past. For qualifying products, all inventory items with a level greater than 0 will have their levels set to exactly 0, and all variants will have their inventory policies set to "deny" (preventing sales, once the variants are all out of stock).
Note: If you choose to use the date only type for the metafield, then the task will receive those dates as midnight local shop time. So, if this task encounters a metafield date that is the same as the task run date, then that metafield date will be in the past based on a seconds-since-midnight calculation.
Find this task in the library at tasks.mechanic.dev, and use the "Try this task" button. Or, import this task's JSON export – see Importing and exporting tasks to learn how imports work.
Found a bug? Got an improvement to add? Start here: ../../CONTRIBUTING.md.
Submit your task requests for consideration by the Mechanic community, and they may be chosen for development and inclusion in the task library!