putyourlightson / craft-campaign

Send and manage email campaigns, contacts and mailing lists in Craft CMS.
https://putyourlightson.com/plugins/campaign
Other
63 stars 25 forks source link

User Group sync on existing users #402

Closed shifuma closed 1 year ago

shifuma commented 1 year ago

I have a module which is adding existing users to a group when they purchase a particular product. This group is then synced with a mailing list.

My understanding was that a user who's been added to the user group after purchase will be added to the mailing list. However, this is not happening. At the moment, I need to sync the group manually every time.

Is this the expected behaviour?

bencroker commented 1 year ago

My understanding was that a user who's been added to the user group after purchase will be added to the mailing list.

Yes, that’s a correct assumption. Are you running the latest version 2.8.3 of the plugin? And can you show me the code that your module uses to assign users to user groups?

shifuma commented 1 year ago

Thanks Ben. Yep on 2.8.3. Here's the relevant code:

$userGroups = [];

foreach ($user->getGroups() as $group) {
  $userGroups[] = $group->id;
}

$membersGroup = Craft::$app->getUserGroups()->getGroupByHandle('members')->id;
if (!$user->isInGroup($membersGroup)) {
  $userGroups[] = $membersGroup;
}

Craft::$app->users->assignUserToGroups($user->id, $userGroups);
Craft::$app->getElements()->saveElement($user);
bencroker commented 1 year ago

You code looks like it should trigger a user sync. Has the user group been synced to the mailing list before the user is assigned to a new group? And can you show me how you’re listening for the event that is triggered when the user purchases a product (in case this is a timing issue)?

shifuma commented 1 year ago

The group is synced and never removed. The next time a user signs makes a purchase I assume that sync should pick them up.

I'm listening to Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, static function (Event $event)

I hope that answers your questions properly.

screenshot_2023_07_28 _17 10

bencroker commented 1 year ago

Please share the full code so I can take a closer look.

shifuma commented 1 year ago

Sure, here you go. I'm using line item options to track users that a purchase is assigned to.

Thanks for your help.

Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, static function (Event $event) {
    $order = $event->sender;
    $purchaser = Craft::$app->getUser()->getIdentity();

    foreach ($order->lineItems as $item) {
        $options = $item->options;

        foreach ($options['id'] as $index => $userId) {
            $name = $options['name'][$index];
            $user = User::find()->id($userId)->one();
            foreach ($user->getGroups() as $group) {
                $userGroups[] = $group->id;
            }
            $membersGroup = Craft::$app->getUserGroups()->getGroupByHandle('members')->id;
            if (!$user->isInGroup($membersGroup)) {
                $userGroups[] = $membersGroup;
            }
            Craft::$app->users->assignUserToGroups($user->id, $userGroups);
            Craft::$app->getElements()->saveElement($user);
        }
    }
});
bencroker commented 1 year ago

All looks fine to me, I’m really not sure what the issue might be. Be sure to test this with the Pro edition of Campaign.

The sync user test is passing: https://github.com/putyourlightson/craft-campaign/blob/26d2ad152bbf63d331be68039f2076c37907d06e/tests/unit/services/SyncServiceTest.php#L47-L75

shifuma commented 1 year ago

Thanks for looking into it...I'm not sure either. I'll just sync manually from time to time then.

shifuma commented 1 year ago

Looking into it some more, I think it might be clashing with another module of mine, where I'm updating a field on Event::on(User::class, Element::EVENT_BEFORE_SAVE. Haven't been able to fully test, but resaving users seems to sync the group so I'm confident this is not a Campaign issue.

Thanks again for your time and help.

bencroker commented 1 year ago

Sure thing, good luck with troubleshooting.