mediabeastnz / craft-commerce-abandoned-cart

Abandoned Cart plugin for Craft Commerce 2
Other
1 stars 15 forks source link

[FR] Fire Events #35

Closed billythekid closed 2 years ago

billythekid commented 4 years ago

I was wondering if we could maybe get a couple of events fired from the plugin, in particular when an AbandonedCart is save()d so that we can hook into this and prevent the save (or amend it)

My particular use-case is that my automated testing of the checkout process on the production server is creating abandoned carts (because we're not completing them) and this is muddying the conversion numbers. Ideally I could listen to the onBeforeSave event for the test email address or whatever and prevent the abandoned cart being created.

I was thinking the abandoned cart models may have been elements and inherited this automatically but had a quick look in the code and see they're not.

mediabeastnz commented 4 years ago

@billythekid 😬 I'm actually in the process of making them into Elements as they come with lots of features, it's just a lot of work to implement - it's in progress though!

In the meantime I can easily add a few events like the one you mention.

An alternative to this is I add the feature that allows you to "blacklist" emails from triggering the plugin?

billythekid commented 4 years ago

Yeah I think that might be a good idea for non-devs too. That way they could prevent their own tests from screwing with the numbers.

It would definitely meet my use-case above.

mediabeastnz commented 4 years ago

Easy, leave it with me

mediabeastnz commented 4 years ago

Released in version 1.5.3 ✌️

You can add a comma separated list of emails under settings and they will be ignored.

billythekid commented 4 years ago

Nice one, cheers!

billythekid commented 4 years ago

Heya @mediabeastnz,

I'm still getting abandoned carts for blacklisted email addresses. Can you take a look please?

Screenshot 2020-04-16 at 18 02 26 Screenshot_2020-04-16_at_17_55_48
joshuapease commented 2 years ago

Some events would be super helpful for a client need that we have.

Manually adding emails to the blacklist isn't an option for our client, so we're hoping to build a an automated way for users to add their emails to a list (we'd build our own custom controllers / records to store the blocked emails).

If we had access to some events before save or email send, we could check our custom blacklist and prevent saving or sending from happening.

Wanted to see if this is something you've already made progress on, or if you're open to a possible PR (I "might" be able to free up some time at work).

Appreciate your plugin. Cheers!

joshuapease commented 2 years ago

It dawned on me that records emit events.

I'm going to attempt creating my own dynamic list of blocked emails and invalidate the AbandonedCart record if exists in the list.

use mediabeastnz\abandonedcart\records\AbandonedCart as AbandonedCartRecord;

Event::on(
    AbandonedCartRecord::class,
    AbandonedCartRecord::EVENT_BEFORE_INSERT,
    function (ModelEvent $event) {
        // TODO invalidate if the record email
        // matches our list of blocked emails.
        $event->isValid = false;
    }
);