From 89a96214e812e947a85fcefe220f5b0707e3abe4 Mon Sep 17 00:00:00 2001 From: Marco Arduini Date: Tue, 8 Oct 2024 22:29:06 +0200 Subject: [PATCH] Write tag when product is edited --- app/graphql/add_tags.rb | 22 ++++++++++++++++++++++ app/jobs/products_update_job.rb | 2 ++ config/initializers/shopify_app.rb | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 app/graphql/add_tags.rb diff --git a/app/graphql/add_tags.rb b/app/graphql/add_tags.rb new file mode 100644 index 0000000..44da4ec --- /dev/null +++ b/app/graphql/add_tags.rb @@ -0,0 +1,22 @@ +class AddTags + include ShopifyGraphql::Query + + MUTATION = <<~GRAPHQL + mutation AddTags($id: ID!, $tags: [String!]!) { + tagsAdd(id: $id, tags: $tags) { + node { + id + } + userErrors { + message + } + } + } + GRAPHQL + + def call(id:, tags:) + response = execute(MUTATION, id: id, tags: tags) + response.data = response.data.tagsAdd + response + end +end diff --git a/app/jobs/products_update_job.rb b/app/jobs/products_update_job.rb index 0055259..228ede8 100644 --- a/app/jobs/products_update_job.rb +++ b/app/jobs/products_update_job.rb @@ -20,6 +20,8 @@ def perform(topic:, shop_domain:, webhook:) shop.with_shopify_session do puts "products/update received, payload #{webhook}" + order_gid = webhook["admin_graphql_api_id"] + AddTags.call(id: order_gid, tags: [ "processed-by-rails-webhook" ]) end end end diff --git a/config/initializers/shopify_app.rb b/config/initializers/shopify_app.rb index 0742683..72e73a2 100644 --- a/config/initializers/shopify_app.rb +++ b/config/initializers/shopify_app.rb @@ -1,7 +1,7 @@ ShopifyApp.configure do |config| config.application_name = "My Shopify App" config.old_secret = "" - config.scope = "read_products,write_discounts" # Consult this page for more scope options: + config.scope = "write_products" # Consult this page for more scope options: # https://help.shopify.com/en/api/getting-started/authentication/oauth/scopes config.embedded_app = true config.new_embedded_auth_strategy = false