All things Shopify and commerce
I'm trying to update a rich text metafield through a Python program.
But it gives me an error: errors":{"value":["is invalid JSON: unexpected token at 'Hello World!'."]}}
Moreover, this error is issued only on the metafield field of the rich text type; if you use any other type, it works!
below is the program code
import requests
shopify_url = 'xxxxxx'
access_token = ‘xxxxxxxx’
def get_shopify_products():
url = f'{shopify_url}/products.json'
headers = {
'X-Shopify-Access-Token': access_token
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()['products']
else:
print("Error fetching Shopify products:", response.status_code)
return []
def update_shopify_product_metafield(shopify_product_id, text_value):
url = f'{shopify_url}/products/{shopify_product_id}/metafields.json'
headers = {
'X-Shopify-Access-Token': access_token
}
metafield_data = {
"metafield": {
"namespace": "custom",
"key": "short_description_2",
"value": text_value,
"type": "rich_text_field"
}
}
response = requests.post(url, json=metafield_data, headers=headers)
if response.status_code == 201:
print(f"Successfully updated short description for product ID {shopify_product_id}")
else:
print(f"Failed to update product ID {shopify_product_id}: {response.status_code}, Response: {response.text}")
def main():
products = get_shopify_products()
for product in products[:1]:
shopify_product_id = product['id']
product_name = product['title']
print(f"Processing product '{product_name}' with ID {shopify_product_id}")
text_value=‘Hello World!’
update_shopify_product_metafield(shopify_product_id, text_value)
if __name__ == "__main__":
main()
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024