Skip to content

Commit

Permalink
correct loop variable; add percentage discount option (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Nov 25, 2024
1 parent 819eb08 commit 2d8817f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Tags: Discounts, Products, Watch

Use this task to generate discounts for future purchases. When a customer buys a "voucher" product, they will be emailed a single-use discount code for a fixed amount of money off a purchase on a corresponding "entitled" product".
Use this task to generate discounts for future purchases. When a customer buys a "voucher" product, they will be emailed a single-use discount code for either a fixed amount of money or percentage off a purchase on a corresponding "entitled" product".

* View in the task library: [tasks.mechanic.dev/generate-a-product-discount-when-a-voucher-product-is-purchased](https://tasks.mechanic.dev/generate-a-product-discount-when-a-voucher-product-is-purchased)
* Task JSON, for direct import: [task.json](../../tasks/generate-a-product-discount-when-a-voucher-product-is-purchased.json)
Expand Down Expand Up @@ -33,15 +33,16 @@ mechanic/actions/perform

## Documentation

Use this task to generate discounts for future purchases. When a customer buys a "voucher" product, they will be emailed a single-use discount code for a fixed amount of money off a purchase on a corresponding "entitled" product".
Use this task to generate discounts for future purchases. When a customer buys a "voucher" product, they will be emailed a single-use discount code for either a fixed amount of money or percentage off a purchase on a corresponding "entitled" product".

If a customer purchases more than one voucher product, they will receive more than one email, each containing a unique discount code.

### Options

- **Voucher product IDs and entitled product IDs:** Enter the IDs of the voucher products you're selling on the left, and the IDs of products you want to be discounted on the right. ([Learn how to find the product IDs.](https://learn.mechanic.dev/techniques/finding-a-resource-id))
- **Discount code prefix:** A small piece of text to add to the beginning of the generated discount code.
- **Discount value:** The money value to be subtracted.
- **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.
- **Email subject, body:** The content to email to the customer. May use the following variables:
- CUSTOMER_FIRST_NAME
- DISCOUNT_CODE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{% assign ve_product_ids_keyval = options.voucher_product_ids_and_entitled_product_ids__keyval_number_required %}
{% assign discount_code_prefix = options.discount_code_prefix__required %}
{% assign discount_value = options.discount_value__number_required %}
{% assign discount_fixed_amount = options.discount_fixed_amount__number %}
{% assign discount_percentage = options.discount_percentage__number %}
{% 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 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.topic == "shopify/orders/paid" %}
{% if event.preview %}
{% capture order_json %}
Expand Down Expand Up @@ -89,7 +97,7 @@
{% continue %}
{% endif %}

{% for line_item_n in (1..line_item.quantity) %}
{% 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 %}

{% capture mutation %}
Expand All @@ -111,10 +119,14 @@
}
}
value: {
discountAmount: {
amount: {{ discount_fixed_amount | abs | times: 1.0 | json }}
appliesOnEachItem: false
}
{% if discount_percentage != blank %}
percentage: {{ discount_percentage | abs | divided_by: 100.0 | json }}
{% else %}
discountAmount: {
amount: {{ discount_fixed_amount | abs | times: 1.0 | json }}
appliesOnEachItem: false
}
{% endif %}
}
}
usageLimit: 1
Expand Down
Loading

0 comments on commit 2d8817f

Please sign in to comment.