Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Subscriber Class Reference
Inheritance diagram for Subscriber:
AbstractModel DataObject

Public Member Functions

 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Newsletter\Helper\Data $newsletterData, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Customer\Model\Session $customerSession, CustomerRepositoryInterface $customerRepository, AccountManagementInterface $customerAccountManagement, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], \Magento\Framework\Stdlib\DateTime\DateTime $dateTime=null, CustomerInterfaceFactory $customerFactory=null, DataObjectHelper $dataObjectHelper=null)
 
 getId ()
 
 setId ($value)
 
 getCode ()
 
 getConfirmationLink ()
 
 getUnsubscriptionLink ()
 
 setCode ($value)
 
 getStatus ()
 
 setStatus ($value)
 
 setMessagesScope ($scope)
 
 getEmail ()
 
 setEmail ($value)
 
 setStatusChanged ($value)
 
 isStatusChanged ()
 
 isSubscribed ()
 
 loadByCustomerId ($customerId)
 
 randomSequence ($length=32)
 
 subscribe ($email)
 
 unsubscribe ()
 
 subscribeCustomerById ($customerId)
 
 unsubscribeCustomerById ($customerId)
 
 updateSubscription ($customerId)
 
 confirm ($code)
 
 received (\Magento\Newsletter\Model\Queue $queue)
 
 sendConfirmationRequestEmail ()
 
 sendConfirmationSuccessEmail ()
 
 sendUnsubscriptionEmail ()
 
 getSubscriberFullName ()
 
 beforeSave ()
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Data Fields

const STATUS_SUBSCRIBED = 1
 
const STATUS_NOT_ACTIVE = 2
 
const STATUS_UNSUBSCRIBED = 3
 
const STATUS_UNCONFIRMED = 4
 
const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template'
 
const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity'
 
const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template'
 
const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity'
 
const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template'
 
const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity'
 
const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm'
 
const XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG = 'newsletter/subscription/allow_guest_subscribe'
 

Protected Member Functions

 _construct ()
 
 _updateCustomerSubscription ($customerId, $subscribe)
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_eventPrefix = 'newsletter_subscriber'
 
 $_eventObject = 'subscriber'
 
 $_isStatusChanged = false
 
 $_newsletterData = null
 
 $_scopeConfig
 
 $_customerSession
 
 $_storeManager
 
 $customerRepository
 
 $customerAccountManagement
 
 $_transportBuilder
 
 $inlineTranslation
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Subscriber model

@method int getStoreId() @method $this setStoreId(int $value) @method string getChangeStatusAt() @method $this setChangeStatusAt(string $value) @method int getCustomerId() @method $this setCustomerId(int $value) @method string getSubscriberEmail() @method $this setSubscriberEmail(string $value) @method int getSubscriberStatus() @method $this setSubscriberStatus(int $value) @method string getSubscriberConfirmCode() @method $this setSubscriberConfirmCode(string $value) @method int getSubscriberId() @method Subscriber setSubscriberId(int $value)

@SuppressWarnings(PHPMD.CouplingBetweenObjects) @SuppressWarnings(PHPMD.CyclomaticComplexity)

@api

Since
100.0.2

Definition at line 40 of file Subscriber.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\Context  $context,
\Magento\Framework\Registry  $registry,
\Magento\Newsletter\Helper\Data  $newsletterData,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Framework\Mail\Template\TransportBuilder  $transportBuilder,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Customer\Model\Session  $customerSession,
CustomerRepositoryInterface  $customerRepository,
AccountManagementInterface  $customerAccountManagement,
\Magento\Framework\Translate\Inline\StateInterface  $inlineTranslation,
\Magento\Framework\Model\ResourceModel\AbstractResource  $resource = null,
\Magento\Framework\Data\Collection\AbstractDb  $resourceCollection = null,
array  $data = [],
\Magento\Framework\Stdlib\DateTime\DateTime  $dateTime = null,
CustomerInterfaceFactory  $customerFactory = null,
DataObjectHelper  $dataObjectHelper = null 
)

Initialize dependencies.

Parameters
\Magento\Framework\Model\Context$context
\Magento\Framework\Registry$registry
\Magento\Newsletter\Helper\Data$newsletterData
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Framework\Mail\Template\TransportBuilder$transportBuilder
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Customer\Model\Session$customerSession
CustomerRepositoryInterface$customerRepository
AccountManagementInterface$customerAccountManagement
\Magento\Framework\Translate\Inline\StateInterface$inlineTranslation
\Magento\Framework\Model\ResourceModel\AbstractResource | null$resource
\Magento\Framework\Data\Collection\AbstractDb | null$resourceCollection
array$data
\Magento\Framework\Stdlib\DateTime\DateTime | null$dateTime
CustomerInterfaceFactory | null$customerFactory
DataObjectHelper | null$dataObjectHelper@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 164 of file Subscriber.php.

181  {
182  $this->_newsletterData = $newsletterData;
183  $this->_scopeConfig = $scopeConfig;
184  $this->_transportBuilder = $transportBuilder;
185  $this->_storeManager = $storeManager;
186  $this->_customerSession = $customerSession;
188  \Magento\Framework\Stdlib\DateTime\DateTime::class
189  );
190  $this->customerFactory = $customerFactory ?: ObjectManager::getInstance()
191  ->get(CustomerInterfaceFactory::class);
192  $this->dataObjectHelper = $dataObjectHelper ?: ObjectManager::getInstance()
193  ->get(DataObjectHelper::class);
194  $this->customerRepository = $customerRepository;
195  $this->customerAccountManagement = $customerAccountManagement;
196  $this->inlineTranslation = $inlineTranslation;
197  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
198  }
$storeManager
$resource
Definition: bulk.php:12
$dateTime

Member Function Documentation

◆ _construct()

_construct ( )
protected

Initialize resource model

Returns
void

Definition at line 205 of file Subscriber.php.

206  {
207  $this->_init(\Magento\Newsletter\Model\ResourceModel\Subscriber::class);
208  }

◆ _updateCustomerSubscription()

_updateCustomerSubscription (   $customerId,
  $subscribe 
)
protected

Saving customer subscription status

Parameters
int$customerId
bool$subscribeindicates whether the customer should be subscribed or unsubscribed
Returns
$this

@SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

If subscription status has been changed then send email to the customer

Definition at line 564 of file Subscriber.php.

565  {
566  try {
567  $customerData = $this->customerRepository->getById($customerId);
568  } catch (NoSuchEntityException $e) {
569  return $this;
570  }
571 
573  if (!$subscribe && !$this->getId()) {
574  return $this;
575  }
576 
577  if (!$this->getId()) {
578  $this->setSubscriberConfirmCode($this->randomSequence());
579  }
580 
581  $sendInformationEmail = false;
583  $isConfirmNeed = $this->_scopeConfig->getValue(
584  self::XML_PATH_CONFIRMATION_FLAG,
586  ) == 1 ? true : false;
587  if ($subscribe) {
589  == $this->customerAccountManagement->getConfirmationStatus($customerId)
590  ) {
592  } elseif ($isConfirmNeed) {
593  if ($this->getStatus() != self::STATUS_SUBSCRIBED) {
595  }
596  }
597  } elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && ($customerData->getConfirmation() === null)) {
599  $sendInformationEmail = true;
600  } else {
602  }
606  if ($status != self::STATUS_UNCONFIRMED && $status != $this->getStatus()) {
607  $sendInformationEmail = true;
608  }
609 
610  if ($status != $this->getStatus()) {
611  $this->setStatusChanged(true);
612  }
613 
614  $this->setStatus($status);
615 
616  if (!$this->getId()) {
617  $storeId = $customerData->getStoreId();
618  if ($customerData->getStoreId() == 0) {
619  $storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
620  }
621  $this->setStoreId($storeId)
622  ->setCustomerId($customerData->getId())
623  ->setEmail($customerData->getEmail());
624  } else {
625  $this->setStoreId($customerData->getStoreId())
626  ->setEmail($customerData->getEmail());
627  }
628 
629  $this->save();
630  $sendSubscription = $sendInformationEmail;
631  if ($sendSubscription === null xor $sendSubscription && $this->isStatusChanged()) {
632  try {
633  switch ($status) {
635  $this->sendUnsubscriptionEmail();
636  break;
639  break;
641  if ($isConfirmNeed) {
643  }
644  break;
645  }
646  } catch (MailException $e) {
647  // If we are not able to send a new account email, this should be ignored
648  $this->_logger->critical($e);
649  }
650  }
651  return $this;
652  }
$customerData
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$status
Definition: order_status.php:8

◆ beforeSave()

beforeSave ( )

Set date of last changed status

Returns
$this
Since
100.2.1

Definition at line 864 of file Subscriber.php.

865  {
866  parent::beforeSave();
867  if ($this->dataHasChangedFor('subscriber_status')) {
868  $this->setChangeStatusAt($this->dateTime->gmtDate());
869  }
870  return $this;
871  }

◆ confirm()

confirm (   $code)

Confirms subscriber newsletter

Parameters
string$code
Returns
boolean

Definition at line 660 of file Subscriber.php.

661  {
662  if ($this->getCode() == $code) {
663  $this->setStatus(self::STATUS_SUBSCRIBED)
664  ->setStatusChanged(true)
665  ->save();
666 
668  return true;
669  }
670 
671  return false;
672  }
$code
Definition: info.phtml:12

◆ getCode()

getCode ( )

Alias for getSubscriberConfirmCode()

Returns
string

Definition at line 236 of file Subscriber.php.

237  {
238  return $this->getSubscriberConfirmCode();
239  }

◆ getConfirmationLink()

getConfirmationLink ( )

Return link for confirmation of subscription

Returns
string

Definition at line 246 of file Subscriber.php.

247  {
248  return $this->_newsletterData->getConfirmationUrl($this);
249  }

◆ getEmail()

getEmail ( )

Alias for getSubscriberEmail()

Returns
string

Definition at line 310 of file Subscriber.php.

311  {
312  return $this->getSubscriberEmail();
313  }

◆ getId()

getId ( )

Alias for getSubscriberId()

Returns
int

Definition at line 215 of file Subscriber.php.

216  {
217  return $this->getSubscriberId();
218  }

◆ getStatus()

getStatus ( )

Alias for getSubscriberStatus()

Returns
int

Definition at line 277 of file Subscriber.php.

278  {
279  return $this->getSubscriberStatus();
280  }

◆ getSubscriberFullName()

getSubscriberFullName ( )

Retrieve Subscribers Full Name if it was set

Returns
string|null

Definition at line 849 of file Subscriber.php.

850  {
851  $name = null;
852  if ($this->hasFirstname() || $this->hasLastname()) {
853  $name = $this->getFirstname() . ' ' . $this->getLastname();
854  }
855  return $name;
856  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getUnsubscriptionLink()

getUnsubscriptionLink ( )

Returns Unsubscribe url

Returns
string

Definition at line 256 of file Subscriber.php.

257  {
258  return $this->_newsletterData->getUnsubscribeUrl($this);
259  }

◆ isStatusChanged()

isStatusChanged ( )

Return status change flag value

Returns
boolean

Definition at line 343 of file Subscriber.php.

◆ isSubscribed()

isSubscribed ( )

Return customer subscription status

Returns
bool

Definition at line 353 of file Subscriber.php.

354  {
355  if ($this->getId() && $this->getStatus() == self::STATUS_SUBSCRIBED) {
356  return true;
357  }
358 
359  return false;
360  }

◆ loadByCustomerId()

loadByCustomerId (   $customerId)

Load subscriber info by customerId

Parameters
int$customerId
Returns
$this

Definition at line 390 of file Subscriber.php.

391  {
392  try {
393  $customerData = $this->customerRepository->getById($customerId);
394  $customerData->setStoreId($this->_storeManager->getStore()->getId());
395  $data = $this->getResource()->loadByCustomerData($customerData);
396  $this->addData($data);
397  if (!empty($data) && $customerData->getId() && !$this->getCustomerId()) {
398  $this->setCustomerId($customerData->getId());
399  $this->setSubscriberConfirmCode($this->randomSequence());
400  $this->save();
401  }
402  } catch (NoSuchEntityException $e) {
403  }
404  return $this;
405  }
$customerData

◆ randomSequence()

randomSequence (   $length = 32)

Returns string of random chars

Parameters
int$length
Returns
string

Definition at line 413 of file Subscriber.php.

414  {
415  $id = '';
416  $par = [];
417  $char = array_merge(range('a', 'z'), range(0, 9));
418  $charLen = count($char) - 1;
419  for ($i = 0; $i < $length; $i++) {
421  $par[$i] = $char[$disc];
422  $id = $id . $char[$disc];
423  }
424  return $id;
425  }
static getRandomNumber($min=0, $max=null)
Definition: Random.php:62
$id
Definition: fieldset.phtml:14
$i
Definition: gallery.phtml:31

◆ received()

received ( \Magento\Newsletter\Model\Queue  $queue)

Mark receiving subscriber of queue newsletter

Parameters
\Magento\Newsletter\Model\Queue$queue
Returns
boolean

Definition at line 680 of file Subscriber.php.

681  {
682  $this->getResource()->received($this, $queue);
683  return $this;
684  }
$queue
Definition: queue.php:21

◆ sendConfirmationRequestEmail()

sendConfirmationRequestEmail ( )

Sends out confirmation email

Returns
$this

Definition at line 691 of file Subscriber.php.

692  {
693  if ($this->getImportMode()) {
694  return $this;
695  }
696 
697  if (!$this->_scopeConfig->getValue(
698  self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
700  ) || !$this->_scopeConfig->getValue(
701  self::XML_PATH_CONFIRM_EMAIL_IDENTITY,
703  )
704  ) {
705  return $this;
706  }
707 
708  $this->inlineTranslation->suspend();
709 
710  $this->_transportBuilder->setTemplateIdentifier(
711  $this->_scopeConfig->getValue(
712  self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
714  )
715  )->setTemplateOptions(
716  [
717  'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
718  'store' => $this->_storeManager->getStore()->getId(),
719  ]
720  )->setTemplateVars(
721  ['subscriber' => $this, 'store' => $this->_storeManager->getStore()]
722  )->setFrom(
723  $this->_scopeConfig->getValue(
724  self::XML_PATH_CONFIRM_EMAIL_IDENTITY,
726  )
727  )->addTo(
728  $this->getEmail(),
729  $this->getName()
730  );
731  $transport = $this->_transportBuilder->getTransport();
732  $transport->sendMessage();
733 
734  $this->inlineTranslation->resume();
735 
736  return $this;
737  }

◆ sendConfirmationSuccessEmail()

sendConfirmationSuccessEmail ( )

Sends out confirmation success email

Returns
$this

Definition at line 744 of file Subscriber.php.

745  {
746  if ($this->getImportMode()) {
747  return $this;
748  }
749 
750  if (!$this->_scopeConfig->getValue(
751  self::XML_PATH_SUCCESS_EMAIL_TEMPLATE,
753  ) || !$this->_scopeConfig->getValue(
754  self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
756  )
757  ) {
758  return $this;
759  }
760 
761  $this->inlineTranslation->suspend();
762 
763  $this->_transportBuilder->setTemplateIdentifier(
764  $this->_scopeConfig->getValue(
765  self::XML_PATH_SUCCESS_EMAIL_TEMPLATE,
767  )
768  )->setTemplateOptions(
769  [
770  'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
771  'store' => $this->_storeManager->getStore()->getId(),
772  ]
773  )->setTemplateVars(
774  ['subscriber' => $this]
775  )->setFrom(
776  $this->_scopeConfig->getValue(
777  self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
779  )
780  )->addTo(
781  $this->getEmail(),
782  $this->getName()
783  );
784  $transport = $this->_transportBuilder->getTransport();
785  $transport->sendMessage();
786 
787  $this->inlineTranslation->resume();
788 
789  return $this;
790  }

◆ sendUnsubscriptionEmail()

sendUnsubscriptionEmail ( )

Sends out unsubscription email

Returns
$this

Definition at line 797 of file Subscriber.php.

798  {
799  if ($this->getImportMode()) {
800  return $this;
801  }
802  if (!$this->_scopeConfig->getValue(
803  self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE,
805  ) || !$this->_scopeConfig->getValue(
806  self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY,
808  )
809  ) {
810  return $this;
811  }
812 
813  $this->inlineTranslation->suspend();
814 
815  $this->_transportBuilder->setTemplateIdentifier(
816  $this->_scopeConfig->getValue(
817  self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE,
819  )
820  )->setTemplateOptions(
821  [
822  'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
823  'store' => $this->_storeManager->getStore()->getId(),
824  ]
825  )->setTemplateVars(
826  ['subscriber' => $this]
827  )->setFrom(
828  $this->_scopeConfig->getValue(
829  self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY,
831  )
832  )->addTo(
833  $this->getEmail(),
834  $this->getName()
835  );
836  $transport = $this->_transportBuilder->getTransport();
837  $transport->sendMessage();
838 
839  $this->inlineTranslation->resume();
840 
841  return $this;
842  }

◆ setCode()

setCode (   $value)

Alias for setSubscriberConfirmCode()

Parameters
string$value
Returns
$this

Definition at line 267 of file Subscriber.php.

268  {
269  return $this->setSubscriberConfirmCode($value);
270  }
$value
Definition: gender.phtml:16

◆ setEmail()

setEmail (   $value)

Alias for setSubscriberEmail()

Parameters
string$value
Returns
$this

Definition at line 321 of file Subscriber.php.

322  {
323  return $this->setSubscriberEmail($value);
324  }
$value
Definition: gender.phtml:16

◆ setId()

setId (   $value)

Alias for setSubscriberId()

Parameters
int$value
Returns
$this

Definition at line 226 of file Subscriber.php.

227  {
228  return $this->setSubscriberId($value);
229  }
$value
Definition: gender.phtml:16

◆ setMessagesScope()

setMessagesScope (   $scope)

Set the error messages scope for subscription

Parameters
boolean$scope
Returns
$this

Definition at line 299 of file Subscriber.php.

300  {
301  $this->getResource()->setMessagesScope($scope);
302  return $this;
303  }

◆ setStatus()

setStatus (   $value)

Alias for setSubscriberStatus()

Parameters
int$value
Returns
$this

Definition at line 288 of file Subscriber.php.

289  {
290  return $this->setSubscriberStatus($value);
291  }
$value
Definition: gender.phtml:16

◆ setStatusChanged()

setStatusChanged (   $value)

Set for status change flag

Parameters
boolean$value
Returns
$this

Definition at line 332 of file Subscriber.php.

333  {
334  $this->_isStatusChanged = (boolean) $value;
335  return $this;
336  }
$value
Definition: gender.phtml:16

◆ subscribe()

subscribe (   $email)

Subscribes by email

Parameters
string$email
Exceptions

Definition at line 437 of file Subscriber.php.

438  {
439  $this->loadByEmail($email);
440 
441  if ($this->getId() && $this->getStatus() == self::STATUS_SUBSCRIBED) {
442  return $this->getStatus();
443  }
444 
445  if (!$this->getId()) {
446  $this->setSubscriberConfirmCode($this->randomSequence());
447  }
448 
449  $isConfirmNeed = $this->_scopeConfig->getValue(
450  self::XML_PATH_CONFIRMATION_FLAG,
452  ) == 1 ? true : false;
453 
454  $isSubscribeOwnEmail = $this->_customerSession->isLoggedIn()
455  && $this->_customerSession->getCustomerDataObject()->getEmail() == $email;
456 
457  if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED
458  || $this->getStatus() == self::STATUS_NOT_ACTIVE
459  ) {
460  if ($isConfirmNeed === true) {
461  $this->setStatus(self::STATUS_NOT_ACTIVE);
462  } else {
463  $this->setStatus(self::STATUS_SUBSCRIBED);
464  }
465  $this->setSubscriberEmail($email);
466  }
467 
468  if ($isSubscribeOwnEmail) {
469  try {
470  $customer = $this->customerRepository->getById($this->_customerSession->getCustomerId());
471  $this->setStoreId($customer->getStoreId());
472  $this->setCustomerId($customer->getId());
473  } catch (NoSuchEntityException $e) {
474  $this->setStoreId($this->_storeManager->getStore()->getId());
475  $this->setCustomerId(0);
476  }
477  } else {
478  $this->setStoreId($this->_storeManager->getStore()->getId());
479  $this->setCustomerId(0);
480  }
481 
482  $this->setStatusChanged(true);
483 
484  try {
485  /* Save model before sending out email */
486  $this->save();
487  if ($isConfirmNeed === true) {
489  } else {
491  }
492  return $this->getStatus();
493  } catch (\Exception $e) {
494  throw new \Exception($e->getMessage());
495  }
496  }
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13

◆ subscribeCustomerById()

subscribeCustomerById (   $customerId)

Subscribe the customer with the id provided

Parameters
int$customerId
Returns
$this

Definition at line 525 of file Subscriber.php.

526  {
527  return $this->_updateCustomerSubscription($customerId, true);
528  }
_updateCustomerSubscription($customerId, $subscribe)
Definition: Subscriber.php:564

◆ unsubscribe()

unsubscribe ( )

Unsubscribes loaded subscription

Exceptions

Definition at line 504 of file Subscriber.php.

505  {
506  if ($this->hasCheckCode() && $this->getCode() != $this->getCheckCode()) {
507  throw new \Magento\Framework\Exception\LocalizedException(
508  __('This is an invalid subscription confirmation code.')
509  );
510  }
511 
512  if ($this->getSubscriberStatus() != self::STATUS_UNSUBSCRIBED) {
513  $this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)->save();
514  $this->sendUnsubscriptionEmail();
515  }
516  return $this;
517  }
__()
Definition: __.php:13

◆ unsubscribeCustomerById()

unsubscribeCustomerById (   $customerId)

Unsubscribe the customer with the id provided

Parameters
int$customerId
Returns
$this

Definition at line 536 of file Subscriber.php.

537  {
538  return $this->_updateCustomerSubscription($customerId, false);
539  }
_updateCustomerSubscription($customerId, $subscribe)
Definition: Subscriber.php:564

◆ updateSubscription()

updateSubscription (   $customerId)

Update the subscription based on latest information of associated customer.

Parameters
int$customerId
Returns
$this

Definition at line 547 of file Subscriber.php.

548  {
551  return $this;
552  }
_updateCustomerSubscription($customerId, $subscribe)
Definition: Subscriber.php:564

Field Documentation

◆ $_customerSession

$_customerSession
protected

Definition at line 98 of file Subscriber.php.

◆ $_eventObject

$_eventObject = 'subscriber'
protected

Definition at line 70 of file Subscriber.php.

◆ $_eventPrefix

$_eventPrefix = 'newsletter_subscriber'
protected

Definition at line 61 of file Subscriber.php.

◆ $_isStatusChanged

$_isStatusChanged = false
protected

Definition at line 77 of file Subscriber.php.

◆ $_newsletterData

$_newsletterData = null
protected

Definition at line 84 of file Subscriber.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 91 of file Subscriber.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 111 of file Subscriber.php.

◆ $_transportBuilder

$_transportBuilder
protected

Definition at line 126 of file Subscriber.php.

◆ $customerAccountManagement

$customerAccountManagement
protected

Definition at line 121 of file Subscriber.php.

◆ $customerRepository

$customerRepository
protected

Definition at line 116 of file Subscriber.php.

◆ $inlineTranslation

$inlineTranslation
protected

Definition at line 131 of file Subscriber.php.

◆ STATUS_NOT_ACTIVE

const STATUS_NOT_ACTIVE = 2

Definition at line 43 of file Subscriber.php.

◆ STATUS_SUBSCRIBED

const STATUS_SUBSCRIBED = 1

Definition at line 42 of file Subscriber.php.

◆ STATUS_UNCONFIRMED

const STATUS_UNCONFIRMED = 4

Definition at line 45 of file Subscriber.php.

◆ STATUS_UNSUBSCRIBED

const STATUS_UNSUBSCRIBED = 3

Definition at line 44 of file Subscriber.php.

◆ XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG

const XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG = 'newsletter/subscription/allow_guest_subscribe'

Definition at line 54 of file Subscriber.php.

◆ XML_PATH_CONFIRM_EMAIL_IDENTITY

const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity'

Definition at line 48 of file Subscriber.php.

◆ XML_PATH_CONFIRM_EMAIL_TEMPLATE

const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template'

Definition at line 47 of file Subscriber.php.

◆ XML_PATH_CONFIRMATION_FLAG

const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm'

Definition at line 53 of file Subscriber.php.

◆ XML_PATH_SUCCESS_EMAIL_IDENTITY

const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity'

Definition at line 50 of file Subscriber.php.

◆ XML_PATH_SUCCESS_EMAIL_TEMPLATE

const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template'

Definition at line 49 of file Subscriber.php.

◆ XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY

const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity'

Definition at line 52 of file Subscriber.php.

◆ XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE

const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template'

Definition at line 51 of file Subscriber.php.


The documentation for this class was generated from the following file: