← back to Kravet Sheet Sync 2026 04 20

test_push_fix_variants.py

65 lines

#!/usr/bin/env python3
"""Add the missing main-sell variant to the 2 test products.
Current state: each product has only the Sample variant.
Target: add a second variant with the DWKK main SKU + MAP price + cost.
"""
import json, urllib.request

TOKEN=os.environ.get('SHOPIFY_ADMIN_TOKEN','')
URL='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json'

MAIN_VARIANTS = [
    {"pid": "gid://shopify/Product/7821427441715",
     "dw_sku": "DWKK-141031", "price": "218.93", "cost": "116.76", "width_in": "20.5"},
    {"pid": "gid://shopify/Product/7821427474483",
     "dw_sku": "DWKK-141042", "price": "240.98", "cost": "128.52", "width_in": "21"},
]


def gql(q, v=None):
    body = json.dumps({"query": q, "variables": v or {}}).encode()
    req  = urllib.request.Request(URL, data=body, method="POST",
            headers={"Content-Type":"application/json","X-Shopify-Access-Token":TOKEN})
    data = json.loads(urllib.request.urlopen(req, timeout=30).read())
    if "errors" in data: raise RuntimeError(data["errors"])
    return data["data"]


# Rename option "Title" to "Size" so naming matches existing DWKK catalog.
RENAME_OPTION = """
query($id: ID!) { product(id:$id){ options{ id name values} } }
"""

# Add main variant with new option value
ADD_VARIANT = """
mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!,
                                   $strategy: ProductVariantsBulkCreateStrategy) {
  productVariantsBulkCreate(productId: $productId, variants: $variants, strategy: $strategy) {
    productVariants { id sku title price }
    userErrors { field message }
  }
}
"""

for m in MAIN_VARIANTS:
    # Shopify's option on these products is "Title" — use the existing option name.
    # Use a descriptive value similar to the existing catalog pattern.
    option_value = f"Sold Per Single Roll - {m['width_in']}In Wide"

    variant_input = {
        "price": m["price"],
        "inventoryItem": {"sku": m["dw_sku"], "tracked": True, "cost": m["cost"]},
        "optionValues": [{"optionName": "Title", "name": option_value}],
    }
    r = gql(ADD_VARIANT, {
        "productId": m["pid"],
        "variants":  [variant_input],
        "strategy":  "DEFAULT",  # don't remove standalone — keep Sample
    })
    errs = r["productVariantsBulkCreate"]["userErrors"]
    if errs:
        print(f"  {m['dw_sku']}: ERRORS: {errs}")
    else:
        v = r["productVariantsBulkCreate"]["productVariants"][0]
        print(f"  {m['dw_sku']}: added {v['sku']} '{v['title']}' @ ${v['price']}")