Skip to content

Taxonomy updates #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions app/services/shopify_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
'X-Shopify-Access-Token': ACCESS_TOKEN
}

# GraphQL query to fetch products with pagination


def fetch_products(cursor=None):
query = '''
Expand All @@ -25,6 +23,7 @@ def fetch_products(cursor=None):
node {
id
title
productType
productCategory {
productTaxonomyNode {
id
Expand All @@ -45,8 +44,6 @@ def fetch_products(cursor=None):
response.raise_for_status()
return response.json()['data']['products']

# GraphQL mutation to update product category


def update_product_category(product_id, taxonomy_node_id):
mutation = '''
Expand Down Expand Up @@ -78,31 +75,38 @@ def update_product_category(product_id, taxonomy_node_id):
return response.json()


def update_uncategorized_products(new_taxonomy_node_id):
def update_uncategorized_products():
cursor = None
tshirt_taxonomy_node_id = 'gid://shopify/ProductTaxonomyNode/9532'
while True:
products_data = fetch_products(cursor)
for edge in products_data['edges']:
product = edge['node']
product_id = product['id']
title = product['title']
product_type = product['productType']
current_category = product['productCategory']
current_taxonomy_node_id = current_category['productTaxonomyNode'][
'id'] if current_category and current_category['productTaxonomyNode'] else None

# print(f"Product: {title}")
# print(f"Current Taxonomy Node ID: {current_taxonomy_node_id}")

print(f"Product: {title}, Type: {product_type}")
# Check if the product has no taxonomy node ID
if not current_taxonomy_node_id:
print(f"Updating product '{title}' to taxonomy node ID '{
new_taxonomy_node_id}'")
update_response = update_product_category(
product_id, new_taxonomy_node_id)
if 'errors' in update_response:
print(f"Error updating product '{title}': {
update_response['errors']}")
if product_type == "T-Shirt":
print(
f"Assigning taxonomy node ID '{tshirt_taxonomy_node_id}' to product '{
title}' (Type: {product_type})"
)
update_response = update_product_category(
product_id, tshirt_taxonomy_node_id)
if 'errors' in update_response:
print(
f"Error updating product '{title}': {update_response['errors']}")
else:
print(f"Product '{title}' updated successfully.")
else:
print(f"Product '{title}' updated successfully.")
print(
f"Product '{title}' (Type: {product_type}) has no taxonomy node and does not match 'T-Shirt'. Skipping.")

# Respect Shopify's API rate limits
time.sleep(0.5)
Expand All @@ -115,6 +119,5 @@ def update_uncategorized_products(new_taxonomy_node_id):
def set_taxonomy_nodeID():
"""This method retrieves uncategorized products from the Shopify store and sets their taxonomy node ID to T-Shirts."""
# Replace this with the actual taxonomy node ID for T-Shirts
NEW_TAXONOMY_NODE_ID = 'gid://shopify/ProductTaxonomyNode/9532'
update_uncategorized_products(NEW_TAXONOMY_NODE_ID)
update_uncategorized_products()
return "ok"
Loading