Add customized code to Shopify theme I am new to making shopify applications. I have made one application which helps merchants add additional features in their stores.

I have written some PHP code that is given below.

$themeList = shopify_call($token,$shop,"/admin/api/2019-07/themes.json",array(),'GET');
$themeList = json_decode($themeList['response'], JSON_PRETTY_PRINT);
$theme_id = $themeList['themes'][0]['id'];

$customTheme = shopify_call($token,$shop,"/admin/api/2019-07/themes/".$theme_id."/assets.json?asset[key]=layout/theme.liquid&amp;theme_id=".$theme_id,array(),'GET');
$customTheme = json_decode($customTheme['response'], JSON_PRETTY_PRINT);
$currentTheme = $customTheme['asset']['value'];
$pos = strpos($currentTheme,"&lt;/body&gt;");
$str = "{% include 'back-to-the-top' %}\n";
$currentTheme =  substr_replace( $currentTheme, $str, $pos, 0 ); 


This code fetches the store theme files and append my customize code into theme file.

I want to know that what is right way to do it. Please check it and tell me that this is right way to do it or not? Please tell me the right way to do it.
I haven't used shopify so I don't know if you can accomplish your task with fewer shopify calls, but I do find it a little odd that the URL in the second call requires redundant theme identification.  I mean, the theme id is in the path and the querystring. 

Outside of that, I spy some basic refinements.


As a general rule, avoid declaring single-use variables. Reasonable exceptions to this rule include when:


the merging of multiple processes/declarations results in a code line which exceeds a sensible length (horizontal scrolling is a drag and I generally obey PHPStorm's visual guide line when deciding when a line is too long) or
the data being processed/produced/declared is clarified by the variable name

Within a given project, determine if variables will be written in camelCase or snake_case.  Use one style consistently throughout.
There is no JSON_PRETTY_PRINT for json_decode() -- you are looking for the true parameter to make an array.  The outcome is the same, but it is a typo worth fixing to avoid future confusion.
Write a space between all comma-separated parameters in functions.


Application:

$themeList = shopify_call($token, $shop, "/admin/api/2019-07/themes.json", array(), 'GET');
$themeList = json_decode($themeList['response'], true);
$themeId = $themeList['themes'][0]['id'];
$themePath = "/admin/api/2019-07/themes/{$themeId}/assets.json?asset[key]=layout/theme.liquid&amp;theme_id={$themeId}";

$customTheme = shopify_call($token, $shop, $themePath, array(), 'GET');
$customTheme = json_decode($customTheme['response'], true);
$currentTheme = $customTheme['asset']['value'];
$pos = strpos($currentTheme, "&lt;/body&gt;");
$str = "{% include 'back-to-the-top' %}\n";
$currentTheme = substr_replace($currentTheme, $str, $pos, 0);


In conclusion, I reckon only the PRETTY_PRINTing, variable formatting, and the spacing should be changed.  The choice of how/when to declare data is mostly about personal taste.