Solved

In a discount function, is it possible to auto create the diccount?

bandini
Shopify Partner
9 0 0

I'm sorry if this is a basic question, but I'm following through all the possible ways the documentation teaches how to create this and all of then involve manually querying the GraphQL api and creating a discount before it works.

 

That's true for both a simple function or a function within an app. 

That's very unpractical, specially if we are distributing it as a public app or to various clients.

 

Is there a way to create the actual discount, from my discount function, when I install the app or the manual way is the only possibility right now?

 

The manual creation is explained here: https://shopify.dev/docs/apps/selling-strategies/discounts/experience/getting-started

Accepted Solution (1)

Nick_Wesselman
Shopify Staff
123 31 49

This is an accepted solution.

Hi @bandini --

If you continue on that tutorial, it describes creating a UI for the Discounts section of shop admin, which shop users can use to create your discount. This is the preferred path as it also enables your function to be configurable using the same UI.

 

It would also be possible to add a GraphQL mutation on app install, during the OAuth flow. This depends on your app implementation but in the Remix template this could be done in the `afterAuth` hook of `shopify.server.ts`:
https://github.com/Shopify/shopify-app-template-remix/blob/main/app/shopify.server.ts

Nick Wesselman | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

View solution in original post

Replies 6 (6)

Nick_Wesselman
Shopify Staff
123 31 49

This is an accepted solution.

Hi @bandini --

If you continue on that tutorial, it describes creating a UI for the Discounts section of shop admin, which shop users can use to create your discount. This is the preferred path as it also enables your function to be configurable using the same UI.

 

It would also be possible to add a GraphQL mutation on app install, during the OAuth flow. This depends on your app implementation but in the Remix template this could be done in the `afterAuth` hook of `shopify.server.ts`:
https://github.com/Shopify/shopify-app-template-remix/blob/main/app/shopify.server.ts

Nick Wesselman | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

bandini
Shopify Partner
9 0 0

Hey Nick, thanks for the quick reply.

 

I did follow through the tutorial, but it still uses manual graphql interaction (step 4).

 

The mutation on the app install tip is great, though! I'll try that.

 

Thanks a lot

Nick_Wesselman
Shopify Staff
123 31 49

I should have been more specific -- if you follow the rest of the tutorial series. The 3rd part of that series describes creating a UI which invokes the GraphQL mutation and integrates with the Discounts section of admin. Again, this is the recommended mechanism.

Nick Wesselman | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

bandini
Shopify Partner
9 0 0

Hey, thanks for replying again

 

So, the tutorial is a bit confusing because it start by building it by using the manual Graphql creation, and then in the last part it creates this UI for it. But all the steps has the previous ones as a requirement.

 

Even if you check only the last step, it still relies on manually registering the function via Graphql in order for it to be shown in the UI.

 

https://shopify.dev/docs/apps/selling-strategies/discounts/experience/ui

 

You can see it here at step 4. If that's not done, it won't show it there. It's this manual part I'm trying to avoid.

Nick_Wesselman
Shopify Staff
123 31 49

The tutorial series has you use GraphiQL earlier on so that you can see your discount work without creating a UI first. Later the UI replaces any need to use GraphiQL. See lines ~64-140 of the code sample in step 2.

 

You are misunderstanding the GraphQL in step 4. That is not a query you execute in GraphiQL -- it's an update to the Function's input query. Input queries are deployed with the Function and automatically executed by Shopify when the Function ew

Nick Wesselman | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

bandini
Shopify Partner
9 0 0
I probably messed something up then, when trying to build it without the
manual queries.

I'll retry.

Thanks!