1

I have the following code working. The only thing i have to do is to check which usergroup is the customer in. Is there a simple way to check what usergroup is the customer in? This is my first custom module.

Event::on(
LineItems::class,
LineItems::EVENT_POPULATE_LINE_ITEM,
function(LineItemEvent $event) {

    // @var LineItem $lineItem
    $lineItem = $event->lineItem;
    
    // @var bool $isNew
    $isNew = $event->isNew;
    
    $purchasable = $lineItem->getPurchasable();
    $variant = $purchasable->getProduct();

    // IF GUEST USER  > NORMAL PRICE
    $lineItem->salePrice = $lineItem->getSalePrice();
    $lineItem->price = $lineItem->getPrice();

    // IF USER IS IN DEALER USERGROUP > DEALER PRICE
    $lineItem->salePrice = $variant->dealerPrice;
    $lineItem->price = $variant->dealerPrice;
    
}

);

2

Remembering a) that not all customers are users and b) that a user can be in one, or more, groups.....then something like this can be used to check if a user is in a particular group:

            $order = $lineItem->getOrder();
            $user = Craft::$app->users->getUserByUsernameOrEmail($order->email);
            if ($user){
                foreach ($user->getGroups() as $group) {
                    if ($group->id == someParticularValue){
                        //do stuff
                    }
                }
            }
1
  • Thanks Jermey for helping me on the right track! This is exactly what i needed.
    – R14
    Jan 19 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.