Skip to content

Commit

Permalink
convert price rules to discounts (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Oct 11, 2024
1 parent 3f76a44 commit bd8c2ed
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This task watches for newly-paid orders, and if the configured product is purcha

```liquid
shopify/orders/paid
mechanic/actions/perform
```

[Learn about event subscriptions in Mechanic](https://learn.mechanic.dev/core/tasks/subscriptions)
Expand All @@ -39,19 +40,19 @@ shopify/orders/paid

This task watches for newly-paid orders, and if the configured product is purchased, sends the customer a discount code that's just for them. Optionally, configure the discounts to only apply to a certain collection of products, and to only last for a certain number of days.

This task watches for newly-paid orders, and if the configured product is purchased, sends the customer a discount code that's just for them. If a customer purchases more than one qualified product, they will receive more than one email, each containing a unique discount code.

### Options

* **Required product ID:** The ID of the product that the customer must purchase, in order to qualify for the discount. ([Learn how to find the product ID.](https://help.usemechanic.com/articles/2946120-how-do-i-find-an-id-for-a-product-collection-order-or-something-else))
* **Discount collection ID (optional):** The ID of a specific collection of products that the discount code should be good for. ([Learn how to find the collection ID.](https://help.usemechanic.com/articles/2946120-how-do-i-find-an-id-for-a-product-collection-order-or-something-else))
* **Discount code prefix (optional):** A small piece of text to add to the beginning of the generated discount code.
* **Discount fixed amount:** The money value to be subtracted. If you choose this option, you cannot choose a discount percentage.
* **Discount percentage:** The percentage to be subtracted. If you choose this option, you cannot choose a fixed discount amount.
* **Discount applies to each line item individually:** If enabled, the discount will apply to each item ordered. If disabled, the discount will only apply once per order.
* **Discount lifetime in days:** How long the discount should be active.
* **Discount can be used by anyone:** If enabled, the discount code can be used by anyone. If disabled, the discount code can only be used by the purchasing customer.
* **Email subject, body:** The content to email to the customer. Use "DISCOUNT_CODE" as a placeholder for the generated discount code.
If a customer purchases more than one qualified product, they will receive more than one email, each containing a unique discount code.

### Options

- **Required product ID:** The ID of the product that the customer must purchase, in order to qualify for the discount. ([Learn how to find the product ID.](https://learn.mechanic.dev/techniques/finding-a-resource-id))
- **Discount collection ID (optional):** The ID of a specific collection of products that the discount code should be good for. ([Learn how to find the collection ID.](https://learn.mechanic.dev/techniques/finding-a-resource-id))
- **Discount code prefix (optional):** A small piece of text to add to the beginning of the generated discount code.
- **Discount fixed amount:** The money value to be subtracted. If you choose this option, you cannot choose a discount percentage.
- **Discount percentage:** The percentage to be subtracted (e.g. 15). If you choose this option, you cannot choose a fixed discount amount.
- **Discount applies to each line item individually:** If enabled and if a collection ID is configured, the discount will apply to each item from the collection on the order. If disabled, the discount will only apply once per order. If no collection ID is configured, this setting will be overridden to apply once oer order.
- **Discount lifetime in days:** How long the discount should be active.
- **Discount can be used by anyone:** If enabled, the discount code can be used by anyone. If disabled, the discount code can only be used by the purchasing customer.
- **Email subject, body:** The content to email to the customer. Use "DISCOUNT_CODE" as a placeholder for the generated discount code.

## Installing this task

Expand Down
Original file line number Diff line number Diff line change
@@ -1,140 +1,169 @@
{% comment %}
Options order:
{{ options.required_product_id__number_required }}
{{ options.discount_collection_id__number }}
{{ options.discount_code_prefix }}
{{ options.discount_fixed_amount__number }}
{{ options.discount_percentage__number }}
{{ options.discount_applies_to_each_line_item_individually__boolean }}
{{ options.discount_lifetime_in_days__number }}
{{ options.discount_can_be_used_by_anyone__boolean }}
{{ options.email_subject__required }}
{{ options.email_body__multiline_required }}
{% endcomment %}

{% if options.discount_percentage__number == blank and options.discount_fixed_amount__number == blank %}
{% assign required_product_id = options.required_product_id__number_required %}
{% assign discount_collection_id = options.discount_collection_id__number %}
{% assign discount_code_prefix = options.discount_code_prefix %}
{% assign discount_fixed_amount = options.discount_fixed_amount__number %}
{% assign discount_percentage = options.discount_percentage__number %}
{% assign discount_applies_to_each_line_item_individually = options.discount_applies_to_each_line_item_individually__boolean %}
{% assign discount_lifetime_in_days = options.discount_lifetime_in_days__number %}
{% assign discount_can_be_used_by_anyone = options.discount_can_be_used_by_anyone__boolean %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__multiline_required %}

{% if discount_percentage == blank and discount_fixed_amount == blank %}
{% error "Please fill either the discount percentage or discount fixed amount." %}
{% elsif options.discount_percentage__number != blank and options.discount_fixed_amount__number != blank %}

{% elsif discount_percentage != blank and discount_fixed_amount != blank %}
{% error "Please choose between the discount percentage and discount fixed amount - only one is permitted." %}
{% endif %}

{% if event.preview %}
{% capture order_json %}
{
"email": "[email protected]",
"customer": {
"admin_graphql_api_id": "gid://shopify/Customer/1234567890"
},
"line_items": [
{
"id": 1234567890,
"product_id": {{ options.required_product_id__number_required | json }},
"quantity": 1
}
]
}
{% endcapture %}

{% assign order = order_json | parse_json %}
{% if discount_collection_id != blank %}
{% assign discount_collection_id = discount_collection_id | prepend: "gid://shopify/Collection/" %}
{% endif %}

{% for line_item in order.line_items %}
{% if line_item.product_id != options.required_product_id__number_required %}
{% continue %}
{% if event.topic == "shopify/orders/paid" %}
{% if event.preview %}
{% capture order_json %}
{
"email": "[email protected]",
"customer": {
"id": 1234567890
},
"line_items": [
{
"id": 1234567890,
"product_id": {{ required_product_id }},
"quantity": 1
}
]
}
{% endcapture %}

{% assign order = order_json | parse_json %}
{% endif %}

{% if order.customer == nil %}
{% if order.customer == blank %}
{% log "This order has no related customer. Skipping" %}
{% continue %}
{% break %}
{% endif %}

{% if order.email == blank %}
{% log "This order has no email address. Skipping" %}
{% continue %}
{% break %}
{% endif %}

{% for n in (1..line_item.quantity) %}
{% assign discount_code = line_item.id | append: n | split: "" | reverse | join: "" | slice: 0, 4 | append: order.order_number | base64 | replace: "=", "" | upcase | prepend: options.discount_code_prefix %}
{% assign customer_id = order.customer.id | prepend: "gid://shopify/Customer/" %}

{% assign email_subject = options.email_subject__required | replace: 'DISCOUNT_CODE', discount_code %}
{% assign email_body = options.email_body__multiline_required | replace: 'DISCOUNT_CODE', discount_code %}
{% comment %}
-- create a discount code for each quantity of matching line items
{% endcomment %}

{% action "email" %}
{
"to": {{ order.email | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}

{% action "shopify" %}
mutation {
priceRuleCreate(
priceRule: {
allocationMethod: {% if options.discount_applies_to_each_line_item_individually__boolean %}EACH{% else %}ACROSS{% endif %}
customerSelection: {
{% if options.discount_can_be_used_by_anyone__boolean %}
forAllCustomers: true
{% else %}
customerIdsToAdd: [
{{ order.customer.admin_graphql_api_id | json }}
]
{% endif %}
}
itemEntitlements: {
{% if options.discount_collection_id__number != blank %}
collectionIds: [
{{ shop.collections[options.discount_collection_id__number].admin_graphql_api_id | json }}
]
{% else %}
targetAllLineItems: true
{% for line_item in order.line_items %}
{% if line_item.product_id != required_product_id %}
{% continue %}
{% endif %}

{% for n in (1..line_item.quantity) %}
{% assign discount_code = line_item.id | append: n | split: "" | reverse | join: "" | slice: 0, 4 | append: order.order_number | base64 | replace: "=", "" | upcase | prepend: discount_code_prefix %}

{% action "shopify" %}
mutation {
discountCodeBasicCreate(
basicCodeDiscount: {
code: {{ discount_code | json }}
customerSelection: {
{% if discount_can_be_used_by_anyone %}
all: true
{% else %}
customers: {
add: {{ array | push: customer_id | json }}
}
{% endif %}
}
title: {{ discount_code | json }}
startsAt: {{ "now" | date: "%FT%T%:z" | json }}
{% if discount_lifetime_in_days != blank %}
{% assign validity_days_s = 60 | times: 60 | times: 24 | times: discount_lifetime_in_days %}
endsAt: {{ "now" | date: "%s" | plus: validity_days_s | date: "%FT%T%:z" | json }}
{% endif %}
customerGets: {
items: {
{% if discount_collection_id != blank %}
collections: {
add: {{ array | push: discount_collection_id | json }}
}
{% else %}
all: true
{% endif %}
}
value: {
{% if discount_percentage != blank %}
percentage: {{ discount_percentage | abs | divided_by: 100.0 | json }}
{% else %}
discountAmount: {
amount: {{ discount_fixed_amount | abs | times: 1.0 | json }}
{% if discount_collection_id != blank %}
appliesOnEachItem: {{ discount_applies_to_each_line_item_individually }}
{% else %}
appliesOnEachItem: false
{% endif %}
}
{% endif %}
}
}
}
target: LINE_ITEM
title: {{ discount_code | json }}
validityPeriod: {
start: {{ "now" | date: "%FT%T%:z" | json }}

{% if options.discount_lifetime_in_days__number != blank %}
{% assign validity_days_s = 60 | times: 60 | times: 24 | times: options.discount_lifetime_in_days__number %}
end: {{ "now" | date: "%s" | plus: validity_days_s | date: "%FT%T%:z" | json }}
{% endif %}
) {
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
codes(first: 1) {
nodes {
id
code
}
}
endsAt
summary
}
}
}
value: {
{% if options.discount_percentage__number != blank %}
percentageValue: {{ options.discount_percentage__number | abs | times: -1 | json }}
{% endif %}

{% if options.discount_fixed_amount__number != blank %}
fixedAmountValue: {{ options.discount_fixed_amount__number | abs | times: -1 | append: "" | json }}
{% endif %}
userErrors {
code
extraInfo
field
message
}
}
priceRuleDiscountCode: {
code: {{ discount_code | json }}
}
) {
priceRule {
id
allocationMethod
endsAt
target
summary
}
priceRuleUserErrors {
code
field
message
}
priceRuleDiscountCode {
code
id
}
}
}
{% endaction %}
{% endaction %}
{% endfor %}
{% endfor %}
{% endfor %}

{% elsif event.topic == "mechanic/actions/perform" %}
{% comment %}
-- only process a successful discountCodeBasicCreate shopify action; the Error reporter task should be used to respond to failures
{% endcomment %}

{% unless action.type == "shopify" and action.run.ok == true %}
{% break %}
{% endunless %}

{% comment %}
-- send a customer email for the created discount code
{% endcomment %}

{% assign discount_code = action.run.result.data.discountCodeBasicCreate.codeDiscountNode.codeDiscount.codes.nodes.first.code %}

{% assign email_subject = email_subject | replace: 'DISCOUNT_CODE', discount_code %}
{% assign email_body = email_body | replace: 'DISCOUNT_CODE', discount_code %}

{% action "email" %}
{
"to": {{ event.parent.data.email | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
Loading

0 comments on commit bd8c2ed

Please sign in to comment.