Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

How i can create draft order using GraphQL API

How i can create draft order using GraphQL API

vinodk
Shopify Partner
24 0 5

Hello
I am working on public app and i want to create draft order with change item price with the help of graphql API but not able to figure out how i will do. Please any one let me know complete process of creating draft order by mutation in graphql.  below is some example code.

mutation {
		draftOrderCreate(
			input: {
				email: "test@gmail.com"
                lineItems: {
                    appliedDiscount: {
                        amount: 20
                        description: "test des"
                        title : "Test two"
                        value: 10
                        valueType: FIXED_AMOUNT
                    }
                    customAttributes: {
                        key: "payment"
                        value: "partial"
                    }
                    quantity: 2
                    weight : {
                        unit: GRAMS
                        value: 10
                    }
                }
                localizationExtensions: {
                    key: SHIPPING_CREDENTIAL_BR
                    value: "test"
                }
                metafields: {
                    description: "test"
                    key: "orde_id"
                    namespace: "prt"
                    type: "test"
                    value: "drt"

                }
                paymentTerms: {
                    paymentSchedules: {
                        dueAt: "2022-12-17T15:50:00Z"
                        issuedAt: "2022-12-15T15:50:00Z"
                    }
                }
                privateMetafields: {
                    key: "Test"
                    namespace: "dftrt"
                    valueInput: {
                        value: "testd"
                        valueType: STRING
                    }
                }
              purchasingEntity: {
                    customerId: "gid://shopify/Customer/6453791457508"
                    purchasingCompany: {
                       companyContactId: "gid://shopify/companies/63373540"
                       companyId: "gid://shopify/companies/63373540"
                       companyLocationId: "gid://shopify/locations/63570148"
                    }
                  }
                shippingAddress: {
                    address1: "sdsd"
                    address2: "sadsd"
                    city: "karnal"
                    company: "desonomate"
                    country: "India"
                    countryCode: IN
                    firstName: "Vinod"
                    lastName: "Kumar"
                    phone: "+919865326598"
                    province: "Haryana"
                    provinceCode: "HR"
                    zip: "132157"
                  }               
                  shippingLine: {
                    price: 50
                    shippingRateHandle: "FEDS"
                    title: "fedex"
                  }


			}
		) {
			 userErrors {
                field
                message
            }
		}
	}
Reply 1 (1)

ShopifyDevSup
Shopify Staff
1453 239 535

Hi @vinodk 👋

 

You can use the `DraftOrderLineItemInput.appliedDiscount` parameter to modify the line item price. In my example `draftOrderCreate` mutation below, a fixed amount discount was applied to a line item: 

mutation ($input: DraftOrderInput!) {
    draftOrderCreate(input: $input) {
        draftOrder {
            id
            lineItems (first:5) {
                nodes {
                    id 
                    originalUnitPrice
                    discountedUnitPrice
                    appliedDiscount {
                        amountV2 {
                            amount
                        }
                    }
                }
            }
        }
    }
}

With variables: 

{
    "input": {
        "lineItems": [{
            "appliedDiscount": {
                "amount": "5.00",
                "description": "test modify lineitem price",
                "title": "modify lineitem price",
                "value": 5.00,
                "valueType": "FIXED_AMOUNT"
            },
            "variantId": "gid://shopify/ProductVariant/39803484209270",
            "quantity": 1
        }
    ],
        "purchasingEntity":{
            "customerId": "gid://shopify/Customer/5790884724854"
        }
    }
}


The mutation successfully returned the below response where we can see the discount applied:

{
    "data": {
        "draftOrderCreate": {
            "draftOrder": {
                "id": "gid://shopify/DraftOrder/897414987894",
                "lineItems": {
                    "nodes": [{
                         "id": "gid://shopify/DraftOrderLineItem/57463112892534",
                         "originalUnitPrice": "97.87",
                         "discountedUnitPrice": "92.87",
                         "appliedDiscount": {
                             "amountV2": {
                                 "amount": "5.0"
                             }
                         }
                    }]
                }
            }
        }
    }
}

 

Hope that helps!

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog