Reputation: 91
I am trying to figure out how to pass data between the theme-app-extension and the backend of the application that the theme-app-extension is connected to. The theme-app-extension is all liquid, css, and javascript so I wasn't sure if there was a built in way to pass data between the two. For example is there a suggested method to pull data into the theme-app-extension from the database and is there a suggested way to send data to the database from the code running the theme-app-extension? I am fairly new to doing anything with theme-app-extensions as well as building Shopify applications. I have built Shopify applications that was admin facing or just cosmetic, this is my first time building a Shopify application that takes user input and sends it to the database and retrieves that data for the end-user to see.
Any suggestions would be greatly appreciated.
Thank You.
Upvotes: 6
Views: 3458
Reputation: 928
There are mainly 2 ways to do so:
Upvotes: 1
Reputation: 21
You can use Shopify metafields API in your backend to add metafields on product, app...
Then in your liquid block you can get the metafield like
<script>
MY_VAR = '{{ app.metafields.mynamespace.myvar }}';
</script>
More information on metafields on the Shopify blog
Upvotes: 2
Reputation: 160
I started working on Shopify App (Theme App Extension) a week ago
I run into the same problem, so I did consume API in a javascript file using Fetch Here is an example of the code:
I add this into global.js file inside assets
And then I linked the script files inside my Liquid block
Also inside global.js, I did manipulate DOM by injecting data consumed from API, here is an example
Finally what I did is communicate directly with DOM to inject or retrieve data, then handle it using javascript
I hope this helps you
Ps: This is my first experience with Shopify too, and I was quite disappointed (lack of resources, docs, and community)
Upvotes: 3
Reputation: 11427
You usually (always) pass data from the front-end to the back-end using an App Proxy. There really is no other way at this time. The Proxy gives you an XHR call you can use, and you can return Liquid or JSON. Your choice.
Upvotes: 0