-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
432 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
261 changes: 145 additions & 116 deletions
261
docs/generate-a-discount-code-when-a-certain-product-is-purchased/script.liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
Oops, something went wrong.