Using the Shopify Rest API and Google Apps Script environment to update product prices using the following code,
// Function to update product price
function updateProductPrice(shopUrl, product, productData, headers, skipSkusPriceUpdate) {
const variant = product.variants[0];
const sku = variant.sku.split('|').pop().trim();
const newPrice = productData.price;
const compareAtPrice = productData.compare_at_price;
if (skipSkusPriceUpdate.includes(sku)) {
Logger.log(`Skipping price update for SKU: ${sku}`);
return;
}
const payload = {
variant: {
id: variant.id,
price: newPrice.toString(),
compare_at_price: newPrice < parseFloat(variant.price) ? variant.price.toString() : null
}
};
Logger.log(`Payload for updating product ID ${product.id} (SKU: ${sku}): ${JSON.stringify(payload)}`);
const endpoint = `${shopUrl}/variants/${variant.id}.json`;
const response = makeRequestWithRetry('PUT', endpoint, headers, payload);
if (response.getResponseCode() === 200) {
Logger.log(`Successfully updated price for product ID ${product.id} (SKU: ${sku}) to ${newPrice}. Previous price: ${variant.price}. Response: ${response.getContentText()}`);
} else {
Logger.log(`Failed to update price for product ID ${product.id} (SKU: ${sku}). Response Code: ${response.getResponseCode()}. Response: ${response.getContentText()}`);
}
}
Logger gives,
11:47:07 AM Info Payload for updating product ID 8778844733779 (SKU: 119781): {"variant":{"id":47834394362195,"price":"189","compare_at_price":"1089.00"}}
11:47:08 AM Info Successfully updated price for product ID 8778844733779 (SKU: 119781) to 189. Previous price: 1089.00. Response: {"variant":{"id":47834394362195,"product_id":8778844733779,"title":"Default Title","price":"1089.00","sku":"Activeshop | 119781","position":1,"inventory_policy":"deny","compare_at_price":null,"fulfillment_service":"manual","inventory_management":"shopify","option1":"Default Title","option2":null,"option3":null,"created_at":"2024-03-18T14:50:38+01:00","updated_at":"2024-07-15T11:16:31+02:00","taxable":true,"barcode":"5906717410086","grams":4600,"weight":4.6,"weight_unit":"kg","inventory_item_id":49933579223379,"inventory_quantity":372,"old_inventory_quantity":372,"requires_shipping":true,"admin_graphql_api_id":"gid://shopify/ProductVariant/47834394362195","image_id":null}}
So there’s a 200 response, even though the price has NOT been adjusted in Shopify from 1089 to 189.
What am I doing wrong here?
Have added more logging, searched for solutions on Shopify forum and found this; https://community.shopify.com/c/graphql-basics-and/put-200-but-price-not-updated/m-p/1853523 but thats for python.