Hello Shopify Community,
I am currently working on a Shopify app and trying to update the config/settings_data.json file, specifically to change the settings of a block in the theme. I have been able to retrieve the current settings data, but I’m facing issues when trying to update it.
Here’s a brief overview of what I’m doing:
-
Fetch the Current Theme: I’m using the API to fetch the current theme and its associated settings_data.json file.
-
Update the Settings: I modify the settings for a specific block (e.g., changing the disabled status and updating other settings) in the fetched data.
-
Save the Updated Data: I attempt to save the updated settings_data.json, but it does not reflect the changes in the Shopify admin.
Here’s the code snippet I’m using:
export async function AppEnableDisable(admin, session, data) {
const EnableDisable = data.EmbedApp === ‘true’ ? true : false;
let DataTheme = null;
try {
const themes = await admin.rest.resources.Theme.all({
session: session,
});
const currentTheme = themes.data.find(theme => theme.role === ‘main’);
if (!currentTheme) {
throw new Error(“No main theme found”);
}DataTheme = await admin.rest.resources.Asset.all({
session: session,
theme_id: currentTheme.id,
asset: { “key”: “config/settings_data.json” },
});
if (!DataTheme) {
throw new Error(“Failed to fetchsettings_data.json”);
}
DataTheme = JSON.parse(DataTheme.data[0].value);let blockFound = false;
for (const blockKey in DataTheme.current.blocks) {
const block = DataTheme.current.blocks[blockKey];if (block.type.includes(‘xxx/blocks/extensionid’)) {
block.disabled = EnableDisable;
block.settings = {
…block.settings
};
blockFound = true;break; // Exit the loop once the block is found and updated
}
}if (!blockFound) {
throw new Error(“No block found with type 'my extension '”);
}
const updatedSettingsData = DataTheme;
const asset = new admin.rest.resources.Asset({ session: session });
asset.theme_id = currentTheme.id
asset.key = ‘config/settings_data.json’;
asset.value = JSON.stringify(updatedSettingsData);
const response = await asset.save({
update: false,});
if (response.errors) {
throw new Error(Failed to update settings_data.json: ${JSON.stringify(response.errors)});
}return { success: true, message: “Settings updated successfully.”, response };
// return response;
} catch (error) {
console.error(“Error fetching asset:”, error);
return { error: “Error during app enable/disable”, details: error.message };
}
}