<p>I've not used Events, but I would presume they extend the Purchasable class as per <a href="https://craftcms.com/docs/commerce/3.x/extend/purchasable-types.html" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/3.x/extend/purchasable-types.html</a></p>
<p>Seems like therefore you could use PHP's <code>instanceof</code> (<a href="https://www.php.net/manual/en/language.operators.type.php" rel="nofollow noreferrer">https://www.php.net/manual/en/language.operators.type.php</a>) to check the type of</p>
<p><code>$purchasable = $item-&gt;getPurchasable();</code></p>
<p>...and create two paths from there depending on what type of line item you're dealing with one each occasion.</p>

CC BY-SA 4.0
2021-09-06T22:43:09.333Z
2021-09-06T22:43:09.333Z
Jeremy Daalder
<p>I have a module to add a user to a group after a specific product type purchase. This was working fine, until I added <a href="https://verbb.io/craft-plugins/events/features" rel="nofollow noreferrer">Events</a> to the mix. From what I understand, Events don't have variants so now I'm having issues.</p>
<p>Here is my code (it's probably very messy but it's worked so far):</p>
<pre><code>Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $event) {
  $order = $event-&gt;sender;
  $membersGroup = [&quot;1&quot;];
  $user = Craft::$app-&gt;getUsers()-&gt;getUserById($order-&gt;customer-&gt;userId);
  foreach ($order-&gt;lineItems as $item) {
    $purchasable = $item-&gt;getPurchasable();
    $variant = $purchasable-&gt;getProduct();
    $productType = $variant-&gt;getType();
    if ($productType-&gt;id == &quot;3&quot;) {
      foreach ($user-&gt;getGroups() as $group) {
        array_push($membersGroup, $group-&gt;id);
      }
      Craft::$app-&gt;users-&gt;assignUserToGroups($order-&gt;customer-&gt;userId, $membersGroup);
    }
  }
});
</code></pre>
<p>As it stands, a regular Commerce product will process without issue. Purchasing an Event ticket will process ok, but the user is taken back to the payment page.</p>
<p>As soon as I comment out <code>$variant = $purchasable-&gt;getProduct();</code> and change <code>$productType = $variant-&gt;getType();</code> to <code>$productType = $purchasable-&gt;getType();</code>, the opposite happens (ticket is successful, regular product is not.)</p>
<p>Does any one have any thoughts on how I could make this work in both scenarios?</p>

<plugin-craftcommerce>
Add users to a group upon purchase [check if a purchasable has a variant]
# Add users to a group upon purchase [check if a purchasable has a variant]

(Asked by: username_0 on 2021-09-05)

I have a module to add a user to a group after a specific product type purchase. This was working fine, until I added Events to the mix. From what I understand, Events don't have variants so now I'm having issues.
Here is my code (it's probably very messy but it's worked so far):
Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $event) {
  $order = $event->sender;
  $membersGroup = ["1"];
  $user = Craft::$app->getUsers()->getUserById($order->customer->userId);
  foreach ($order->lineItems as $item) {
    $purchasable = $item->getPurchasable();
    $variant = $purchasable->getProduct();
    $productType = $variant->getType();
    if ($productType->id == "3") {
      foreach ($user->getGroups() as $group) {
        array_push($membersGroup, $group->id);
      }
      Craft::$app->users->assignUserToGroups($order->customer->userId, $membersGroup);
    }
  }
});

As it stands, a regular Commerce product will process without issue. Purchasing an Event ticket will process ok, but the user is taken back to the payment page.
As soon as I comment out $variant = $purchasable->getProduct(); and change $productType = $variant->getType(); to $productType = $purchasable->getType();, the opposite happens (ticket is successful, regular product is not.)
Does any one have any thoughts on how I could make this work in both scenarios?

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2021-09-06. Score: 1

I've not used Events, but I would presume they extend the Purchasable class as per <https://craftcms.com/docs/commerce/3.x/extend/purchasable-types.html>

Seems like therefore you could use PHP's `instanceof` (<https://www.php.net/manual/en/language.operators.type.php>) to check the type of

`$purchasable = $item->getPurchasable();`

...and create two paths from there depending on what type of line item you're dealing with one each occasion.


---

