laravel-shopify icon indicating copy to clipboard operation
laravel-shopify copied to clipboard

Adding event to notify App Installation on the store

Open abishekrsrikaanth opened this issue 1 year ago • 9 comments

The AppInstalled event will be triggered after the installation of App on the store. This can be used to run jobs after successful installation. For example, fetching data from the store that is needed by the application.

Here is an example of how to set up a Listener and subscribe to it on the project's EventServiceProvider.


<?php

namespace App\Listeners;

use Osiset\ShopifyApp\Messaging\Events\AppInstalled;

class AppInstalledEventListener
{
    /**
     * Handle AppInstalled Event.
     *
     * @param  Osiset\ShopifyApp\Messaging\Events\AppInstalled $event
     * @return void
     */
    public function handle(AppInstalled $event)
    {
            $shop = $event->getShop();
            // Get Shop Details
            // Get Products
            // Get Collections
            // etc...
    }
}

Once your listener has been defined, you may register it within your application's EventServiceProvider:

<?php

namespace App\Providers;

use App\Listeners\AppInstalledEventListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Osiset\ShopifyApp\Messaging\Events\AppInstalled;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        AppInstalled::class => [
            AppInstalledEventListener::class,
        ],
    ];
}

@Kyon147, I am resending this PR again as the last PR (#1058) had additional code that was already merged in to the repo.

abishekrsrikaanth avatar Sep 26 '22 10:09 abishekrsrikaanth

Hi @abishekrsrikaanth

Thanks for submitting a PR. Could you let me know how you would use this differently to the AfterAuthenticate job that already exists in the package which can be used for the same thing.

Thanks!

Kyon147 avatar Oct 04 '22 07:10 Kyon147

Also, need to add the AppUninstalled, PlanPurchased, and PlanChanged events.

@osiset and @Kyon147

nahid avatar Oct 04 '22 12:10 nahid

@nahid I think Kyron147 was asking how does it differ from AfterAuthenticate, are you saying we should deprecate AfterAuthenticate in favor of these events? If so, let us know :) and if thats the case, we would have to update documentation and make a major release for these events as it would break existing apps depending on if they use AfterAuthenticate.

gnikyt avatar Oct 04 '22 15:10 gnikyt

@osiset Thanks, got it. I think the AfterAuthenticate job feature should be deprecated in the next version but legacy support is required up to a certain version. It can be configurable. If someone wants to use the legacy feature he/she didn't need to configure anything but if someone is want to use the Event Listener feature they need to configure it.

In my previous comment I meant that we need to add AppUninstalled, PlanPurchased, and PlanChanged events besides the AppInstalled event. Because sometimes App owners need to notify the shop owners of these events.

nahid avatar Oct 05 '22 05:10 nahid

@nahid Sounds fine to me, if you would like to add those additional events and some jotnotes on how to use the events so we can form a wiki entry for people, that would be great. We can then review and merge in for a major release.

gnikyt avatar Oct 05 '22 12:10 gnikyt

Thank you @osiset, I'll make a PR with these changes ASAP.

nahid avatar Oct 06 '22 05:10 nahid

@nahid One thing to note for appinstalled, is that there will need to be an option to run it "inline" and not later somehow. Some people require changes to be made before the shop user sees the app (example, set some flag in the database).

gnikyt avatar Oct 06 '22 17:10 gnikyt

@nahid One thing to note for appinstalled, is that there will need to be an option to run it "inline" and not later somehow. Some people require changes to be made before the shop user sees the app (example, set some flag in the database).

@osiset Given that this is not a job being dispatch but an event, then note that events are always synchronous. The listeners one might register for these events on the other hand can be either queued or synchronous. So "inline" is essentially build into the implementation using events, making it a really nice solution for any use case 👍

bilfeldt avatar Oct 20 '22 01:10 bilfeldt

@bilfeldt exactly, if someone wants to run Listener immediately, just ignore the ShouldQueue implementation

nahid avatar Oct 20 '22 01:10 nahid