Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
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; }
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.
If this fixed your issue, likes and accepting as a solution are highly appreciated.
Build an online presence with our custom built Shopify Theme EcomifyTheme