我想要得到:在woocommerce上订购的客户的链接,数量和产品名称。我已经尝试了许多新手级别的方法(对不起,我刚刚自己学过python),问题是一张发票中有多个订单。这是示例顺序,即通过woocommerce API发送回的json代码:
{'id': 123456, 'parent_id': 0, 'number': '123456', 'order_key': 'wc_order_abcd', 'created_via': 'checkout', 'version': '3.6.4', 'status': 'processing', 'currency': 'USD', 'date_created': '2021-02-15T00:43:05', 'date_created_gmt': '2021-02-15T00:43:05', 'date_modified': '2021-02-15T00:43:33', 'date_modified_gmt': '2021-02-15T00:43:33', 'discount_total': '0.00', 'discount_tax': '0.00', 'shipping_total': '0.00', 'shipping_tax': '0.00', 'cart_tax': '0.00', 'total': '1.84', 'total_tax': '0.00', 'prices_include_tax': False, 'customer_id': 0, 'customer_ip_address': '1.2.3.4', 'customer_user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36', 'customer_note': '', 'billing': {'first_name': '', 'last_name': '', 'company': '', 'address_1': '', 'address_2': '', 'city': '', 'state': '', 'postcode': '', 'country': '', 'email': 'abcd@gmail.com', 'phone': ''}, 'shipping': {'first_name': '', 'last_name': '', 'company': '', 'address_1': '', 'address_2': '', 'city': '', 'state': '', 'postcode': '', 'country': ''}, 'payment_method': 'paypal', 'payment_method_title': 'PayPal', 'transaction_id': 'ABCD', 'date_paid': '2021-02-15T00:43:33', 'date_paid_gmt': '2021-02-15T00:43:33', 'date_completed': None, 'date_completed_gmt': None, 'cart_hash': 'abcd', 'meta_data': [{'id': 1054882, 'key': 'is_vat_exempt', 'value': 'no'}, {'id': 1054884, 'key': 'Payment type', 'value': 'instant'}, {'id': 1054886, 'key': '_paypal_status', 'value': 'completed'}, {'id': 1054887, 'key': 'PayPal Transaction Fee', 'value': '0.38'}], 'line_items': [{'id': 71301, 'name': 'Product A', 'product_id': 14033, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '0.36', 'subtotal_tax': '0.00', 'total': '0.36', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 100000, 'key': '', 'value': 'Image1 $0.00 × 300'}, {'id': 100001, 'key': '', 'value': 'Worldwide ( FREE )'}, {'id': 100002, 'key': 'Speed of delivery', 'value': 'As fast as possible'}, {'id': 100003, 'key': '', 'value': 'https://www. example.com'}], 'sku': '', 'price': 0.36}, {'id': 71302, 'name': 'Product B', 'product_id': 13973, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '0.04', 'subtotal_tax': '0.00', 'total': '0.04', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 100000, 'key': '', 'value': 'Image2 $0.00 × 10'}, {'id': 100001, 'key': '', 'value': 'https://www. example.com'}], 'sku': '', 'price': 0.04}, {'id': 71303, 'name': 'Product A', 'product_id': 14033, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '0.42', 'subtotal_tax': '0.00', 'total': '0.42', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 100000, 'key': '', 'value': 'Image1 $0.00 × 350'}, {'id': 100001, 'key': '', 'value': 'Worldwide ( FREE )'}, {'id': 100002, 'key': 'Speed of delivery', 'value': 'As fast as possible'}, {'id': 100003, 'key': '', 'value': 'https://www. example.com'}], 'sku': '', 'price': 0.42}, {'id': 71304, 'name': 'Product C', 'product_id': 13980, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '0.40', 'subtotal_tax': '0.00', 'total': '0.40', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 100000, 'key': '', 'value': 'Image3 $0.00 × 200'}, {'id': 100001, 'key': 'Speed of delivery', 'value': 'As fast as possible'}, {'id': 100002, 'key': '', 'value': 'https://www. example.com'}], 'sku': '', 'price': 0.4}, {'id': 71305, 'name': 'Product A', 'product_id': 14033, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '0.32', 'subtotal_tax': '0.00', 'total': '0.32', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 100000, 'key': '', 'value': 'Image4 $0.00 × 270'}, {'id': 100001, 'key': '', 'value': 'Worldwide ( FREE )'}, {'id': 100002, 'key': '', 'value': 'https://www. example.com'}], 'sku': '', 'price': 0.324}], 'tax_lines': [], 'shipping_lines': [], 'fee_lines': [{'id': 71306, 'name': 'PayPal Fee', 'tax_class': '0', 'tax_status': 'taxable', 'amount': '0.3', 'total': '0.30', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 791979, 'key': '_legacy_fee_key', 'value': 'paypal-fee'}]}], 'coupon_lines': [], 'refunds': [], '_links': {'self': [{'href': 'https://example.com/wp-json/wc/v3/orders/123456'}], 'collection': [{'href': 'https://example.com/wp-json/wc/v3/orders'}]}}
这是我的代码:
ordernumber = input("Order Number: ")
order = "orders/"
wcapi = API(
url="https://example.com",
consumer_key="mykey",
consumer_secret="mysecret",
version="wc/v3"
)
m = (wcapi.get(order + ordernumber).json())
orderno1 = json.dumps(m) # convert json to text
if "Product A" in orderno1:
print("Found Product A ...")
link1 = re.search("(?Phttps?://[^\s]+)", orderno1).group("url") # get link
link1a = re.sub('>|"|}|]|,', '', link1) # replace >|"|}|]| to space
print('Link : ', link1a)
if "u00d7" in orderno1: # find quantity
quantity = orderno1[orderno1.find("u00d7")+6:].split()[0] # Get number from u00d7
quantitya = re.sub('>|"|}|]|,', '', quantity) # replace >|"|}|]| to space
#do something
if "Product B" in orderno1:
print("Found Product B ...")
link1 = re.search("(?Phttps?://[^\s]+)", orderno1).group("url") # get link
link1a = re.sub('>|"|}|]|,', '', link1) # replace >|"|}|]| to space
print('Link : ', link1a)
if "u00d7" in orderno1: # find quantity
quantity = orderno1[orderno1.find("u00d7")+6:].split()[0] # Get number from u00d7
quantitya = re.sub('>|"|}|]|,', '', quantity) # replace >|"|}|]| to space
#do something
if "Product C" in orderno1:
print("Found Product C ...")
link1 = re.search("(?Phttps?://[^\s]+)", orderno1).group("url") # get link
link1a = re.sub('>|"|}|]|,', '', link1) # replace >|"|}|]| to space
print('Link : ', link1a)
if "u00d7" in orderno1: # find quantity
quantity = orderno1[orderno1.find("u00d7")+6:].split()[0] # Get number from u00d7
quantitya = re.sub('>|"|}|]|,', '', quantity) # replace >|"|}|]| to space
#do something
如何获得链接,数量,产品名称总是在变化。
谢谢!!
解决方案
暂无回答