Y
Y
Yuriy2020-09-29 11:24:41
1C-Bitrix
Yuriy, 2020-09-29 11:24:41

Bitrix Trigger mailings how to personalize by user groups?

Tell me, how can I limit the sending of letters by trigger mailing by user groups?

Created trigger mailing list Forgotten cart, should not be sent to users who belong to a certain group (Dealers).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2020-09-30
@yous

Option if you need to restrict all trigger mailings by the user's belonging to a user group

$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->addEventHandler('sender', 'OnBeforePostingSendRecipient', ['SenderEvents', 'OnBeforePostingSendRecipient']);

class SenderEvents {
  const USER_GROUP_DILLER_ID = 1;
  
  public static function OnBeforePostingSendRecipient($event) {
    $parameters = $event->getParameters()[0];
    $isUserDiller = self::userInGroup($parameters['FIELDS']['USER_ID'], self::USER_GROUP_DILLER_ID);
    
    if($isUserDiller) {
      $parameters['FIELDS']['EMAIL_TO'] = '';
    }
    
    return new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS, $parameters);
  }
  
  private static function userInGroup(int $userId = 0, $userGroupsId = []) {
    if($userId && $userGroupsId) {
      $userGroupsId = (array) $userGroupsId;
      $userGroups = CUser::GetUserGroup($userId);
      
      return array_intersect($userGroups, $userGroupsId) ? true : false;
    }
    else {
      return false;
    }
  }
}

Option if you want to limit certain triggers, such as "Forgotten Cart"
class SenderEvents {
  const USER_GROUP_DILLER_ID = 1;
  
  public static function OnBeforePostingSendRecipient($event) {
    $parameters = $event->getParameters()[0];
    
    $mailingChainId = $parameters['MAILING_CHAIN_ID'];
    $isUserDiller = self::userInGroup($parameters['FIELDS']['USER_ID'], self::USER_GROUP_DILLER_ID);
    
    if($mailingChainId && $isUserDiller) {
      $mailingTrigger = new \MailingTrigger($mailingChainId);
      
      if($mailingTrigger->isTriggerBasketForgotten()) {
        $parameters['FIELDS']['EMAIL_TO'] = '';
      }
    }

    return new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS, $parameters);
  }
  
  private static function userInGroup(int $userId = 0, $userGroupsId = []) {
    if($userId && $userGroupsId) {
      $userGroupsId = (array) $userGroupsId;
      $userGroups = CUser::GetUserGroup($userId);
      
      return array_intersect($userGroups, $userGroupsId) ? true : false;
    }
    else {
      return false;
    }
  }
}

class MailingTrigger {
  private $trigger;
  
  public function __construct(int $mailingChainId) {
    $this->setTrigger($mailingChainId);
  }
  
  public function getTrigger(int $mailingChainId) {
    return $this->trigger;
  }
  
  private function setTrigger(int $mailingChainId) {
    if(!isset($this->trigger)) {
      $this->trigger = \Bitrix\Sender\MailingTriggerTable::getList([
        'select' => ['ENDPOINT'],
        'filter' => ['MAILING_CHAIN_ID' => $mailingChainId, 'IS_TYPE_START' => 1]
      ])->fetch();
    }
  }
  
  public function getCode() {
    return $this->trigger['ENDPOINT']['CODE'];
  }
  
  public function isTriggerBasketForgotten() {
    $triggerBasketForgottenCode =  \Bitrix\Sale\Sender\TriggerBasketForgotten::getCode();
    
    return $this->getCode() == $triggerBasketForgottenCode ? true : false;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question