Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Email.php
Go to the documentation of this file.
1 <?php
2 
8 
33 
43 class Email extends AbstractModel
44 {
45  const XML_PATH_EMAIL_PRICE_TEMPLATE = 'catalog/productalert/email_price_template';
46 
47  const XML_PATH_EMAIL_STOCK_TEMPLATE = 'catalog/productalert/email_stock_template';
48 
49  const XML_PATH_EMAIL_IDENTITY = 'catalog/productalert/email_identity';
50 
56  protected $_type = 'price';
57 
63  protected $_website;
64 
70  protected $_customer;
71 
77  protected $_priceProducts = [];
78 
84  protected $_stockProducts = [];
85 
91  protected $_priceBlock;
92 
98  protected $_stockBlock;
99 
105  protected $_productAlertData = null;
106 
112  protected $_scopeConfig;
113 
117  protected $_storeManager;
118 
123 
127  protected $_appEmulation;
128 
133 
137  protected $_customerHelper;
138 
154  public function __construct(
155  Context $context,
157  Data $productAlertData,
158  ScopeConfigInterface $scopeConfig,
161  View $customerHelper,
162  Emulation $appEmulation,
163  TransportBuilder $transportBuilder,
165  AbstractDb $resourceCollection = null,
166  array $data = []
167  ) {
168  $this->_productAlertData = $productAlertData;
169  $this->_scopeConfig = $scopeConfig;
170  $this->_storeManager = $storeManager;
171  $this->customerRepository = $customerRepository;
172  $this->_appEmulation = $appEmulation;
173  $this->_transportBuilder = $transportBuilder;
174  $this->_customerHelper = $customerHelper;
175  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
176  }
177 
185  public function setType($type)
186  {
187  $this->_type = $type;
188  }
189 
195  public function getType()
196  {
197  return $this->_type;
198  }
199 
207  public function setWebsite(\Magento\Store\Model\Website $website)
208  {
209  $this->_website = $website;
210  return $this;
211  }
212 
221  public function setWebsiteId($websiteId)
222  {
223  $this->_website = $this->_storeManager->getWebsite($websiteId);
224  return $this;
225  }
226 
236  public function setCustomerId($customerId)
237  {
238  $this->_customer = $this->customerRepository->getById($customerId);
239  return $this;
240  }
241 
249  public function setCustomerData($customer)
250  {
251  $this->_customer = $customer;
252  return $this;
253  }
254 
260  public function clean()
261  {
262  $this->_customer = null;
263  $this->_priceProducts = [];
264  $this->_stockProducts = [];
265 
266  return $this;
267  }
268 
276  public function addPriceProduct(\Magento\Catalog\Model\Product $product)
277  {
278  $this->_priceProducts[$product->getId()] = $product;
279  return $this;
280  }
281 
289  public function addStockProduct(\Magento\Catalog\Model\Product $product)
290  {
291  $this->_stockProducts[$product->getId()] = $product;
292  return $this;
293  }
294 
301  protected function _getPriceBlock()
302  {
303  if ($this->_priceBlock === null) {
304  $this->_priceBlock = $this->_productAlertData->createBlock(Price::class);
305  }
306  return $this->_priceBlock;
307  }
308 
315  protected function _getStockBlock()
316  {
317  if ($this->_stockBlock === null) {
318  $this->_stockBlock = $this->_productAlertData->createBlock(Stock::class);
319  }
320  return $this->_stockBlock;
321  }
322 
331  public function send()
332  {
333  if ($this->_website === null || $this->_customer === null) {
334  return false;
335  }
336 
337  if (!$this->_website->getDefaultGroup() || !$this->_website->getDefaultGroup()->getDefaultStore()) {
338  return false;
339  }
340 
341  if (!in_array($this->_type, ['price', 'stock'])) {
342  return false;
343  }
344 
345  $products = $this->getProducts();
346  if (count($products) === 0) {
347  return false;
348  }
349 
350  $templateConfigPath = $this->getTemplateConfigPath();
351  if (!$templateConfigPath) {
352  return false;
353  }
354 
355  $store = $this->getStore((int) $this->_customer->getStoreId());
356  $storeId = $store->getId();
357 
358  $this->_appEmulation->startEnvironmentEmulation($storeId);
359 
360  $block = $this->getBlock();
361  $block->setStore($store)->reset();
362 
363  // Add products to the block
364  foreach ($products as $product) {
365  $product->setCustomerGroupId($this->_customer->getGroupId());
366  $block->addProduct($product);
367  }
368 
369  $templateId = $this->_scopeConfig->getValue(
370  $templateConfigPath,
372  $storeId
373  );
374 
375  $alertGrid = $this->_appState->emulateAreaCode(
377  [$block, 'toHtml']
378  );
379  $this->_appEmulation->stopEnvironmentEmulation();
380 
381  $customerName = $this->_customerHelper->getCustomerName($this->_customer);
382  $this->_transportBuilder->setTemplateIdentifier(
384  )->setTemplateOptions(
385  ['area' => Area::AREA_FRONTEND, 'store' => $storeId]
386  )->setTemplateVars(
387  [
388  'customerName' => $customerName,
389  'alertGrid' => $alertGrid,
390  ]
391  )->setFrom(
392  $this->_scopeConfig->getValue(
393  self::XML_PATH_EMAIL_IDENTITY,
395  $storeId
396  )
397  )->addTo(
398  $this->_customer->getEmail(),
399  $customerName
400  )->getTransport()->sendMessage();
401 
402  return true;
403  }
404 
413  private function getStore(?int $customerStoreId): StoreInterface
414  {
415  return $customerStoreId > 0
416  ? $this->_storeManager->getStore($customerStoreId)
417  : $this->_website->getDefaultStore();
418  }
419 
426  private function getBlock(): AbstractEmail
427  {
428  return $this->_type === 'price'
429  ? $this->_getPriceBlock()
430  : $this->_getStockBlock();
431  }
432 
438  private function getProducts(): array
439  {
440  return $this->_type === 'price'
441  ? $this->_priceProducts
443  }
444 
450  private function getTemplateConfigPath(): string
451  {
452  return $this->_type === 'price'
455  }
456 }
$customer
Definition: customers.php:11
$storeManager
$resource
Definition: bulk.php:12
__construct(Context $context, Registry $registry, Data $productAlertData, ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager, CustomerRepositoryInterface $customerRepository, View $customerHelper, Emulation $appEmulation, TransportBuilder $transportBuilder, AbstractResource $resource=null, AbstractDb $resourceCollection=null, array $data=[])
Definition: Email.php:154
$block
Definition: block.php:8
$templateId
Definition: queue.php:15
$type
Definition: item.phtml:13
addStockProduct(\Magento\Catalog\Model\Product $product)
Definition: Email.php:289
setWebsite(\Magento\Store\Model\Website $website)
Definition: Email.php:207
addPriceProduct(\Magento\Catalog\Model\Product $product)
Definition: Email.php:276