Created
February 5, 2025 09:43
-
-
Save remy727/85b773507aa9f761c57b55085b12249a to your computer and use it in GitHub Desktop.
Detect the presence of theme app embed block in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | |
return false if blocks.blank? | |
# Detecting theme 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