ShopifySharp
ShopifySharp copied to clipboard
Adding Line items in existing order does not seem to work
Hi,
I try to add a free product in the order created webhook. It does not return an error, but also no extra product is added in the order
Is this possible or am i doing it wrong?
var order = getOrder(orderId);
var orderlines = order.LineItems.ToList(); orderlines.Add(new LineItem { Price = 0, ProductId = productId, Quantity = 1 }); order.LineItems = orderlines; var service = new OrderService(CurrentShop.Name, CurrentShop.Token); await service.UpdateAsync(order.Id.Value, order);
Hey @RubinhoD, did you get this working? I'm not positive but I think once an order is already created, you can't modify its line items or price. If you need to do that then you might want to use a Draft Order via the DraftOrderService
class.
Hi @nozzlegear , yes i got it working, but not this way. Shopify does not support order updates via this Api, but you should use GraphQL.
Below, you find my example. I also used the GraphQLNETClient.cs class from here to execute: https://github.com/kiflay/GraphQL-NET-Client
` var graphQl = new GraphQLNETClient { CurrentShop = CurrentShop };
// Start mutation
string query = @"
mutation {
orderEditBegin(id: ""gid://shopify/Order/" + orderId + @""") {
calculatedOrder {
id
}
}
}
";
var jSonResponse = await graphQl.Execute(query);
var calculatedOrder = jSonResponse.data.orderEditBegin.calculatedOrder.id.ToString();
// Add orderline
query = @"
mutation addVariantToOrder{
orderEditAddVariant(id: """ + calculatedOrder + @""", variantId: ""gid://shopify/ProductVariant/" + productId + @""", quantity: " + quantity + @"){
calculatedLineItem {
id
}
userErrors {
field
message
}
}
}
";
jSonResponse = await graphQl.Execute(query);
var lineItemId = jSonResponse.data.orderEditAddVariant.calculatedLineItem.id.ToString();
// Discount
query = @"
mutation orderEditAddLineItemDiscount{
orderEditAddLineItemDiscount(discount: {percentValue: 100}, id: """ + calculatedOrder + @""", lineItemId: """ + lineItemId + @"""){
userErrors {
field
message
}
}
}
";
// Commit
jSonResponse = await graphQl.Execute(query); // Response eventueel loggen
query = @"
mutation commitEdit {
orderEditCommit(id: """ + calculatedOrder + @""", notifyCustomer: false) {
order {
id
}
userErrors {
field
message
}
}
}
";
jSonResponse = await graphQl.Execute(query); `