[英]Shopify API: How do I send customer an email about product being shipped?
我正在嘗試通過更新訂單履行情況向客戶發送一封包含訂單跟蹤信息的電子郵件,但該電子郵件並未發送出去。 我可以檢索履行並添加跟蹤代碼,它會正確保存,但狀態不會更新。
fulfillment_id = 3252574519475
shopify_order_id = 3683332686003
fulfillment = shopify.Fulfillment.find(fulfillment_id,order_id=shopify_order_id)
fulfillment.tracking_numbers = [tracking_number]
fulfillment.tracking_company = tracking_company
fulfillment.notify_customer = "true"
fulfillment.status = "success"
fulfillment.shipment_status = "delivered"
result = fulfillment.save()
當我訪問https://dev-store.myshopify.com/admin/orders/3683332686003/fulfillments.json
我看到tracking_numbers
與tracking_company
和tracking_urls
一起更新,但狀態仍處於open
狀態:
{
"fulfillments": [{
"id": 3252573110451,
"order_id": 3683332686003,
"status": "success",
"created_at": "2021-03-22T14:36:55-04:00",
"service": "gift_card",
"updated_at": "2021-03-22T14:36:56-04:00",
"tracking_company": null,
"shipment_status": null,
"location_id": 61514940595,
"line_items": [{
"id": 9689309610163,
"variant_id": 39396209983667,
"title": "$100 ta E-Gift Card",
"quantity": 1,
"sku": null,
"variant_title": "$100.00 USD",
"vendor": "A",
"fulfillment_service": "gift_card",
"product_id": 6571217256627,
"requires_shipping": false,
"taxable": false,
"gift_card": true,
"name": "$100 ta E-Gift Card - $100.00 USD",
"variant_inventory_management": null,
"properties": [],
"product_exists": true,
"fulfillable_quantity": 0,
"grams": 0,
"price": "95.00",
"total_discount": "0.00",
"fulfillment_status": "fulfilled",
"price_set": {
"shop_money": {
"amount": "95.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "95.00",
"currency_code": "USD"
}
},
"total_discount_set": {
"shop_money": {
"amount": "0.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "0.00",
"currency_code": "USD"
}
},
"discount_allocations": [],
"tax_lines": []
}],
"tracking_number": null,
"tracking_numbers": [],
"tracking_url": null,
"tracking_urls": [],
"receipt": {
"gift_cards": [{
"id": 483988799667,
"line_item_id": 9689309610163,
"masked_code": "•••• •••• •••• 74e2"
}]
},
"name": "#1013.1"
}, {
"id": 3252574519475,
"order_id": 3683332686003,
"status": "open",
"created_at": "2021-03-22T14:37:23-04:00",
"service": "manual",
"updated_at": "2021-03-22T14:49:45-04:00",
"tracking_company": "UPS",
"shipment_status": null,
"location_id": 61514940595,
"line_items": [{
"id": 9689309642931,
"variant_id": 39396224663731,
"title": "The Pack",
"quantity": 1,
"sku": "FM-001",
"variant_title": null,
"vendor": "A",
"fulfillment_service": "manual",
"product_id": 6571220402355,
"requires_shipping": true,
"taxable": true,
"gift_card": false,
"name": "The Pack",
"variant_inventory_management": null,
"properties": [],
"product_exists": true,
"fulfillable_quantity": 0,
"grams": 3629,
"price": "111.99",
"total_discount": "0.00",
"fulfillment_status": null,
"price_set": {
"shop_money": {
"amount": "111.99",
"currency_code": "USD"
},
"presentment_money": {
"amount": "111.99",
"currency_code": "USD"
}
},
"total_discount_set": {
"shop_money": {
"amount": "0.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "0.00",
"currency_code": "USD"
}
},
"discount_allocations": [],
"tax_lines": []
}],
"tracking_number": "1Z6R96W802851935",
"tracking_numbers": ["1Z6R96W802851935"],
"tracking_url": "https:\/\/www.ups.com\/WebTracking?loc=en_US\u0026requester=ST\u0026trackNums=1Z6R96W80302851935",
"tracking_urls": ["https:\/\/www.ups.com\/WebTracking?loc=en_US\u0026requester=ST\u0026trackNums=1Z6R96W80302851935"],
"receipt": {},
"name": "#1013.2"
}]
}
我如何讓 shopify 向客戶發送帶有跟蹤號的電子郵件,並將履行狀態從open
狀態更新為success
以便將整個訂單設置為已履行? 我一直在商店上手動創建訂單,我不確定這是否與它有關。
我正在使用這個 Python 包https://github.com/Shopify/shopify_python_api
如果您在履行時將“notify_customer”設置為 true,這是第一件事。 然后,您應該創建一個所謂的 fullfillment 事件,並將“status”設置為“in_transit”。 因此,Shopify 將:
在 Shopify 的 API 文檔中,很好地描述了履行事件的創建: https ://shopify.dev/docs/admin-api/rest/reference/shipping-and-fulfillment/fulfillmentevent
在履行事件的幫助下,您還可以設置更多狀態值,例如“out_for_delivery”和“delivered”。
經過一番搜索,我找到了一個Shopify 社區帖子,這可能就是您要找的內容:
import shopify
API_KEY = 'app API key'
PASSWORD = 'app password'
SHOP_NAME = 'your shop name'
shop_url = "https://%s:%s@%s.myshopify.com/admin" % (API_KEY, PASSWORD, SHOP_NAME)
shopify.ShopifyResource.set_site(shop_url)
shop = shopify.Shop.current()
order = shopify.Order()
order.email = "foo@foo.com"
order.note = "a test note here"
order.fulfillment_status = "fulfilled"
order.send_receipt = True
order.send_fulfillment_receipt = False
order.line_items = [{"variant_id": 01234567890, "quantity": 1}]
success = order.save()
據我所知,shopify 與 smpt 沒有任何關系,因此需要另一個庫,在 python 上發送電子郵件很容易; jinja 是為了方便,你可以發送一個完整的 html 頁面。
from email.mime.text import MIMEText
from email.header import Header
import smtplib
import jinja2
import ssl
import os
class Shipment_Email:
mail_addr = 'smtp.mail.yahoo.com'
mail_port = 465
__email = str('your_name@yahoo.com')
__passw = str('your_api_password')
def send_to(self, to, sub, data):
content = self.template.render(data=data)
msg = MIMEText(content, 'html', 'utf-8')
msg['Subject'] = Header(sub, 'utf-8')
msg['From'] = self.__email
if not isinstance(to, str):
msg['To'] = ', '.join(to)
else:
msg['To'] = to
self.server.sendmail(self.__email, to, msg.as_string())
@staticmethod
def load_template(src):
if os.path.isfile(src):
with open(src, 'r') as file:
return jinja2.Template(file.read())
def __init__(self, debug=False):
ssl_context = ssl.create_default_context()
self.server = smtplib.SMTP_SSL(self.mail_addr, self.mail_port,
context=ssl_context)
self.server.login(self.__email, self.__passw)
self.server.set_debuglevel(debug)
self.template = self.load_template('./index.html')
def exit(self):
self.server.quit()
if __name__ == '__main__':
mail_hanlde = Shipment_Email(debug=True)
mail_hanlde.send_to('a_target@yahoo.com',
'Shopping list', {
'Name': 'Alex',
'Product': {
'Coffe': 8.50,
'Keyboard': 150
}
})
mail_hanlde.exit()
index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style> p { margin-left: 20px } </style>
</head>
<body>
<h4>{{ data['Name']}}'s shoping list:</h4>
{% for item in data['Product'] %}
<p>{{ item }} : {{data['Product'][item]}} eu</p>
{% endfor %}
</body>
</html>
為了在雅虎郵件上工作,您需要獲得自定義設備密碼,因此請勾選標記的選項並使用該密碼而不是帳戶,它們是不一樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.