orderEditBegin OrderEditAddLineItemDiscount not applying a discount to the item

Topic summary

A developer is attempting to apply a discount to individual line items in a Shopify order using the GraphQL Admin API with Python. They’re using a two-step process: first calling orderEditBegin to initiate the edit session, then orderEditAddLineItemDiscount to apply the discount.

Current Issue:

  • The API returns a success response, but the discount is not actually being applied to the order item
  • The code successfully retrieves the calculated order ID and line item ID from the initial mutation

Implementation Details:

  • Using a fixed value discount of $5.00 USD
  • Discount description: “Surcharge Adjustment”
  • Testing with a single line item before scaling to multiple items

Code Structure:
The provided code shows the GraphQL mutations and variable setup, though portions of the response handling code appear corrupted or reversed in the original post. The developer is seeking help to identify why the discount mutation completes without errors but fails to modify the order.

Summarized with AI on November 8. AI used: claude-sonnet-4-5-20250929.

Hello all, I am trying to apply a discount for each line item in the order. I am trying to just do one line at the moment, I return a success however the discount is not being applied to the order item. Below is the code, I am using python.

# GraphQL endpoint
graphql_url = f"https://{SHOP_NAME}.myshopify.com/admin/api/{SHOPIFY_API_VERSION}/graphql.json"

headers = {
    "Content-Type": 'application/json',
    "X-Shopify-Access-Token": ACCESS_TOKEN,
}

begin_edit_query = '''
mutation BeginEdit {
  orderEditBegin(id: "gid://shopify/Order/5907645858092") {
    userErrors {
      field
      message
    }
    calculatedOrder {
      id
      lineItems(first: 1) {
        edges {
          node {
            id
            quantity
            variant {
              price
              inventoryItem {
                unitCost {
                  amount
                  currencyCode
                }
              }
            }
          }
        }
      }
    }
  }
}
'''

# Send the GraphQL request to begin the order edit
response = requests.post(graphql_url, headers=headers, data=json.dumps({'query': begin_edit_query}))
result = response.json()

# Extract calculatedOrder ID and lineItem ID
calculated_order_id = result['data']['orderEditBegin']['calculatedOrder']['id']
line_item_id = result['data']['orderEditBegin']['calculatedOrder']['lineItems']['edges'][0]['node']['id']

# GraphQL mutation for adding a discount
add_discount_mutation = '''
mutation OrderEditAddLineItemDiscount($discount: OrderEditAppliedDiscountInput!, $id: ID!, $lineItemId: ID!) {
  orderEditAddLineItemDiscount(discount: $discount, id: $id, lineItemId: $lineItemId) {
    calculatedOrder {
      id
    }
    userErrors {
      field
      message
    }
  }
}
'''

# Replace with the actual values
variables = {
    "discount": {
        "fixedValue": {
            "amount": "5.00",
            "currencyCode": "USD"
        },
        "description": "Surcharge Adjustment"
    },
    "id": f"{calculated_order_id}",
    "lineItemId": f"{line_item_id}"
}

# Send the GraphQL request to add the discount
response = requests.post(graphql_url, headers=headers, json={'query': add_discount_mutation, 'variables': variables})
result = response.json()

# Check if there are any errors in adding the discount
if 'errors' in result:
    print(f"Error adding discount: {result['errors']}")
else:
    print("Discount added successfully.")

# Print response from adding discount
print(result)

Any help would be appreciated