diff --git a/CHANGELOG.md b/CHANGELOG.md index bd301922f..e5d96c4e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Unreleased ---------- * Fix `add_webhook` generator to create the webhook jobs under the correct directory[#1748](https://github.com/Shopify/shopify_app/pull/1748) +* Add support for metafield_namespaces in webhook registration [#1745](https://github.com/Shopify/shopify_app/pull/1745) 21.8.1 (December 6, 2023) ---------- diff --git a/docs/shopify_app/webhooks.md b/docs/shopify_app/webhooks.md index eed665c27..b8da43fa8 100644 --- a/docs/shopify_app/webhooks.md +++ b/docs/shopify_app/webhooks.md @@ -40,6 +40,20 @@ ShopifyApp.configure do |config| end ``` +If you need to read metafields, you can pass in the `metafield_namespaces` parameter in `config/webhooks`. Note if you are also using the `fields` parameter you will need to add `metafields` into that as well. Shopify documentation on metafields in webhooks can be found [here](https://shopify.dev/docs/api/admin-rest/2023-10/resources/webhook#resource-object). + +```ruby +ShopifyApp.configure do |config| + config.webhooks = [ + { + topic: 'orders/create', + path: 'webhooks/order_create', + metafield_namespaces: ['app-namespace'], + }, + ] +end +``` + If you'd rather implement your own controller then you'll want to use the [`ShopifyApp::WebhookVerification`](/lib/shopify_app/controller_concerns/webhook_verification.rb) module to verify your webhooks, example: ```ruby diff --git a/lib/shopify_app/managers/webhooks_manager.rb b/lib/shopify_app/managers/webhooks_manager.rb index d02c71a2e..4ee17df67 100644 --- a/lib/shopify_app/managers/webhooks_manager.rb +++ b/lib/shopify_app/managers/webhooks_manager.rb @@ -52,6 +52,7 @@ def add_registrations path: webhook_path, handler: delivery_method == :http ? webhook_job_klass(webhook_path) : nil, fields: attributes[:fields], + metafield_namespaces: attributes[:metafield_namespaces], ) end end diff --git a/test/shopify_app/managers/webhooks_manager_test.rb b/test/shopify_app/managers/webhooks_manager_test.rb index 43195c61f..50e5eca4d 100644 --- a/test/shopify_app/managers/webhooks_manager_test.rb +++ b/test/shopify_app/managers/webhooks_manager_test.rb @@ -22,9 +22,10 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase path: "webhooks/orders_updated", handler: OrdersUpdatedJob, fields: nil, + metafield_namespaces: nil, } - ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(expected_hash).once + ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(**expected_hash).once ShopifyApp.configure do |config| config.webhooks = [ { topic: "orders/updated", path: "webhooks/orders_updated" }, @@ -40,9 +41,10 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase path: "/webhooks/orders_updated", handler: OrdersUpdatedJob, fields: nil, + metafield_namespaces: nil, } - ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(expected_hash).once + ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(**expected_hash).once ShopifyApp.configure do |config| config.webhooks = [ {