Skip to content

Commit

Permalink
convert remaining metafieldSet task due to deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus committed Sep 30, 2024
1 parent 1c1f918 commit ea6334b
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 197 deletions.
148 changes: 78 additions & 70 deletions docs/advanced-scheduled-price-changes/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

{% if valid_start_datetime == blank %}
{% unless event.preview %}
{% error "The event start date is not a valid date. Please re-enter it (per the task documentaion) and try scheduling the event again." %}
{% error "The event start date is not a valid date. Please re-enter it (per the task documentation) and try scheduling the event again." %}
{% break %}
{% endunless %}
{% endif %}
Expand All @@ -88,7 +88,7 @@

{% if valid_end_datetime == blank %}
{% unless event.preview %}
{% error "The event end date is not a valid date. Please re-enter it (per the task documentaion) and try scheduling the event again." %}
{% error "The event end date is not a valid date. Please re-enter it (per the task documentation) and try scheduling the event again." %}
{% break %}
{% endunless %}
{% endif %}
Expand All @@ -100,7 +100,7 @@

{% if start_datetime_s <= now or start_datetime_s >= end_datetime_s %}
{% unless event.preview %}
{% error "The event start and end dates must be future dates and the start date must be before the end date. Please re-enter them (per the task documentaion) and try scheduling the event again." %}
{% error "The event start and end dates must be future dates and the start date must be before the end date. Please re-enter them (per the task documentation) and try scheduling the event again." %}
{% break %}
{% endunless %}
{% endif %}
Expand Down Expand Up @@ -137,7 +137,7 @@
{% if discount != discount_string %}
{% unless event.preview %}
{% error
message: "Percentage discount entry incorrect. Please update the task configuration (per the task documentaion) and try scheduling the event again.",
message: "Percentage discount entry incorrect. Please update the task configuration (per the task documentation) and try scheduling the event again.",
discount: discount
%}
{% break %}
Expand All @@ -147,7 +147,7 @@
{% if discount_percentage <= 0 or discount_percentage >= 100 %}
{% unless event.preview %}
{% error
message: "Percentage discount entries should contain an number between 0 and 100. Please update the task configuration (per the task documentaion) and try scheduling the event again.",
message: "Percentage discount entries should contain an number between 0 and 100. Please update the task configuration (per the task documentation) and try scheduling the event again.",
discount: discount
%}
{% break %}
Expand All @@ -160,7 +160,7 @@
{% if discount_absolute == 0 %}
{% unless event.preview %}
{% error
message: "Fixed price and fixed discount entries must contain an absolute number > 0. Please update the task configuration (per the task documentaion) and try scheduling the event again.",
message: "Fixed price and fixed discount entries must contain an absolute number > 0. Please update the task configuration (per the task documentation) and try scheduling the event again.",
discount: discount
%}
{% break %}
Expand Down Expand Up @@ -1014,7 +1014,7 @@

{% for product in products %}
{% assign variant_updates = array %}
{% assign mutations = array %}
{% assign metafields_to_delete = array %}

{% assign variants_with_metafield = product.variants.edges | map: "node" | where: "metafield" %}

Expand All @@ -1026,44 +1026,48 @@
-- revert the prices on this variant and delete the metafield
{% endcomment %}

{% capture variant_update %}
{
id: {{ variant.id | json}}
price: {{ metafield.original_price | json }}
{% if metafield.original_compare_at_price %}compareAtPrice: {{ metafield.original_compare_at_price | json }}{% endif %}
}
{% endcapture %}

{% assign variant_update = hash %}
{% assign variant_update["id"] = variant.id %}
{% assign variant_update["price"] = metafield.original_price %}
{% if metafield.original_compare_at_price %}
{% assign variant_update["compareAtPrice"] = metafield.original_compare_at_price %}
{% endif %}
{% assign variant_updates = variant_updates | push: variant_update %}

{% capture mutation %}
mutation {
metafieldDelete(
input: {
id: {{ variant.metafield.id | json }}
}
) {
deletedId
userErrors {
field
message
}
}
}
{% endcapture %}

{% assign mutations = mutations | push: mutation %}
{% assign metafield_to_delete = hash %}
{% assign metafield_to_delete["ownerId"] = variant.id %}
{% assign metafield_to_delete["namespace"] = variant_metafield_namespace | json %}
{% assign metafield_to_delete["key"] = variant_metafield_key | json %}
{% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}
{% endif %}
{% endfor %}

{% if metafields_to_delete != blank %}
{% action "shopify" %}
mutation {
metafieldsDelete(
metafields: {{ metafields_to_delete | graphql_arguments }}
) {
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}

{% if variant_updates != blank %}
{% action "shopify" %}
mutation {
productVariantsBulkUpdate(
productId: {{ product.id | json }}
variants: [
{{ variant_updates | join: newline }}
]
variants: {{ variant_updates | graphql_arguments }}
) {
product {
id
Expand All @@ -1084,10 +1088,6 @@
}
{% endaction %}
{% endif %}

{% for mutation in mutations %}
{% action "shopify" mutation %}
{% endfor %}
{% endfor %}

{% if products_result.data.products.pageInfo.hasNextPage %}
Expand Down Expand Up @@ -1267,7 +1267,7 @@

{% for product in products %}
{% assign variant_updates = array %}
{% assign mutations = array %}
{% assign metafields_to_delete = array %}

{% assign variants_with_metafield = product.variants.edges | map: "node" | where: "metafield" %}

Expand All @@ -1284,43 +1284,47 @@
-- revert the prices on this variant and delete the metafield
{% endcomment %}

{% capture variant_update %}
{
id: {{ variant.id | json}}
price: {{ metafield.original_price | json }}
{% if metafield.original_compare_at_price %}compareAtPrice: {{ metafield.original_compare_at_price | json }}{% endif %}
}
{% endcapture %}

{% assign variant_update = hash %}
{% assign variant_update["id"] = variant.id %}
{% assign variant_update["price"] = metafield.original_price %}
{% if metafield.original_compare_at_price %}
{% assign variant_update["compareAtPrice"] = metafield.original_compare_at_price %}
{% endif %}
{% assign variant_updates = variant_updates | push: variant_update %}

{% capture mutation %}
{% assign metafield_to_delete = hash %}
{% assign metafield_to_delete["ownerId"] = variant.id %}
{% assign metafield_to_delete["namespace"] = variant_metafield_namespace | json %}
{% assign metafield_to_delete["key"] = variant_metafield_key | json %}
{% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}
{% endfor %}

{% if metafields_to_delete != blank %}
{% action "shopify" %}
mutation {
metafieldDelete(
input: {
id: {{ variant.metafield.id | json }}
}
metafieldsDelete(
metafields: {{ metafields_to_delete | graphql_arguments }}
) {
deletedId
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endcapture %}

{% assign mutations = mutations | push: mutation %}
{% endfor %}
{% endaction %}
{% endif %}

{% if variant_updates != blank %}
{% action "shopify" %}
mutation {
productVariantsBulkUpdate(
productId: {{ product.id | json }}
variants: [
{{ variant_updates | join: newline }}
]
variants: {{ variant_updates | graphql_arguments }}
) {
product {
id
Expand All @@ -1341,10 +1345,6 @@
}
{% endaction %}
{% endif %}

{% for mutation in mutations %}
{% action "shopify" mutation %}
{% endfor %}
{% endfor %}

{% if products_result.data.products.pageInfo.hasNextPage %}
Expand All @@ -1360,12 +1360,20 @@

{% action "shopify" %}
mutation {
metafieldDelete(
input: {
id: {{ shop.metafield.id | json }}
}
metafieldsDelete(
metafields: [
{
ownerId: {{ shop.id | json }}
namespace: {{ shop_metafield_namespace | json }}
key: {{ shop_metafield_key | json }}
}
]
) {
deletedId
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
Expand Down
12 changes: 5 additions & 7 deletions docs/auto-remove-a-customer-tag-x-days-after-its-added/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Tags: Schedule, Untag

Use this this task to monitor for the addition of a new customer tag, and to schedule the customer to be untagged some number of days later. Useful for granting temporary access to discounts, or other resources.
Use this this task to monitor for the addition of a specific customer tag, and to schedule the customer to be untagged a configurable number of days later. Useful for granting temporary access to discounts, or other resources.

* View in the task library: [tasks.mechanic.dev/auto-remove-a-customer-tag-x-days-after-its-added](https://tasks.mechanic.dev/auto-remove-a-customer-tag-x-days-after-its-added)
* Task JSON, for direct import: [task.json](../../tasks/auto-remove-a-customer-tag-x-days-after-its-added.json)
Expand Down Expand Up @@ -30,13 +30,11 @@ user/task/untag_customer

## Documentation

Use this this task to monitor for the addition of a new customer tag, and to schedule the customer to be untagged some number of days later. Useful for granting temporary access to discounts, or other resources.
Use this this task to monitor for the addition of a specific customer tag, and to schedule the customer to be untagged a configurable number of days later. Useful for granting temporary access to discounts, or other resources.

This task monitors new and updated customers, watching for the configured customer tag.

As soon as that tag is detected, the task will _add_ a second tag, indicating that the customer is scheduled to be untagged. (For example, if the task is configured to watch for the tag "Approved", the task will _add_ the tag "Approved - will be auto-removed by Mechanic".) The task will then schedule a followup event for the future, according to the configured number of days to wait. At that time, the task will remove both tags.

Important note: To _prevent_ the task from untagging the customer later, manually remove the task's additional tag (i.e. the "will be auto-removed by Mechanic" tag). If the additional tag is found missing, the task will leave the tag in place instead of auto-removing it.
As soon as that tag is detected, the task will _add_ a second tag, indicating that the customer is scheduled to be untagged. (For example, if the task is configured to watch for the tag "Approved", the task will _add_ the tag "Approved - will be auto-removed by Mechanic".) The task will then schedule a followup event for the future, according to the configured number of days to wait. At that time, the task will remove both tags.

Important note: To _prevent_ the task from untagging the customer later, manually remove the task's additional tag (i.e. the "will be auto-removed by Mechanic" tag).

## Installing this task

Expand Down
Loading

0 comments on commit ea6334b

Please sign in to comment.