craftcms / shopify

Synchronize and extend product data from your Shopify storefront.
MIT License
44 stars 25 forks source link

How to add additional webhooks #76

Closed johndwells closed 2 months ago

johndwells commented 11 months ago

Description

We need to synchronise Shopify collection information to Craft categories. In our custom module, we have registered a custom handler:

namespace modules\sitemodule;

use modules\sitemodule\handlers\CollectionHandler;
use Shopify\Webhooks\Registry;
use Shopify\Webhooks\Topics;
use yii\base\Module;

class SiteModule extends Module
{
  public function init() {
    Registry::addHandler(Topics::COLLECTIONS_CREATE, new CollectionHandler());
    Registry::addHandler(Topics::COLLECTIONS_UPDATE, new CollectionHandler());
    Registry::addHandler(Topics::COLLECTIONS_DELETE, new CollectionHandler());
  }
}

And then in CollectionHandler, we match for the topics we are interested in, and then call our service layer accordingly. It is written just like the plugin's own Product handler: https://github.com/craftcms/shopify/blob/develop/src/handlers/Product.php

Finally in Shopify, we have created Collection-specific webhooks, using the same Callback URL as the rest, e.g. https://domain.com/shopify/webhook/handle

I had hoped this would suffice - however, Collections do not seem to be synchronising. I'm trying to figure out why.

In the Shopify plugin's Webhooks section, for "Webhook Management", the docs say that we have to create these on a per-environment basis. This only appears to include the plugin's supported topics - products/create, products/update, products/delete, and inventory_levels/update. It doesn't give me any way to create others.

Is this where the problem lies? Will the plugin not support additional webhook handlers?

Additional info

lukeholder commented 2 months ago

Sorry for the delay.

Is this where the problem lies? Will the plugin not support additional webhook handlers?

Yes correct. Your plugin/module will need to register the webhooks itself (and make sure the API key has permissions to the resources you are listening to), and create the webhooks in your plugin/module.

You can see how to do this this here: https://github.com/craftcms/shopify/blob/develop/src/controllers/WebhooksController.php#L84-L109

Our webhook creation code is only for the features we support (and advise to make a limited API key for), so we require other plugins/modules to manually register manually with Shopify.

Thanks.