Programmatically update product.liquid on app install - shopify

Our Shopify app is activated by adding our snippet into the product and cart liquid files.
We noticed that adding the snippet is a tricky part for most merchants and we would like to offer them an option that we would do the heavy lifting for them.
My questions:
What is the best practice for doing such action?
How we can go on preview mode and wait till the merchant accept the change?
Any code samples using the Shopify Python SDK will be highly appreciated.
Thanks!

Here is a pattern that will work, and cause the least damage on uninstall.
download product.liquid using API (have fun ensuring you get the one that matters, as it could be in sections or snippets now)
inject your special codes in that snippet
upload your modified product.foobleflap.liquid
So now all your merchant need do is substitute your Liquid file for the original one. On App uninstall, no kittens have been killed, they need to simply revert back to their original Liquid file
Of course there are other patterns, but modding their original file with your snippet is probably least desirable from an uninstall perspective, as they would have to manually erase traces of your code.

Related

Shopify Interact with product page HTML via app

We plan to create a Shopify app but we face some problems to find in documentations how to interact with pages.
I`m not sure we must ask every time users to add codes and etc, so I need to know if I miss something.
I need to interact with the product pages in Shopify from an app.
I search and read all the web for this and everything is how to start but not the actual examples.
I know for the ScriptTags and how to include them but that is.
If someone can give me a simple example of how for example to hide the Buy button and insert something on instead.
I know how to select it with the default theme with JQuery but what about all other themes?
There are two ways to interact with the front page:
1) Inject some code in the page ( the live one )
2) Use ScriptTag as you mentioned.
The code injection script will modify the product template for example and inject your code if you like to do this automatically or you can instruct the user to do so on their own, but if they are not code savvy there might be issues.
In addition this code will live only on the live theme in most cases. And if the user likes to delete your app at a specific time you must be sure to write the logic in such way that it won't affect the site if your app is no longer present ( since it will be really hard to clean up the files once you add the code automatically )
For this approach you will need to use the Asset API.
You will need to get the content of the file with a GET request to the file and make a put request in order to update it.
The better approach is to use ScriptTag API.
This will allow the use of a script file that will be attached on EVERY theme. This will not modify the theme files in any form or shape.
It seems that this is the approach that you are looking for.
Please have in mind that you will be the one hosting the file from your app so pretty much you can write what ever you like there. So if you like to use jQuery you must be sure that the themes have included jQuery or you will have to add the jQuery core code inside your script.
As for how to write a script tag, there is a pretty straightforward documentation here: https://help.shopify.com/en/api/reference/online-store/scripttag#create-2020-01

Need advice on creating an online contract generation system

First of all, be assured I am not asking how to write up a contract; I already have that covered. My question is how I can best program a system for generating it for clients to digitally sign?
My original plan that just seems like overkill to me is this:
I fill in all the details.
Click a button to preview it, making sure everything is correct.
Click a button to simultaneously create a directory for the client and create 3 files inside that directory.
The three newly created files are:
the unsigned contract.
a confirmation page of having signed the contract, with other details the client will need to know.
the actual signed contract that the client can now view online at any time by going to a URL such as /clients/username/index.php
I have it working, though I keep thinking it could be simplified further, but not sure of the best way to do it.
Any suggestions?
I recently helped on a project much like this, & I think you've got the right idea...
Write up the contract & preview (make sure it's right)
Have them come "sign" it
Save it
Only thing I'd change is, I'd use a DB instead of file creation. Personally, I find that simpler, but it might not feel that way to everybody. It's definitely more flexible though, 'cause you can easily generate an HTML or a PDF from the DB data, but you'd have a harder time doing that from a file on the system. Assuming you're using PHP (based on the .php you mentioned), there's a whole section of functions for generating PDF's in PHP.

How to create shopify ScriptTag that would be used without asyncLoad wrapper?

Shopify does wrap all scripts added through ScriptTag API into special asyncLoad function. While it makes sense in many circumstances there still situations when you need script tag inserted directly into the header.
For example Optimizely wouldn't work that smooth if there wasn't other way to insert script tag, i.e. somehow theirs script appears in the store front independently, i.e. without asyncLoad function.
How would I do the same?
One option is to parse and then send back current theme, but it looks weird and not quite reliable, and according my research Optimizely does know another approach.
I was able to get in touch with Optimizely, here is their's response:
We actually partnered with Shopify to get our snippet added to the page in this way by default. I don’t think there is a simple way to work around the async limitation otherwise.
and here is Shopify's response:
Unfortunately this was a one-off hack-y fix, and not something that we're open to doing again at this time.
So, for now the only reliable way to work around the async limitation is to instruct users to place the script into current theme as others applications does.

Integrating custom analytics with Shopify

I am developing an analytics product similar to Google Analytics and want to offer an integration for our Shopify users.
I would need to do just two things:
1) Insert our JavaScript snippet into the body of all the shop's pages
2) Insert a second conversion snippet into the body of only the "thank you" page after a purchase to record the sale, passing back the order total and order number
Could you point me in the right direction?
The ScriptTag endpoint looks optimal except that it doesn't seem to provide any way to differentiate between the "thank you" page and other pages, or to pass the order details from the "thank you" page into the script.
It seems like I either need an endpoint that can inject arbitrary code into all pages, or one which lets me alter the theme layout file to add code before the </body> tag.
If I can do that much, it looks like I might be able to use a conditional in the templating language to only show the conversion code if the URL matches the "thank you" page URL, and use {{ total_price }} and {{ order_number }} in that code.
Does the uninstall webhook allow me to make changes before the rights for the app are revoked? It seems like I would need a way to clean up at uninstall to remove my code from the theme, if I'm allowed to edit the layout file.
itThere are two ways you can go about this:
ScriptTags: You're already aware of this method. It's true that the script is loaded into each and every page, but you can look at document.url in your js to figure out which page you're on and conditionally execute code based on that.
Scripts inserted this way are executed during the 'onLoad' event. At that time you have access to the DOM and can do basically whatever you want with the page.
Snippets: Using the Assets endpoint you can make arbitrary changes to the user's theme. Be careful with this power! The recommended way to make complex changes to a page is to create a custom snippet in the theme with your additions and then tell the user to insert an include tag into their code where it needs to appear. This reduces clutter in the main theme files.
I'd recommend using ScriptTags wherever possible. As mentioned below, they don't need cleaning up and remove the need for user interaction when setting up the app.
--
As for the uninstall webhook: It is fired after the app has been uninstalled from the shop, so you no longer have access. It's designed to be used to trigger cleanup on your end (remove db entries, etc.). Note that ScriptTags and Webhook subscriptions are automatically cleaned up, but any changes you've made to the theme aren't
You ask a lot of questions in one posting.
For your #1 and #2 read:
http://api.shopify.com/scripttag.html
Secondly, for your app install/uninstall read:
http://api.shopify.com/webhook.html
Pay attention to the part about app/uninstalled since you're interested in that.
As for knowing when someone installs your App, that is really up to you to figure out. Should not be too hard since you probably read about how to make and App in the first place. Shopify provides lots of boiler plate code on github that can show you almost everything you asked here.

How do I update a variant price using shopify gem in rails 3.2.2?

Rails 3.2.2 app, using the most recent shopify gem on github.
I've looked at other threads on the shopify google group that suggested switching
ShopifyAPI::Base to use xml, but that had no effect.
Simple script, updates a product's variant prices. This used to work,
not sure when it stopped working, but script is identical to the old
working version.
Essentially, if I set a variant price and compare_at_price, and then
save, shopify does not reflect a change. The method returns true,
however. Originally, the script simply called product.save at the end,
but I've also tried saving the variants individually and no luck.
Thanks for any help.
I wasn't able to reproduce the problem.
If you provide us with a request id then we could lookup the request in our logs. You can obtain the request id using the following code just after save that isn't working.
ShopifyAPI::Base.connection.response['x-request-id']
What I can tell you is that:
You shouldn't need to use xml format (JSON tends to be faster)
You should be able to save changes on a variant or product
This was a bug in Shopify, which has now been fixed.
It affected product updates through the API for shops that had inventory management enabled.
Please retest and confirm that it has been fixed.