PUT new file under 'Sections'

Highlighted
New Member
2 0 0

Hi, I am trying to use Shopify API to create a some new files 'list-collections-template-myshop.liquid' under my theme's coding tree under 'Sections'

However,  i can successfully add new files under 'Templates'  using the Api  Assets PUT request:

PUT /admin/api/2020-04/themes/828155753/assets.json

 I can not add new files under 'Sections' using the same request

Any advice will really help

Thanking you in advance

0 Likes
Highlighted
Shopify Partner
2294 116 353

what is the actual request body in both cases?

check spelling and structure https://shopify.dev/tutorials/develop-theme-templates#theme-structure

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
1 Like
Highlighted
New Member
2 0 0

Hi Paul and thank you for your response, here is the code:

 

$data = array (
  array (
  'asset' => 
  array (
    'key' => 'sections/list-collections-template-jumbo.liquid',
    'value' => $section_value
  )
)
);

# Form data string
$postString = http_build_query($data, '', '&');

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://token+shop/admin/api/2020-04/themes/82368921647/assets.json'); 
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
// Set the content type to application/json
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

$result = curl_exec($ch);
					
if (curl_errno($ch)) {
    // this would be your first hint that something went wrong
    die('Couldn\'t send request: ' . curl_error($ch));
} else {
    // check the HTTP status code of the request
    $resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($resultStatus != 200) {
        die('Request failed: HTTP status code: ' . $resultStatus);
    }
}

curl_close($ch);

 In Variable '$sections_value' i include the content of the theme Section code file 'list-collections-template.liquid' in order to create a new identical file under Sections.

0 Likes
Highlighted
Shopify Partner
2294 116 353

Assuming token+shop in CURLOPT_URL is a placeholder and not in production.

Nothing obviously wrong stands out nor does simple linting show anything.

 

Is $section_value a valid section file with it's schema etc? Try duplicating an existing section file with a rename.

you can get see theme errors by manually adding it a development stores theme code editor to get   feedback, or try themekit

 

You could also check what happens if you use source_key to copy an existing asset.

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
1 Like