Add JavaScript code in theme.liquid with out changing exiting code of theme.liquid

Highlighted
Shopify Partner
2 0 0

Hello Devs, 

Actually I want to add JavaScript code in theme.liquid using Asset API, But while editing of theme.liquid,I got http 200 ok and and Javascript code added in theme.liquid but previous exiting code of theme.liquid was totally remove and my new javascript code is added & I don't want to do that.
I just want to add javascript line code in <head> section with existing theme.liquid code. 
Also I am attacing snapshot and API endpoint and sample json submitting.

  1. API Endpoint Name :- https://testing007ga.myshopify.com/admin/themes/7148240924/assets.json 
  2. API doucmentation :- https://help.shopify.com/api/reference/asset#update
  3. API key :- 2848e26cb8737f320af05439fdc510eb
  4. Shop Name :- https://testing007ga.myshopify.com
  5. Method :- Put
  6. Header :-
    1. Content-Type : application/json
    2. X-Shopify-Access-Token :  9f702bc996103d932c989ecdf0824a85
  7. Input Sample Json :- {"asset": {"key": "layout/theme.liquid","value": "{{ content_for_header }}}<script>console.log('foo')</script>{{ content_for_layout }}"    }}
  8. Http Status code :- 200
  9. OutPut Json :- {"asset": {"key": "layout\/theme.liquid","public_url": null,"created_at": "2018-01-05T01:05:07-05:00","updated_at": "2018-01-07T11:41:44-05:00","content_type": "text\/x-liquid","size": 485,"theme_id": 7148240924,"warnings": []}}

 

1.Image Before Asset API called, theme.liquid context code.

 

2.After Assest API called previous code is removed and My JavaScript Code added 

 

Hopefully Above information which is giving by me  it will sufficient.Please help me in these matter.
Last Remember , I just want to add javascript code with exiting theme.liquid in <head> section.

Waiting your reply as soon as possbile. 

Thanks & regards

 

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1129 82 179

Hey Nisarg, 

When you make a PUT request, whatever you send in the body of your API call will replace what is already there - this is normal and intentional behaviour though and what you're seeing is expected.

What you're trying to accomplish here is possible, however you'd have to fetch the current theme.liquid content first, then insert your code after the <head> tag programmatically somehow, and make your PUT request after that with all of the content combined at the same time. You won't be able to only send in the bits and pieces of information that you need to add. 

1 Like
Highlighted
Shopify Partner
2 0 0

Hi Josh Wheeler,

Thanks for reply, I understand your explanation for these.

Thanks.

0 Likes
Highlighted
New Member
6 0 0

Hi  Nisarg Raval

Please help .Can you tell me solution . how got your desired output, i am facing same problem. im using put method.its overwrite file. i want to edit any file. 

$addscript = $shopify('PUT /admin/themes/'.$themes[0]['id'].'/assets.json',
                        array(
                            "asset" => array(
                                "key" => "assets/testing.js",
                                "value" => "{{ 'jquery-1.4.3.min.jsor' | asset_url | script_tag }}",
                                
                             )
                        )
                    );
        
    $all_assets = $shopify('GET /admin/themes/'.$themes[0]['id'].'/assets.json');
    print_r($all_assets); die('k');
    }

ThankYou

0 Likes
Highlighted
New Member
3 0 0

Has this issue resolved? I also have the same problem. Thank you.

0 Likes