How can I duplicate a navigation menu for seasonal changes?

for duplicate menu python code:

import requests
import json
import time

API_ACCESS_TOKEN = “your_access_token_here” # ← Replace with your real access token

STORE_DOMAIN = “yourstore.myshopify.com” # ← Replace with your actual store domain (no https)

GRAPHQL_URL = f"https://{STORE_DOMAIN}/admin/api/2023-04/graphql.json"

RAW_MENU_ID = “197670600890” # ← Replace with the handle of the menu you want to copy example: content/menus/197670600890

MENU_GID = f"gid://shopify/Menu/{RAW_MENU_ID}"

HEADERS = {
“Content-Type”: “application/json”,
“X-Shopify-Access-Token”: API_ACCESS_TOKEN,
}

def graphql_request(query, variables=None):
payload = {“query”: query, “variables”: variables or {}}
response = requests.post(GRAPHQL_URL, headers=HEADERS, json=payload)
if response.status_code == 200:
return response.json()
else:
print(“GraphQL error:”, response.status_code)
print(response.text)
return None

def get_menu_by_id(menu_gid):
query = “”"
query getMenu($id: ID!) {
menu(id: $id) {
id
title
handle
items {
title
type
url
resourceId
items {
title
type
url
resourceId
items {
title
type
url
resourceId
}
}
}
}
}
“”"
result = graphql_request(query, {“id”: menu_gid})
return result.get(“data”, {}).get(“menu”)

def transform_items(items):
transformed =
for item in items:
new_item = {
“title”: item[“title”],
“type”: item[“type”],
“url”: item[“url”]
}

Include resourceId if present

if “resourceId” in item and item[“resourceId”]:
new_item[“resourceId”] = item[“resourceId”]

Recursively transform child items

if item.get(“items”):
new_item[“items”] = transform_items(item[“items”])
transformed.append(new_item)
return transformed

def create_menu_with_items(title, handle, items :disappointed_face:
mutation = “”"
mutation CreateMenu($title: String!, $handle: String!, $items: [MenuItemCreateInput!]!) {
menuCreate(title: $title, handle: $handle, items: $items) {
menu {
id
title
handle
}
userErrors {
field
message
}
}
}
“”"
variables = {“title”: title, “handle”: handle, “items”: items}
result = graphql_request(mutation, variables)

print(" Shopify response from menuCreate:")
print(json.dumps(result, indent=2))

if not result or “data” not in result:
return None

errors = result[“data”][“menuCreate”][“userErrors”]
if errors:
print(" Shopify returned errors:“)
for e in errors:
print(f”- {e[‘field’]}: {e[‘message’]}")
return None

return result[“data”][“menuCreate”][“menu”]

def main():
print(“Starting menu duplication process…”)

1. Get the original menu

menu = get_menu_by_id(MENU_GID)
if not menu:
print(f"Error: Menu with ID {RAW_MENU_ID} not found.")
return

2. Create new title and handle with timestamp to avoid duplicates

timestamp = int(time.time())
new_title = f"{menu[‘title’]} - Copy"
new_handle = f"{menu[‘handle’]}-copy-{timestamp}"

3. Convert menu items to correct format

items_input = transform_items(menu.get(“items”, ))

print(“Sending the following items:”)
print(json.dumps(items_input, indent=2))

4. Create the new duplicated menu

new_menu = create_menu_with_items(new_title, new_handle, items_input)

5. Report result

if new_menu:
print(f"Duplication successful! New menu: {new_menu[‘title’]} (handle: {new_menu[‘handle’]})")
else:
print(“Duplication failed.”)

if name == “main”:
main()
input(“Press Enter to exit…”)