Hello, I am using the REST API to get orders for one day using created_at_min and created_at_max.
Shopify shows 554 orders for 07-23-2023 but the script is returning 0 orders. Am I using the created_at_min and created_at_max wrong in the script?
#!/usr/bin/env python3
import os
import requests
from dotenv import load_dotenv
load_dotenv()
token = os.getenv('TOKEN')
shop = os.getenv('SHOP')
api_version = '2023-01'
shop_url = f"https://{shop}/admin/api/{api_version}"
def print_order_details(order):
print(f"Order Id: {order['id']}")
print(f"Day: {order['created_at']}")
print(f"Financial Status: {order['financial_status']}")
print(f"Gross Sales: {order['total_line_items_price']}")
print(f"Discounts: {order['total_discounts']}")
print(f"Taxes: {order['total_tax']}")
# Check if the order has refunds
refunds_total = get_total_refunds(order)
if refunds_total > 0:
print(f"Refunds: {refunds_total}")
else:
print("Refunds: 0.0")
# Get shipping amount
shipping_amount = get_shipping_amount(order)
duties = get_duties_amount(order)
print(f"Shipping Amount: {shipping_amount}")
print(f"Duties: {duties}")
# Calculate net sales
gross_sales = float(order['total_line_items_price'])
discounts = float(order['total_discounts'])
net_sales = gross_sales - discounts - refunds_total
net_sales_formatted = "{:.2f}".format(net_sales)
print(f"Net Sales: {net_sales_formatted}")
# Print line items for the order
print("Line Items:")
for line_item in order['line_items']:
print(f" - Product: {line_item['title']}")
print(f" SKU: {line_item['sku']}")
print(f" Quantity: {line_item['quantity']}")
print(f" Price: {line_item['price']}")
print("-" * 30)
def get_total_refunds(order):
refunds_total = 0.0
if 'refunds' in order:
for refund in order['refunds']:
for refund_line_item in refund['refund_line_items']:
price = float(refund_line_item['line_item']['price'])
quantity = int(refund_line_item['quantity'])
refunds_total += price * quantity
return refunds_total
def get_shipping_amount(order):
shipping_amount = 0.0
if 'total_shipping_price_set' in order:
shipping_amount = float(order['total_shipping_price_set']['shop_money']['amount'])
return shipping_amount
def get_duties_amount(order):
duties_amount = 0.0
if 'original_total_duties_set' in order and order['original_total_duties_set']:
duties_amount = float(order['original_total_duties_set']['shop_money']['amount'])
return duties_amount
def get_data(object_name):
endpoint = f"{shop_url}/{object_name}.json"
headers = {
"X-Shopify-Access-Token": token,
}
params = {
"created_at_min": "2023-07-23T00:00:00Z",
"created_at_max": "2023-07-23T23:59:59Z",
"limit": 250
}
response = requests.get(endpoint, headers=headers, params=params)
response_data = response.json().get(object_name, [])
all_data = response_data
while response.links.get('next'):
next_url = response.links['next']['url']
response = requests.get(next_url, headers=headers)
response_data = response.json().get(object_name, [])
all_data.extend(response_data)
return all_data
def main():
orders = get_data('orders')
order_count = len(orders)
print(f"Total Orders: {order_count}")
for order in orders:
print_order_details(order)
if __name__ == "__main__":
main()