How to Update or Create multiple Assets for a Theme

loitn
Shopify Partner
4 0 0

Currently, I use foreach to create or update asset one by one. It takes a lot of time to do it.

Is there a way to make it at once?

 
foreach (ShopifyConsts::ASSET_LIST as $asset) {
     $publicUrl = self::createOrUpdateAsset($session, $themeId, $asset['key'], $asset['path']);
}
public function createOrUpdateAsset(Session $session, $themeId, $key, $path)
{
    $asset = new Asset($session);
    $asset->theme_id = $themeId;
    $asset->key = $key;
    $asset->value = file_get_contents($path);
    $asset->save(
        true,
    );
    return $asset->public_url;
}
 
Replies 2 (2)

EcomGraduates
Shopify Partner
588 48 77

there is a way to update multiple assets at once using the Shopify API. You can use the Asset::putMany() method to update multiple assets in a single API call. Here is an example code snippet that shows how to use this method:

 

$assets = [];

foreach (ShopifyConsts::ASSET_LIST as $asset) {
    $assets[$asset['key']] = file_get_contents($asset['path']);
}

Asset::putMany($session, $themeId, $assets);

 

In this code snippet, we first loop through all the assets in the ASSET_LIST array and read their contents into a $assets array using the asset key as the array key. We then pass this array to the Asset::putMany() method along with the session and theme ID to update all the assets in a single API call.

 

loitn
Shopify Partner
4 0 0
Call to undefined method Shopify\Rest\Admin2023_04\Asset::putMany()
I can't find putMany function, I just find function delete and all in class Asset.
Thanks for your reply