Skip to content

Instantly share code, notes, and snippets.

@remy727
Last active May 30, 2024 16:47
Show Gist options
  • Save remy727/dd75d94fea2898a43e7f865aa2abcea5 to your computer and use it in GitHub Desktop.
Save remy727/dd75d94fea2898a43e7f865aa2abcea5 to your computer and use it in GitHub Desktop.
Detect app embed blocks in Shopify Theme in Rails
# Documentation: https://shopify.dev/docs/apps/build/online-store/theme-app-extensions/configuration#detecting-app-blocks-and-app-embed-blocks
module ShopifyApiWrapper
class Theme
class << self
def app_embed_activated?(session:, theme_app_extension_id:)
new(session: session)
.app_embed_activated?(theme_app_extension_id: theme_app_extension_id)
end
end
def initialize(session:)
@session = session
end
def client
@client ||= ShopifyAPI::Clients::Rest::Admin.new(session: @session)
end
def app_embed_activated?(theme_app_extension_id:)
response = client.get(path: "themes")
# Retrieve main theme
main_theme = response.body["themes"].find { |theme| theme["role"] == "main" }
# Retrieve settings_data.json for main theme
response = client.get(path: "themes/#{main_theme["id"]}/assets", query: {
"asset[key]": "config/settings_data.json",
})
content = response.body.dig("asset", "value")
parsed_content = JSON.parse(content)
current_setting = parsed_content["current"]
# For new fresh installed store
# {"current"=>"Default", ...}
return false unless current_setting.is_a?(Hash)
blocks = current_setting["blocks"]
# Detecting app embed blocks
blocks.any? do |_block_unique_id, setting|
setting["type"].include?("blocks/widget/#{theme_app_extension_id}") &&
!setting["disabled"]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment