Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
In my Shopify App which I'm building with NodeJS + React, I would like to insert liquid code into the theme files of the shop.
I found this tutorial, but it only shows how to do it "manually".
I thought about getting the theme file as a string using the Asset API, then inserting a liquid code using javascript native functions like .findIndex, .slice, etc etc. Is this the only way to insert code into theme files? Or is there an easier way?
Thanks.
Hey,
You need to an API call to the theme. Please read all the instructions here. It's important that you already do have experience with Javascript.
https://shopify.dev/docs/admin-api/rest/reference/online-store/asset
Hi.
Thanks for the reply.
I've read the instructions, but I don't think there is an explanation about updating an existing theme.
I would like to know how to insert a liquid code into (using the Asset API) an existing theme.
Thanks.
Hello,
Yes, there is, you just need to grab the current theme's ID and make a PUT call to add create or update whatever file you wish.
I reckon that you're not very familiarized with Javascript itself since this is a fairly basic API call, therefore I'd highly suggest getting proficient in React before diving into Shopify App developement, it will save you a lot of headache and make your progress much more smooth.
Unfortunately app development for Shopify is not much beginner-friendly, I feel you there.
Consider taking a look in Udemy courses for React/Node to gain a bit more of understanding of how it works and I promise you it's going to be much more easy to understand!
Wishing you the best.
Kind regards,
Diego
I apologize for my confusion question.
I really appreciate the explanation, but I have solid experience in React + NodeJS applications.
I understand that I have to make a PUT call to update the theme, but I not sure if my methodology is correct.
At first, I get the liquid code by calling
GET /admin/api/2021-01/themes/{theme_id}/assets.json?asset[key]=templates/index.liquid
And next, for example, if I want to insert <h1>Hello World</h1> right before {{ content_for_header }}, should I do something like below?
const str = "Liquid code as string"
const indexOfContentForHeader = str.search("{{ content_for_header }}");
const updatedLiquidCode = str.slice(0, indexOfContentForHeader) + "<h1>Hello World</h1>" + str.slice(indexOfContentForHeader)
then,
PUT /admin/api/2021-01/themes/{theme_id}/assets.json
{
"asset": {
"key": "templates/index.liquid",
"value": updatedLiquidCode
}
}
or is there a "cleaner" way? (Is there a library to do something like the JavaScript code above?)
Sincerely,
Daniel
Oh, I see.
Yes, as far as I am aware this would be the best way to do it.
Personally I'd do something like:
data.replace(`{{ content_for_header }}`, `<h1>hey</h1>
{{ content_for_header }}`);
Of course you'd need to use REGEX to target something like {{content_for_header }} as well, but that's about it!
In Shopify dev-ing there's a lot of "non-conventional/ideal" ways to do things, unfortunately.
Shopify's support specifically for app developers is terrible as well, so feel free to reach out for anything you need, I'll be happy to help.
Kind regards,
Diego
Thanks for the quick replay!
Yes, I think using the replace function and REGEX is great and necessary!
I've been feeling that too, so thank you so much for helping!
In Shopify dev-ing there's a lot of "non-conventional/ideal" ways to do things, unfortunately.
Best,
Daniel
hello Diego_ezfy
I hope you are doing well,
can you help me out with some sample code to make an api call
I'm not able to do this.
using react and node for the developement
thanks.
I was using the same query till now but shopify has declared REST as legacy, so I was trying to use themeFileUpsert query in graphQL, but it says you need write_themes and write_themes_assets access scope which I have been trying to add in my TOML file but it throws an error saying that write_themes_assets is not a valid access scope, do you have any migration steps which can help me.
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024