orderEditBegin OrderEditAddLineItemDiscount not applying a discount to the item

orderEditBegin OrderEditAddLineItemDiscount not applying a discount to the item

BalloutMohamad
Shopify Partner
6 0 0

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 

Replies 0 (0)