How to create matching order and draft order metafields using graphql?

I have a problem with creating a draft order metafield with graphql but also I would like to know what would be the correct way to create matching draft order metafield and order metafield using graphql?

Something like here. But I want to create it using MetafieldDefinitionInput and metafieldDefinitionCreate

1 Like

Hi @madfcat,

Regarding the problem with creating a draft order metafield definition. I have answered that question in the thread here, any questions or follow ups with that specific issue can be done in that thread directly:

As for the question on how to sync metafields from a completed Draft Order to the newly created Order.

Unfortunately the functionality that you linked in the Help Center, is only available via the Shopify Admin at this time. I will be submitting some feedback on your behalf that you would like to see away to enable this copying of metafields from draft orders to orders via the API as well. You can also submit this feedback yourself via the Shopify.dev documentation, scrolling to the bottom of the page and selecting Was this section helpful?: No and entering your custom feedback.

I do however have a workaround that I can suggest to implement this feature manually via the API and webhooks.

  1. Subscribe to the draft_orders/update metafield via the API, ensuring that you add the namespace of the metafield you’re wanting to transfer in the mutation input.
  2. When the draft order is closed, it will trigger the webhook to be sent, and will include the metafield in the payload. You can tell that the metafield is closed by the status field returned.
  3. After the draft order is closed, you can then add the metafield returned from the webhook payload to the newly created order. The newly created order can be retreived from the Draft Order object with the order field
1 Like

Thank you for your detailed explanation and suggestions.

What I am trying to achieve is a really basic thing.

Create an order with a metafield and a localizedField.

orderCreate does not have localizedFields input. Why so? It would solve my problem from the very beginning.

Therefore, I have 2 options.

orderCreate first, then orderUpdate with localizedField.
Or, draftOrderCreate and then draftOrderComplete.

The problem that I am experiencing is that my draft order and then order ends up without metafield that I have as the input for the mutation.

The only way it works using matched metafield. When order has a metafield matched to the draft order metafield. That’s why I got stuck creating draft order metafield.

The question is. If I create the same metafield for Order and Draft order will it solve my problem? Will I be able to create a draft order with a specific metafield and then translate it into an order with draftOrderComplete having that same metafield.

What is the correct way to do it?

Hi @madfcat,

Unfortunately it will not automatically create the same metafield on the Order after completing the Draft Order, even if there are duplicate metafield definitions for both Drafts and Orders.

The correct way of adding metafields to an order created from a draft order in this case would be with the workaround I provided in my previous reply, using webhooks to be notified when a Draft Order is closed and an Order created, copying the metafield from the webhook payload, and adding it to the new Order manually, with an orderUpdate mutation.

Regarding the localizedFields input, I can confirm this is not available via the orderCreate mutation at this time, and I will be submitting feedback for this as well, and you can also submit feedback on the Shopify.dev documentation directly as previously mentioned.

As a workaround for this, I would recommend adding the country specific tax info, or other localized field info via a metafield or customAttributes when creating the order with the orderCreate mutation.

Matching draft order definition created from the order metafield definition manually from admin panel made it work for me without webhooks.

That is correct, as long as you have created the Draft Order Definition first, and linked it to the Order Definition created in the admin, where you can utilize the Matching draft order definition settings.

I apologize for any confusion, though I was offering workarounds specifically for programatic solutions that did not require manual metafield definitions created in the admin, specifically for apps with API and Webhook access.

Thank you. I appreciate it. Webhooks just add a bit of complexity to my setup.
I want to be able to create matching draft order metafields programatically but in my case it is ok for some manual setup tweaking. Otherwise, I would use webhooks as a workaround. But again it would be great to be able to control matching draft order metafields.
Or have the createOrder with all the needed parameters for the full order creation and no need for additional orderUpdate.

I’m very glad to help!

I can confirm I have submitted some feedback on your behalf for this feature to be available via the API as well, and I would also recommend submitting feedback yourself as well through our Shopify.dev documentation. On any relevant page you can scroll down to the bottom and select Was this section helpful?: No and enter your custom feedback.

I may recommend submitting the feedback on this page specifically:

I’ll go ahead and solve this thread for now, but please do follow up if you have any more questions I can help with!

1 Like