Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add snippet in theme.liquid in Shopify App (Using PUT api) #28

Open
gautam14 opened this issue Jan 18, 2017 · 7 comments
Open

How to add snippet in theme.liquid in Shopify App (Using PUT api) #28

gautam14 opened this issue Jan 18, 2017 · 7 comments

Comments

@gautam14
Copy link

Hi,
I am building an shopify app using phpish/shopify_app-skeleton, can anyone tell me how to add the snippet in theme.liquid when app is installed in store first time. I used this code

$shopify('PUT /admin/themes/145783113/assets.json', array(
'asset' => array(
'key' => 'layout/theme.liquid',
'value' => "hello";
) ));

But this code overwrite whole code and just append "hello" . Please help me in adding snippet and content to it.

Thanks

@myjanky
Copy link

myjanky commented Jan 18, 2017

Hi @gautam14
You will need to save a copy of the liquid file as a string then insert your code.
see this http://stackoverflow.com/questions/30883360/modify-theme-liquid-using-shopify-api

@gautam14
Copy link
Author

Hi myjanky

Thanks for your response.

I read this post and follow the way it tells
--first save a copy of the theme.liquid then used this code

$shopify('PUT /admin/themes/145783113/assets.json', array(
'asset' => array(
'key' => 'layout/theme.liquid',
'value' => "hello";
) ));

to append the data/value, but the main problem is that when I append my data in theme.liquid it overwrite all the previous data and just append "hello" in file.

Can you please tell how I can append my data after/before the particular data in theme.liquid file.

Thanks!

@myjanky
Copy link

myjanky commented Jan 19, 2017

The value must contain the entire liquid of the theme.liquid file as well as your "Hello" wherever you want it to be at. For example if the contents of theme.liquid are

<head>
     <body>My Theme</body>
</head>

You would grab a copy of the theme liquid with the asset API get.
Pseudocode...

var current_theme =  '<head>
     <body>My Theme</body>
</head>'; //possibly from the get asset var. 

var new_theme = jQuery(current_theme).find('body').append('Hello');

then pass new_theme in as the value parameter in the put

@gautam14
Copy link
Author

gautam14 commented Jan 20, 2017

Hi myjanky,

I have tried the code but getting the desired result.

--my code is:

"<?php
$mycustom = $shopify('GET /admin/themes/145783113/assets.json?asset[key]=layout/theme.liquid&theme_id=145783113');
$me = $mycustom['value'];

?>"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>	
	$(document).ready(function(){	
	var current_theme =  "<?php echo $me; ?>"; //possibly from the get asset var. 
    var new_theme = jQuery(current_theme).find('body').append('<h1>Hello</h1>');
	//console.log(new_theme);	
})
	</script>

"<?php //////// code /////////?>"

But "hello" is not appending in code. I just the beginner, can you please tell where I am doing wrong.

Thanks!

@myjanky
Copy link

myjanky commented Jan 20, 2017

I am not sure that JS is correct. Do you have the console errors from console.log?
What does the content of $me look like dumped to the console?

@gautam14
Copy link
Author

Hi myjanky,

I don't know whats going wrong, but no error is showing in console.log. Can you please help me in this, I just want modify theme.liquid using API.

Thanks!

@myjanky
Copy link

myjanky commented Jan 23, 2017

I would not mix PHP and JS for this. I was thinking you would use one or the other.

printr($me);

what is in that?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants