12 use Magento\Customer\Api\Data\CustomerInterfaceFactory;
136 private $customerFactory;
141 private $dataObjectHelper;
167 \
Magento\Newsletter\Helper\Data $newsletterData,
168 \
Magento\Framework\
App\Config\ScopeConfigInterface $scopeConfig,
176 \
Magento\Framework\Data\Collection\AbstractDb $resourceCollection =
null,
178 \
Magento\Framework\Stdlib\DateTime\DateTime $dateTime =
null,
179 CustomerInterfaceFactory $customerFactory =
null,
182 $this->_newsletterData = $newsletterData;
183 $this->_scopeConfig = $scopeConfig;
184 $this->_transportBuilder = $transportBuilder;
186 $this->_customerSession = $customerSession;
188 \
Magento\Framework\Stdlib\DateTime\DateTime::class
191 ->get(CustomerInterfaceFactory::class);
193 ->get(DataObjectHelper::class);
217 return $this->getSubscriberId();
228 return $this->setSubscriberId(
$value);
238 return $this->getSubscriberConfirmCode();
248 return $this->_newsletterData->getConfirmationUrl($this);
258 return $this->_newsletterData->getUnsubscribeUrl($this);
269 return $this->setSubscriberConfirmCode(
$value);
279 return $this->getSubscriberStatus();
290 return $this->setSubscriberStatus(
$value);
312 return $this->getSubscriberEmail();
323 return $this->setSubscriberEmail(
$value);
334 $this->_isStatusChanged = (boolean)
$value;
355 if ($this->
getId() && $this->
getStatus() == self::STATUS_SUBSCRIBED) {
368 public function loadByEmail($subscriberEmail)
370 $storeId = $this->_storeManager->getStore()->getId();
374 $customer = $this->customerFactory->create();
375 $this->dataObjectHelper->populateWithArray(
378 \
Magento\Customer\Api\Data\CustomerInterface::class
394 $customerData->setStoreId($this->_storeManager->getStore()->getId());
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];
439 $this->loadByEmail(
$email);
441 if ($this->
getId() && $this->
getStatus() == self::STATUS_SUBSCRIBED) {
445 if (!$this->
getId()) {
449 $isConfirmNeed = $this->_scopeConfig->getValue(
450 self::XML_PATH_CONFIRMATION_FLAG,
452 ) == 1 ? true :
false;
454 $isSubscribeOwnEmail = $this->_customerSession->isLoggedIn()
455 && $this->_customerSession->getCustomerDataObject()->getEmail() ==
$email;
457 if (!$this->
getId() || $this->
getStatus() == self::STATUS_UNSUBSCRIBED
458 || $this->
getStatus() == self::STATUS_NOT_ACTIVE
460 if ($isConfirmNeed ===
true) {
461 $this->
setStatus(self::STATUS_NOT_ACTIVE);
463 $this->
setStatus(self::STATUS_SUBSCRIBED);
465 $this->setSubscriberEmail(
$email);
468 if ($isSubscribeOwnEmail) {
470 $customer = $this->customerRepository->getById($this->_customerSession->getCustomerId());
471 $this->setStoreId(
$customer->getStoreId());
472 $this->setCustomerId(
$customer->getId());
474 $this->setStoreId($this->_storeManager->getStore()->getId());
475 $this->setCustomerId(0);
478 $this->setStoreId($this->_storeManager->getStore()->getId());
479 $this->setCustomerId(0);
487 if ($isConfirmNeed ===
true) {
493 }
catch (\Exception $e) {
494 throw new \Exception($e->getMessage());
506 if ($this->hasCheckCode() && $this->
getCode() != $this->getCheckCode()) {
507 throw new \Magento\Framework\Exception\LocalizedException(
508 __(
'This is an invalid subscription confirmation code.')
512 if ($this->getSubscriberStatus() != self::STATUS_UNSUBSCRIBED) {
513 $this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)->save();
573 if (!$subscribe && !$this->
getId()) {
577 if (!$this->
getId()) {
581 $sendInformationEmail =
false;
583 $isConfirmNeed = $this->_scopeConfig->getValue(
584 self::XML_PATH_CONFIRMATION_FLAG,
586 ) == 1 ? true :
false;
589 == $this->customerAccountManagement->getConfirmationStatus(
$customerId)
592 }
elseif ($isConfirmNeed) {
593 if ($this->
getStatus() != self::STATUS_SUBSCRIBED) {
599 $sendInformationEmail =
true;
607 $sendInformationEmail =
true;
616 if (!$this->
getId()) {
619 $storeId = $this->_storeManager->getWebsite(
$customerData->getWebsiteId())->getDefaultStore()->getId();
630 $sendSubscription = $sendInformationEmail;
631 if ($sendSubscription ===
null xor $sendSubscription && $this->
isStatusChanged()) {
641 if ($isConfirmNeed) {
648 $this->_logger->critical($e);
663 $this->
setStatus(self::STATUS_SUBSCRIBED)
664 ->setStatusChanged(
true)
693 if ($this->getImportMode()) {
697 if (!$this->_scopeConfig->getValue(
698 self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
700 ) || !$this->_scopeConfig->getValue(
701 self::XML_PATH_CONFIRM_EMAIL_IDENTITY,
708 $this->inlineTranslation->suspend();
710 $this->_transportBuilder->setTemplateIdentifier(
711 $this->_scopeConfig->getValue(
712 self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
715 )->setTemplateOptions(
717 'area' => \
Magento\Framework\
App\Area::AREA_FRONTEND,
718 'store' => $this->_storeManager->getStore()->getId(),
721 [
'subscriber' => $this,
'store' => $this->_storeManager->getStore()]
723 $this->_scopeConfig->getValue(
724 self::XML_PATH_CONFIRM_EMAIL_IDENTITY,
731 $transport = $this->_transportBuilder->getTransport();
732 $transport->sendMessage();
734 $this->inlineTranslation->resume();
746 if ($this->getImportMode()) {
750 if (!$this->_scopeConfig->getValue(
751 self::XML_PATH_SUCCESS_EMAIL_TEMPLATE,
753 ) || !$this->_scopeConfig->getValue(
754 self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
761 $this->inlineTranslation->suspend();
763 $this->_transportBuilder->setTemplateIdentifier(
764 $this->_scopeConfig->getValue(
765 self::XML_PATH_SUCCESS_EMAIL_TEMPLATE,
768 )->setTemplateOptions(
770 'area' => \
Magento\Framework\
App\Area::AREA_FRONTEND,
771 'store' => $this->_storeManager->getStore()->getId(),
774 [
'subscriber' => $this]
776 $this->_scopeConfig->getValue(
777 self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
784 $transport = $this->_transportBuilder->getTransport();
785 $transport->sendMessage();
787 $this->inlineTranslation->resume();
799 if ($this->getImportMode()) {
802 if (!$this->_scopeConfig->getValue(
803 self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE,
805 ) || !$this->_scopeConfig->getValue(
806 self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY,
813 $this->inlineTranslation->suspend();
815 $this->_transportBuilder->setTemplateIdentifier(
816 $this->_scopeConfig->getValue(
817 self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE,
820 )->setTemplateOptions(
822 'area' => \
Magento\Framework\
App\Area::AREA_FRONTEND,
823 'store' => $this->_storeManager->getStore()->getId(),
826 [
'subscriber' => $this]
828 $this->_scopeConfig->getValue(
829 self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY,
836 $transport = $this->_transportBuilder->getTransport();
837 $transport->sendMessage();
839 $this->inlineTranslation->resume();
852 if ($this->hasFirstname() || $this->hasLastname()) {
853 $name = $this->getFirstname() .
' ' . $this->getLastname();
866 parent::beforeSave();
868 $this->setChangeStatusAt($this->dateTime->gmtDate());
sendConfirmationSuccessEmail()
__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)
subscribeCustomerById($customerId)
const XML_PATH_CONFIRM_EMAIL_TEMPLATE
unsubscribeCustomerById($customerId)
sendUnsubscriptionEmail()
static getRandomNumber($min=0, $max=null)
elseif(isset( $params[ 'redirect_parent']))
updateSubscription($customerId)
loadByCustomerId($customerId)
const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE
received(\Magento\Newsletter\Model\Queue $queue)
randomSequence($length=32)
const XML_PATH_CONFIRM_EMAIL_IDENTITY
const ACCOUNT_CONFIRMATION_REQUIRED
sendConfirmationRequestEmail()
_updateCustomerSubscription($customerId, $subscribe)
dataHasChangedFor($field)
const STATUS_UNSUBSCRIBED
const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY
const XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG
const XML_PATH_SUCCESS_EMAIL_TEMPLATE
const XML_PATH_SUCCESS_EMAIL_IDENTITY
const XML_PATH_CONFIRMATION_FLAG
$customerAccountManagement
if(!isset($_GET['name'])) $name