woocommerce offtopic - How can I get a get a list of customers from a list of email addresses?

admin2025-01-07  5

I would like to get the order history for 25k customers. I have each of the customer's email address. My current pipeline is to loop through the email addresses, get the corresponding customer, then get the orders corresponding to that customer.

I'd like to speed this up through one or both of the following:

1) Getting the customers from emails in batches, rather than one at a time

2) Going directly from customer email to list of orders.

How can either of these be accomplished? Is there a better way to accomplish my goal that I am overlooking?

Edit: My current (working but slow) code:

import json
from woocommerce import API

with open("credentials/woocommerce_api_keys.json",'r') as f:
    woo_credentials = json.load(f)

wcapi = API(
    url="",
    consumer_key=woo_credentials["consumer_key"],
    consumer_secret=woo_credentials["consumer_secret"],
    version="wc/v3"
)


with open("customer_emails.csv",'r') as f:
    customer_emails = f.read().split(",")
customers = []
orders_by_customer = {}
order_dates_by_customer = {}
for email in customer_emails:
    customer = wcapi.get("customers/?email={}".format(email)).json()
    if len(customer) == 1:
        customer = customer[0]
        customers.append(customer)
        customer_id = customer["id"]
        orders = wcapi.get("orders/?customer={}".format(customer_id)).json()
        orders_by_customer[email] = orders
        order_dates = []
        for o in orders:
            order_dates.append(o["date_modified_gmt"])
        order_dates_by_customer[email] = order_dates
    else:
        print("email {} returns {} customers".format(email,len(customer)))

with open("data/customers.json",'w') as f:
    json.dump(customers,f,indent=4)
with open("data/orders_by_customer.json",'w') as f:
    json.dump(orders_by_customer,f,indent=4)
with open("data/order_dates_by_customer.json","w") as f:
    json.dump(order_dates_by_customer,f,indent=4)

I would like to get the order history for 25k customers. I have each of the customer's email address. My current pipeline is to loop through the email addresses, get the corresponding customer, then get the orders corresponding to that customer.

I'd like to speed this up through one or both of the following:

1) Getting the customers from emails in batches, rather than one at a time

2) Going directly from customer email to list of orders.

How can either of these be accomplished? Is there a better way to accomplish my goal that I am overlooking?

Edit: My current (working but slow) code:

import json
from woocommerce import API

with open("credentials/woocommerce_api_keys.json",'r') as f:
    woo_credentials = json.load(f)

wcapi = API(
    url="https://monq.com",
    consumer_key=woo_credentials["consumer_key"],
    consumer_secret=woo_credentials["consumer_secret"],
    version="wc/v3"
)


with open("customer_emails.csv",'r') as f:
    customer_emails = f.read().split(",")
customers = []
orders_by_customer = {}
order_dates_by_customer = {}
for email in customer_emails:
    customer = wcapi.get("customers/?email={}".format(email)).json()
    if len(customer) == 1:
        customer = customer[0]
        customers.append(customer)
        customer_id = customer["id"]
        orders = wcapi.get("orders/?customer={}".format(customer_id)).json()
        orders_by_customer[email] = orders
        order_dates = []
        for o in orders:
            order_dates.append(o["date_modified_gmt"])
        order_dates_by_customer[email] = order_dates
    else:
        print("email {} returns {} customers".format(email,len(customer)))

with open("data/customers.json",'w') as f:
    json.dump(customers,f,indent=4)
with open("data/orders_by_customer.json",'w') as f:
    json.dump(orders_by_customer,f,indent=4)
with open("data/order_dates_by_customer.json","w") as f:
    json.dump(order_dates_by_customer,f,indent=4)
Share Improve this question edited Oct 7, 2019 at 17:41 Michael Fishman asked Oct 3, 2019 at 13:54 Michael FishmanMichael Fishman 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You need to implement the loop for all the emails you have in your database. then for each you can use below code: $exists = email_exists($email); if ( $exists ) echo "That E-mail is registered to user number "; else echo "That E-mail doesn't belong to any registered users on this site";

If you are facing issue please share more details on your question

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265435a1045.html

最新回复(0)