Why does my custom JS file load multiple times in Shopify?

Hello Folk,

Hope you all are well !!

I’m facing an issue with shopify_call. I want to call my custom js file on order status but so I used script_tag API but, it called multiple times so any idea about that. Really need to fix it.Could anyone help me out from this issue
My code

$array = array(
		'script_tag' => array( 
			"event" => "onload",
			'src' => "order.js",
			'display_scope' => "order_status"
		)
	);
  
    $scriptTag = shopify_call($token, $shop, "/admin/api/2021-10/script_tags.json", $array, 'PUT');

Hi @tecoektamerai ,
You need to create script tag only once. Are checking in code that script tag is created or not? otherwise, everytime you open APP, it will create new

Hello @Jivan_Suhagiya

Oh Now
Can I delete the script using the below ‘DEL’ API?

$webhook = shopify_call($token, $shop, "/admin/api/2022-01/script_tags/script_id.json", array(), 'DEL');

yes, you can delete
method is ‘DELETE’ in REST API

Ok, Thank you @Jivan_Suhagiya for your time.
Have a good day

Hello @Jivan_Suhagiya

I have checked this using GET method of the script tag api and set condition if script tag is created less then 1 then create script tag else it will not create.
Now, when i install app first time in my dev store it is create two script at same time after that whenever i reload APP its not create next script, I want to stop to create two scripts at same time when installing APP.
Can you please check my code and help me to fix this issue.
Here is the code

$scriptTagList = shopify_call($access_token, $shop, "/admin/api/2022-04/script_tags.json", array(), 'GET');
$scriptTagList = json_decode($scriptTagList['response'], JSON_PRETTY_PRINT);

$countScriptTag = count($scriptTagList['script_tags']);

if ($countScriptTag < 1) {
	$scriptTag = shopify_call($access_token, $shop, "/admin/api/2022-04/script_tags.json", $scriptArray, 'POST');
	$scriptTag = json_decode($scriptTag['response'], JSON_PRETTY_PRINT);
	$counter++;

}

Hi Ankit
I have tried this way and its working fine. Let me know if you have better solution then this.

$tagArray = array(

"script_tag" => array(

"src" => "https://xyz.co.uk/apps/example_app/inc/js/ahirwp.js",
"event" => "onload",
"created_at" => "2022-10-03T12:46:20-04:00",
"updated_at" => "2022-10-03T12:46:20-04:00",
"display_scope" => "all",
"cache" => "false"
)
);

$ScriptTag = shopify_call($token, $shop, "/admin/api/2022-10/script_tags.json", $tagArray, 'GET');
$ScriptTag = json_decode($ScriptTag['response'], JSON_PRETTY_PRINT);
$countScriptTag = count($scriptTagList['script_tags']);

if ($countScriptTag = 0 || empty($countScriptTag)) {

$ScriptTag = shopify_call($token, $shop, "/admin/api/2022-10/script_tags.json", $tagArray, 'POST');
$ScriptTag = json_decode($ScriptTag['response'], JSON_PRETTY_PRINT);
echo "

~~~
";print_r($ScriptTag);

}

Hi @ahirwp ,

Thanks for your help, I have tried this way but its not working. It is still create two scripts at same time in POST method first time when to install APP.

Hi @shop_dev1
I am working on one of the private app. i will check same scenario in my private app and will let you know if that works or not. Stay connected.

Thanks

Ahir Hemant

Hi @ahirwp ,
Hope you are doing well !

I Wish that you have checked this scenario in your private APP and let me know better solution than this.

Thanks

Hi @shop_dev1
Yes doing good, Thanks :slightly_smiling_face:

// Delete script tag API call
$scriptTagList = shopify_call($token, $shop, "/admin/api/2022-04/script_tags.json", array(), 'GET');
$scriptTagList = json_decode($scriptTagList['response'], JSON_PRETTY_PRINT);

foreach($scriptTagList as $scriptTagLists){
foreach ($scriptTagLists as $key => $script_tag_id) {
$deleteScript = shopify_call($token, $shop, "/admin/api/2022-10/script_tags/".$script_tag_id['id']. ".json", array(), 'DELETE');
$deleteScript = json_decode($deleteScript['response'], JSON_PRETTY_PRINT);
print_r($deleteScript);

}
}

I hope you have deleted scripts first. if not then run above code and then comment above code once deleted. then please install your app again.

I did like below and works fine for me.

// Insert/GET script tag API call
$ScriptTag = shopify_call($token, $shop, "/admin/api/2022-10/script_tags.json", $tagArray, 'GET');
$ScriptTag = json_decode($ScriptTag['response'], JSON_PRETTY_PRINT);

$countScriptTag = count($ScriptTag['script_tags']);

if ($countScriptTag = 0 || empty($countScriptTag)) {

$ScriptTag = shopify_call($token, $shop, "/admin/api/2022-10/script_tags.json", $tagArray, 'POST');
$ScriptTag = json_decode($ScriptTag['response'], JSON_PRETTY_PRINT);

}

Let me know how it goes.

Thanks
Ahir Hemant