Updateinventorycost

hi, ive been pocking arround for nearly 20h im not good at all in coding but ive already created this same code using the rest api and chatgpt

i cannot seem to find a way to update the cost of the product even if i have all the needed argument… ive been stuck at this for a long time and both ai cannot seem to find a probleme and me neither…

the code need to update unit cost per variants

def update_inventory_cost(inventory_item_id, cost_amount, currency_code):
    """
    Updates the cost of an inventory item in Shopify.

    Args:
        inventory_item_id (str): The GID of the inventory item.
        cost_amount (Union[str, float, Decimal]): The final cost you want to set.
        currency_code (str): Currency code, e.g., 'CHF' or 'USD'.
    """
    mutation = """
    mutation UpdateInventoryCost($input: InventoryItemInput!) {
       inventoryItemUpdate(input: $input) {
        inventoryItem {
          id
          unitCost {
            amount
            currencyCode
          }
        }
        userErrors {
          field
          message
        }
      }
    }
    """

    try:
        # Convert cost_amount to a string with 2 decimals
        cost_str = f"{Decimal(cost_amount):.2f}"

        # Prepare variables for the mutation
        variables = {
            "input": {
                "id": inventory_item_id,  # Ensure this is a valid GID
                "cost": {  # Correct field name
                    "amount": cost_str,  # Use the value of cost_str
                    "currencyCode": currency_code
                }
            }
         }     
       error message 
 Exception: GraphQL Errors: [{'message': "Field 'inventoryItemUpdate' is missing required arguments: id", 'locations': [{'line': 3, 'column': 8}], 'path': ['mutation UpdateInventoryCost', 'inventoryItemUpdate'], 'extensions': {'code': 'missingRequiredArguments', 'className': 'Field', 'name': 'inventoryItemUpdate', 'arguments': 'id'}}]


let me know if this need way more explaination or anything…

thanks for your time

Hi, try this

mutation productVariantsBulkUpdate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
  productVariantsBulkUpdate(productId: $productId, variants: $variants) {
    product {
      id
    }
    productVariants {
      id
      sku
      inventoryItem{
        sku
        unitCost{
            amount
            currencyCode
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

input:

{
  "productId": "gid://shopify/Product/8356465410331",
  "variants": [
    {
      "id": "gid://shopify/ProductVariant/47020309938459",
      "inventoryItem":{
        "cost":888
      }
    }
  ]
}