Update PIM product
In this example, we will update the products from the PIM (using pagination).
import requests
import json
def get_all_products_from_pim():
url_update = 'https://pim-client.wizart.ai/api/customer/articles?page=1'
headers = {
'Accept': 'application/json',
'Authorization': access_token
}
get_uploaded_products = requests.get(url_update, headers=headers, timeout=None)
uploaded_products = json.loads(get_uploaded_products.text)
total_num_page = uploaded_products['meta']['last_page'] #get all pages
for page_num in range(1, total_num_page + 1):
try:
url_get_exist_product = f'https://pim-client.wizart.ai/api/customer/articles?page={page_num}&locale=en'
get_exist_product = requests.get(url_get_exist_product, headers=headers, timeout=None)
all_uploaded_products = json.loads(get_exist_product.text)
info_uploaded_products = all_uploaded_products['data']
page_num = page_num + 1
time.sleep(2)
except:
pass
return info_uploaded_products
all_products_pim = get_all_products_from_pim()
def update_product():
for products_pim in all_products_pim:
product_uuid = products_pim['uuid']
collection_uuid = products_pim['collection']['uuid']
update_url = f'https://pim-client.wizart.ai/api/v2/collection/{collection_uuid}/article/{product_uuid}'
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': access_token
}
#here, you can have your loop that iterates through your update list.
payload = {
"description": update_description,
"link": [update_link],
"locale": "en",
"availability": update_availability,
"name": update_name
}
response = requests.patch(update_url, headers=headers, json=payload)
Imports the necessary libraries (
requests
andjson
).Defines a function named
get_all_products_from_pim
that retrieves all products from the PIM system. It sends a GET request to the specified URL (https://pim-client.wizart.ai/api/customer/articles?page=1
) with the appropriate headers, and then iterates through all the pages of products using a loop. The retrieved product information is stored in theinfo_uploaded_products
variable.Calls the
get_all_products_from_pim
function and stores the returned products in theall_products_pim
variable.Defines a function named
update_product
that updates each product in the PIM system. It iterates through theall_products_pim
list, extracts the necessary information (such as product UUID and collection UUID), and constructs the update URL using the extracted information. The function then sends a PATCH request to the update URL with the appropriate headers and payload, which contains the updated product information.The response from the PATCH request is not handled in the provided code.