A gold jewellery store that prices by weight has to change its product prices when gold moves. The rule is simple: price = grams × karat rate + making charge. A scheduled job fetches the rate from GET /v1/carat, recomputes each product and writes the new price to the shop. The Shopify and WooCommerce requests in the examples are printed and not sent, because a store and its IDs are needed to send them.
- One rate request per run serves the whole catalog. Product count does not change the number of API calls.
- Compute with
Decimal, round once per product, and send the price as a string. - Shopify updates variants with the
productVariantsBulkUpdateGraphQL mutation. WooCommerce updatesregular_priceon the product or variation endpoint.
Compute the prices
Each product record holds its grams, karat and making charge. The script fetches the rates once, computes every price and prints the update body for Shopify and for WooCommerce.
import jsonfrom decimal import Decimal, ROUND_HALF_UPfrom urllib.request import urlopenAPI = "https://api.goldprice.dev"cents = lambda d: d.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)with urlopen(f"{API}/v1/carat?currency=USD", timeout=10) as r: rates = json.load(r)PRODUCTS = [ # Shopify variant id, WooCommerce product id, grams, karat, making charge ("gid://shopify/ProductVariant/1001", 2001, "4.20", 14, "35.00"), ("gid://shopify/ProductVariant/1002", 2002, "6.80", 18, "52.00"),]def price(grams, karat, making): return cents(Decimal(grams) * Decimal(rates[f"price_gram_{karat}k"]) + Decimal(making))for variant_id, woo_id, grams, karat, making in PRODUCTS: p = str(price(grams, karat, making)) shopify = {"productId": "gid://shopify/Product/<id>", "variants": [{"id": variant_id, "price": p}]} woo = ("PUT", f"/wp-json/wc/v3/products/{woo_id}", {"regular_price": p}) print(json.dumps(shopify)) print(*woo[:2], json.dumps(woo[2]))Run on 2 November 2026:
{"productId": "gid://shopify/Product/<id>", "variants": [{"id": "gid://shopify/ProductVariant/1001", "price": "342.52"}]}PUT /wp-json/wc/v3/products/2001 {"regular_price": "342.52"}{"productId": "gid://shopify/Product/<id>", "variants": [{"id": "gid://shopify/ProductVariant/1002", "price": "692.15"}]}PUT /wp-json/wc/v3/products/2002 {"regular_price": "692.15"}The 14K ring at 4.20 g came to 342.52 dollars and the 18K ring at 6.80 g to 692.15 dollars.
Write the prices to Shopify
Shopify's Admin GraphQL API takes the productVariantsBulkUpdate mutation with a product id and a list of variants. Each variant carries its id and the new price. Send it to https://<store>.myshopify.com/admin/api/2026-10/graphql.json with the X-Shopify-Access-Token header.
mutation ($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkUpdate(productId: $productId, variants: $variants) { productVariants { id price } userErrors { field message } }}The variables are the JSON objects the script printed. Read userErrors on every response.
Write the prices to WooCommerce
The WooCommerce REST API updates a product with PUT /wp-json/wc/v3/products/<id> and a JSON body that holds regular_price as a string. Variations use /products/<id>/variations/<variation id>. Authenticate with a consumer key and secret over HTTPS.
curl -X PUT https://shop.example.com/wp-json/wc/v3/products/2001 \ -u consumer_key:consumer_secret \ -H "Content-Type: application/json" \ -d '{"regular_price": "342.52"}'Run it on a schedule
- Cadence. A 15-minute job makes 96 requests a day to the price API, about 2,900 a month, which is inside the 5,000 requests on a free key.
- Delay. Free and keyless prices are 15 minutes late. Paid plans return live prices.
- Rounding. Choose a price ending, such as .00 or .95, and apply it after the sum.
- Display. Show the update time on the product page so a customer knows how current the price is.
- Failures. If the rate fetch fails, leave prices as they are. Do not write zero.
- Writes. Update only the products whose price changed since the last run.