Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hey all,
Trying to update an image with productUpdateMedia.
This is the mutation I have written:
mutation productUpdateMedia($media: [UpdateMediaInput!]!, $productId: ID!) {
productUpdateMedia(media: $media, productId: $productId) {
mediaUserErrors {
field
message
}
product {
id
}
}
}
and passing the variables:
{
"media": {
"alt": "1123123",
"id": "gid://shopify/ImageSource/24369728061606"
},
"productId": "gid://shopify/Product/7036787359910"
}
But getting an error saying invalid id.
So, I changed the id from ImageSource to ProductImage
{
"media": {
"alt": "1123123",
"id": "gid://shopify/ProductImage/31986317394086"
},
"productId": "gid://shopify/Product/7036787359910"
}
_
But still getting the same error.
I want to use productUpdateMedia instead of productImageUpdate -- as productUpdateMedia can be used for updating media in bulk, and hence is faster.
Any idea what is going wrong with the id?
Thank you!
Arjun
Solved! Go to the solution
This is an accepted solution.
Figured this out. I need to pass MediaImage as ID:
"id": "gid://shopify/MediaImage/24365378732198"
-
It works!
Just double checked and the valued of the IDs are correct.
As in "gid://shopify/ImageSource/24369728061606" and "gid://shopify/ProductImage/31986317394086"
are for the same image - and belong the product : "gid://shopify/Product/7036787359910"
Appreciate any help regarding this.
Thank you!
Arjun
This is an accepted solution.
Figured this out. I need to pass MediaImage as ID:
"id": "gid://shopify/MediaImage/24365378732198"
-
It works!
That MediaImage ID is different than the ProductImage ID?
Yes, it's different. Which is annoying because I can't figure out how to get the MediaImage ID from a ProductImage ID
I solved it like this:
def update_shopify_ids_in_db(db, products_to_update):
# Get Shopify API credentials
shop_url = os.getenv('SHOPIFY_STORE_URL')
api_secret = os.getenv('SHOPIFY_API_SECRET')
"""
Updates the order_products_master table in the database with Shopify product IDs and Media IDs.
"""
# Create a Shopify session
create_shopify_session()
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": api_secret
}
# Shopify GraphQL endpoint
graphql_url = f'https://{shop_url}/admin/api/2022-01/graphql.json'
for product in products_to_update:
handle = product['product.handle']
logger.info(f"Processing product with handle: {handle}")
# Find the Shopify product ID using the handle
shopify_product = shopify.Product.find_first(handle=handle)
if shopify_product:
shopify_id = shopify_product.id
logger.info(f"Found Shopify ID {shopify_id} for handle: {handle}")
# Set up the GraphQL query
graphql_query = {
"query": """
query($productId: ID!) {
product(id: $productId) {
id
media(first: 5) {
edges {
node {
... on MediaImage {
id
alt
}
}
}
}
}
}
""",
"variables": {
"productId": f"gid://shopify/Product/{shopify_id}"
}
}
# Make the GraphQL API request
response = requests.post(graphql_url, headers=headers, json=graphql_query)
if response.status_code == 200:
media_data = response.json().get('data', {}).get('product', {}).get('media', {}).get('edges', [])
media_ids = [edge['node']['id'] for edge in media_data if 'node' in edge]
logger.info(f"Found media IDs {media_ids} for handle: {handle}")
else:
logger.error(f"GraphQL query failed for handle: {handle} with status code {response.status_code}")
# Update the individual product in the database with its Shopify ID and Media IDs
update_query = "UPDATE order_products_master SET `product.shopify.id` = %s, `product_media_id` = %s WHERE `Row ID` = %s"
with db.cursor() as cursor:
cursor.execute(update_query, (shopify_id, ','.join(map(str, media_ids)), product['Row ID']))
logger.info(f"Updated Shopify ID and Media IDs in database for product with Row ID: {product['Row ID']}")
else:
logger.error(f"Shopify product not found for handle: {handle}")
# Introduce a delay between requests
time.sleep(2)
db.commit()
# Update all matching products with the same handle
propagate_id_query = """
UPDATE order_products_master dest,
(SELECT `product.handle`, `product.shopify.id`, `product_media_id` FROM order_products_master WHERE `product.shopify.id` IS NOT NULL) src
SET dest.`product.shopify.id` = src.`product.shopify.id`, dest.`product_media_id` = src.`product_media_id`
WHERE dest.`product.handle` = src.`product.handle` AND dest.`product.shopify.id` IS NULL
"""
with db.cursor() as cursor:
cursor.execute(propagate_id_query)
db.commit()
Spagetti code for the WIN 😜
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025