created function for creating shopify discount codes
parent
604fffdd2d
commit
fb16516ddb
|
@ -45,7 +45,7 @@ orderId = orderId["value"]
|
||||||
if orderId.startswith("#"):
|
if orderId.startswith("#"):
|
||||||
orderId = orderId[1:]
|
orderId = orderId[1:]
|
||||||
|
|
||||||
discountCode = None
|
generated_discount_code = None
|
||||||
|
|
||||||
|
|
||||||
def CreateCrmCustomer():
|
def CreateCrmCustomer():
|
||||||
|
@ -117,49 +117,6 @@ def CheckIfCrmCustomerExists():
|
||||||
GetCustomerActivityLinks(customerId=customerId, thirdVoucher=thirdVoucher)
|
GetCustomerActivityLinks(customerId=customerId, thirdVoucher=thirdVoucher)
|
||||||
|
|
||||||
|
|
||||||
# create shopify discount code
|
|
||||||
# this function will retry 3 times to create a new discount code if the random generate code already exists
|
|
||||||
def createShopifyDiscountCode(retries):
|
|
||||||
# generate random code
|
|
||||||
characters = string.ascii_uppercase + string.digits
|
|
||||||
discount_code = "".join(random.choice(characters) for _ in range(12))
|
|
||||||
|
|
||||||
# load data from secrets
|
|
||||||
shopify_secrets = secrets["shopify"]
|
|
||||||
|
|
||||||
shopify_url = shopify_secrets["store_url"]
|
|
||||||
x_shopify_app_access_token = shopify_secrets["x_shopify_app_access_token"]
|
|
||||||
shopify_discount_code_price_rule = shopify_secrets["discount_price_rules"]["order_voucher_10_percent"]
|
|
||||||
|
|
||||||
response = requests.post(
|
|
||||||
url=f"{shopify_url}/admin/api/2024-01/price_rules/{shopify_discount_code_price_rule}/discount_codes.json",
|
|
||||||
headers={
|
|
||||||
"X-Shopify-Access-Token": x_shopify_app_access_token,
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
json={
|
|
||||||
"discount_code": {"code": discount_code}
|
|
||||||
})
|
|
||||||
|
|
||||||
retries += 1
|
|
||||||
|
|
||||||
if retries >= 3:
|
|
||||||
sys.exit("Error, tried 3 times to create discount code")
|
|
||||||
|
|
||||||
if response.status_code == 422:
|
|
||||||
print("Failed to create code, response status code 422. Trying to generate new discount code")
|
|
||||||
createShopifyDiscountCode(retries)
|
|
||||||
|
|
||||||
if response.status_code != 201:
|
|
||||||
sys.exit(f"Failed to create discount code, status code: {response.status_code}")
|
|
||||||
|
|
||||||
print(f"Created discount code: {discount_code}")
|
|
||||||
|
|
||||||
global discountCode
|
|
||||||
|
|
||||||
discountCode = discount_code
|
|
||||||
|
|
||||||
|
|
||||||
def CreateCrmActivityLink(customerId, thirdVoucher):
|
def CreateCrmActivityLink(customerId, thirdVoucher):
|
||||||
print(f"Creating CRM activity link for customer {customerId}")
|
print(f"Creating CRM activity link for customer {customerId}")
|
||||||
|
|
||||||
|
@ -179,9 +136,11 @@ def CreateCrmActivityLink(customerId, thirdVoucher):
|
||||||
print(f"CreateCrmActivityLink req error {response.status_code}")
|
print(f"CreateCrmActivityLink req error {response.status_code}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
createShopifyDiscountCode(0)
|
global generated_discount_code
|
||||||
|
|
||||||
req("10 % Gutschein", f"{secrets['shopify']['public_url']}/discount/{discountCode}?utm_source=order&utm_medium=qrcode&utm_campaign=ordervouchercodes&utm_content=reedemvouchercode")
|
generated_discount_code = utils.create_shopify_discount_code("order_voucher_10_percent")
|
||||||
|
|
||||||
|
req("10 % Gutschein", f"{secrets['shopify']['public_url']}/discount/{generated_discount_code}?utm_source=order&utm_medium=qrcode&utm_campaign=ordervouchercodes&utm_content=reedemvouchercode")
|
||||||
req("5 € Gutschein", f"{productUrl}?utm_source=order&utm_medium=qrcode&utm_campaign=ordervouchercodes&utm_content=qualifyforvouchercode")
|
req("5 € Gutschein", f"{productUrl}?utm_source=order&utm_medium=qrcode&utm_campaign=ordervouchercodes&utm_content=qualifyforvouchercode")
|
||||||
|
|
||||||
if thirdVoucher:
|
if thirdVoucher:
|
||||||
|
@ -276,7 +235,7 @@ def ReplaceHtmlVariables():
|
||||||
|
|
||||||
file = open("backPage.html", "r")
|
file = open("backPage.html", "r")
|
||||||
html = file.read()
|
html = file.read()
|
||||||
html = html.replace("{{DISCOUNT_CODE}}", discountCode)
|
html = html.replace("{{DISCOUNT_CODE}}", generated_discount_code)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
file = open("backPage.html", "w")
|
file = open("backPage.html", "w")
|
||||||
|
|
Binary file not shown.
|
@ -5,7 +5,9 @@ import shutil
|
||||||
import sys
|
import sys
|
||||||
import qrcode
|
import qrcode
|
||||||
import json
|
import json
|
||||||
|
import string
|
||||||
|
import random
|
||||||
|
import requests
|
||||||
|
|
||||||
def clear_workspace(files):
|
def clear_workspace(files):
|
||||||
if not files or not isinstance(files, list) or len(files) == 0:
|
if not files or not isinstance(files, list) or len(files) == 0:
|
||||||
|
@ -148,3 +150,54 @@ def create_qrcode(qr_code_url, save_path, back_color):
|
||||||
def get_secrets():
|
def get_secrets():
|
||||||
with open('../../secrets/secrets.json') as content:
|
with open('../../secrets/secrets.json') as content:
|
||||||
return json.load(content)
|
return json.load(content)
|
||||||
|
|
||||||
|
|
||||||
|
# create shopify discount code
|
||||||
|
# this function will retry 3 times to create a new discount code if the random generate code already exists
|
||||||
|
def create_shopify_discount_code(shopify_discount_code_price_rule_name):
|
||||||
|
# load data from secrets
|
||||||
|
secrets = get_secrets()
|
||||||
|
shopify_secrets = secrets["shopify"]
|
||||||
|
shopify_url = shopify_secrets["store_url"]
|
||||||
|
x_shopify_app_access_token = shopify_secrets["x_shopify_app_access_token"]
|
||||||
|
shopify_discount_code_price_rule = shopify_secrets["discount_price_rules"][shopify_discount_code_price_rule_name]
|
||||||
|
|
||||||
|
characters = string.ascii_uppercase + string.digits
|
||||||
|
|
||||||
|
# _createShopifyDiscountCode(secrets, characters, shopify_discount_code_price_rule, 0)
|
||||||
|
|
||||||
|
generated_discount_code = None
|
||||||
|
retries = 0
|
||||||
|
|
||||||
|
while (generated_discount_code is None or retries >= 3):
|
||||||
|
# generate random code
|
||||||
|
discount_code = "".join(random.choice(characters) for _ in range(12))
|
||||||
|
|
||||||
|
# shopify_discount_code_price_rule = shopify_secrets["discount_price_rules"]["order_voucher_10_percent"]
|
||||||
|
|
||||||
|
response = requests.post(
|
||||||
|
url=f"{shopify_url}/admin/api/2024-01/price_rules/{shopify_discount_code_price_rule}/discount_codes.json",
|
||||||
|
headers={
|
||||||
|
"X-Shopify-Access-Token": x_shopify_app_access_token,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
json={
|
||||||
|
"discount_code": {"code": discount_code}
|
||||||
|
})
|
||||||
|
|
||||||
|
if response.status_code == 422:
|
||||||
|
print("Failed to create code, response status code 422. Trying to generate new discount code")
|
||||||
|
|
||||||
|
if response.status_code != 201:
|
||||||
|
sys.exit(f"Failed to create discount code, status code: {response.status_code}")
|
||||||
|
|
||||||
|
generated_discount_code = discount_code
|
||||||
|
|
||||||
|
retries += 1
|
||||||
|
|
||||||
|
if retries >= 3:
|
||||||
|
sys.exit("Error, tried 3 times to create discount code")
|
||||||
|
|
||||||
|
print(f"Created discount code: {generated_discount_code}. Retries: {retries}")
|
||||||
|
|
||||||
|
return generated_discount_code
|
||||||
|
|
Loading…
Reference in New Issue