Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Observer Class Reference

Public Member Functions

 __construct (\Magento\Catalog\Helper\Data $catalogData, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ProductAlert\Model\ResourceModel\Price\CollectionFactory $priceColFactory, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory, \Magento\ProductAlert\Model\ResourceModel\Stock\CollectionFactory $stockColFactory, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\ProductAlert\Model\EmailFactory $emailFactory, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, ProductSalability $productSalability=null)
 
 process ()
 

Data Fields

const XML_PATH_ERROR_TEMPLATE = 'catalog/productalert_cron/error_email_template'
 
const XML_PATH_ERROR_IDENTITY = 'catalog/productalert_cron/error_email_identity'
 
const XML_PATH_ERROR_RECIPIENT = 'catalog/productalert_cron/error_email'
 
const XML_PATH_PRICE_ALLOW = 'catalog/productalert/allow_price'
 
const XML_PATH_STOCK_ALLOW = 'catalog/productalert/allow_stock'
 

Protected Member Functions

 _getWebsites ()
 
 _processPrice (\Magento\ProductAlert\Model\Email $email)
 
 _processStock (\Magento\ProductAlert\Model\Email $email)
 
 _sendErrorEmail ()
 

Protected Attributes

 $_websites
 
 $_errors = []
 
 $_catalogData = null
 
 $_scopeConfig
 
 $_storeManager
 
 $_priceColFactory
 
 $customerRepository
 
 $productRepository
 
 $_dateFactory
 
 $_stockColFactory
 
 $_transportBuilder
 
 $_emailFactory
 
 $inlineTranslation
 
 $productSalability
 

Detailed Description

ProductAlert observer

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 14 of file Observer.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Helper\Data  $catalogData,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\ProductAlert\Model\ResourceModel\Price\CollectionFactory  $priceColFactory,
\Magento\Customer\Api\CustomerRepositoryInterface  $customerRepository,
\Magento\Catalog\Api\ProductRepositoryInterface  $productRepository,
\Magento\Framework\Stdlib\DateTime\DateTimeFactory  $dateFactory,
\Magento\ProductAlert\Model\ResourceModel\Stock\CollectionFactory  $stockColFactory,
\Magento\Framework\Mail\Template\TransportBuilder  $transportBuilder,
\Magento\ProductAlert\Model\EmailFactory  $emailFactory,
\Magento\Framework\Translate\Inline\StateInterface  $inlineTranslation,
ProductSalability  $productSalability = null 
)
Parameters
\Magento\Catalog\Helper\Data$catalogData
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\ProductAlert\Model\ResourceModel\Price\CollectionFactory$priceColFactory
\Magento\Customer\Api\CustomerRepositoryInterface$customerRepository
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository
\Magento\Framework\Stdlib\DateTime\DateTimeFactory$dateFactory
\Magento\ProductAlert\Model\ResourceModel\Stock\CollectionFactory$stockColFactory
\Magento\Framework\Mail\Template\TransportBuilder$transportBuilder
\Magento\ProductAlert\Model\EmailFactory$emailFactory
\Magento\Framework\Translate\Inline\StateInterface$inlineTranslation
ProductSalability | null$productSalability@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 136 of file Observer.php.

149  {
150  $this->_catalogData = $catalogData;
151  $this->_scopeConfig = $scopeConfig;
152  $this->_storeManager = $storeManager;
153  $this->_priceColFactory = $priceColFactory;
154  $this->customerRepository = $customerRepository;
155  $this->productRepository = $productRepository;
156  $this->_dateFactory = $dateFactory;
157  $this->_stockColFactory = $stockColFactory;
158  $this->_transportBuilder = $transportBuilder;
159  $this->_emailFactory = $emailFactory;
160  $this->inlineTranslation = $inlineTranslation;
162  $this->productSalability = $productSalability ?: $objectManager->get(ProductSalability::class);
163  }
$objectManager
Definition: bootstrap.php:17
$storeManager

Member Function Documentation

◆ _getWebsites()

_getWebsites ( )
protected

Retrieve website collection array

Returns
array
Exceptions

Definition at line 171 of file Observer.php.

172  {
173  if ($this->_websites === null) {
174  try {
175  $this->_websites = $this->_storeManager->getWebsites();
176  } catch (\Exception $e) {
177  $this->_errors[] = $e->getMessage();
178  throw $e;
179  }
180  }
181  return $this->_websites;
182  }

◆ _processPrice()

_processPrice ( \Magento\ProductAlert\Model\Email  $email)
protected

Process price emails

Parameters
\Magento\ProductAlert\Model\Email$email
Returns
$this
Exceptions

Definition at line 193 of file Observer.php.

194  {
195  $email->setType('price');
196  foreach ($this->_getWebsites() as $website) {
197  /* @var $website \Magento\Store\Model\Website */
198  if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
199  continue;
200  }
201  if (!$this->_scopeConfig->getValue(
202  self::XML_PATH_PRICE_ALLOW,
204  $website->getDefaultGroup()->getDefaultStore()->getId()
205  )
206  ) {
207  continue;
208  }
209  try {
210  $collection = $this->_priceColFactory->create()->addWebsiteFilter(
211  $website->getId()
212  )->setCustomerOrder();
213  } catch (\Exception $e) {
214  $this->_errors[] = $e->getMessage();
215  throw $e;
216  }
217 
218  $previousCustomer = null;
219  $email->setWebsite($website);
220  foreach ($collection as $alert) {
221  try {
222  if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
223  $customer = $this->customerRepository->getById($alert->getCustomerId());
224  if ($previousCustomer) {
225  $email->send();
226  }
227  if (!$customer) {
228  continue;
229  }
230  $previousCustomer = $customer;
231  $email->clean();
232  $email->setCustomerData($customer);
233  } else {
234  $customer = $previousCustomer;
235  }
236 
237  $product = $this->productRepository->getById(
238  $alert->getProductId(),
239  false,
240  $website->getDefaultStore()->getId()
241  );
242 
243  $product->setCustomerGroupId($customer->getGroupId());
244  if ($alert->getPrice() > $product->getFinalPrice()) {
245  $productPrice = $product->getFinalPrice();
246  $product->setFinalPrice($this->_catalogData->getTaxPrice($product, $productPrice));
247  $product->setPrice($this->_catalogData->getTaxPrice($product, $product->getPrice()));
248  $email->addPriceProduct($product);
249 
250  $alert->setPrice($productPrice);
251  $alert->setLastSendDate($this->_dateFactory->create()->gmtDate());
252  $alert->setSendCount($alert->getSendCount() + 1);
253  $alert->setStatus(1);
254  $alert->save();
255  }
256  } catch (\Exception $e) {
257  $this->_errors[] = $e->getMessage();
258  throw $e;
259  }
260  }
261  if ($previousCustomer) {
262  try {
263  $email->send();
264  } catch (\Exception $e) {
265  $this->_errors[] = $e->getMessage();
266  throw $e;
267  }
268  }
269  }
270  return $this;
271  }
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13

◆ _processStock()

_processStock ( \Magento\ProductAlert\Model\Email  $email)
protected

Process stock emails

Parameters
\Magento\ProductAlert\Model\Email$email
Returns
$this
Exceptions

Definition at line 282 of file Observer.php.

283  {
284  $email->setType('stock');
285 
286  foreach ($this->_getWebsites() as $website) {
287  /* @var $website \Magento\Store\Model\Website */
288 
289  if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
290  continue;
291  }
292  if (!$this->_scopeConfig->getValue(
293  self::XML_PATH_STOCK_ALLOW,
295  $website->getDefaultGroup()->getDefaultStore()->getId()
296  )
297  ) {
298  continue;
299  }
300  try {
301  $collection = $this->_stockColFactory->create()->addWebsiteFilter(
302  $website->getId()
303  )->addStatusFilter(
304  0
305  )->setCustomerOrder();
306  } catch (\Exception $e) {
307  $this->_errors[] = $e->getMessage();
308  throw $e;
309  }
310 
311  $previousCustomer = null;
312  $email->setWebsite($website);
313  foreach ($collection as $alert) {
314  try {
315  if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
316  $customer = $this->customerRepository->getById($alert->getCustomerId());
317  if ($previousCustomer) {
318  $email->send();
319  }
320  if (!$customer) {
321  continue;
322  }
323  $previousCustomer = $customer;
324  $email->clean();
325  $email->setCustomerData($customer);
326  } else {
327  $customer = $previousCustomer;
328  }
329 
330  $product = $this->productRepository->getById(
331  $alert->getProductId(),
332  false,
333  $website->getDefaultStore()->getId()
334  );
335 
336  $product->setCustomerGroupId($customer->getGroupId());
337 
338  if ($this->productSalability->isSalable($product, $website)) {
339  $email->addStockProduct($product);
340 
341  $alert->setSendDate($this->_dateFactory->create()->gmtDate());
342  $alert->setSendCount($alert->getSendCount() + 1);
343  $alert->setStatus(1);
344  $alert->save();
345  }
346  } catch (\Exception $e) {
347  $this->_errors[] = $e->getMessage();
348  throw $e;
349  }
350  }
351 
352  if ($previousCustomer) {
353  try {
354  $email->send();
355  } catch (\Exception $e) {
356  $this->_errors[] = $e->getMessage();
357  throw $e;
358  }
359  }
360  }
361 
362  return $this;
363  }
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13

◆ _sendErrorEmail()

_sendErrorEmail ( )
protected

Send email to administrator if error

Returns
$this

Definition at line 370 of file Observer.php.

371  {
372  if (count($this->_errors)) {
373  if (!$this->_scopeConfig->getValue(
374  self::XML_PATH_ERROR_TEMPLATE,
376  )
377  ) {
378  return $this;
379  }
380 
381  $this->inlineTranslation->suspend();
382 
383  $transport = $this->_transportBuilder->setTemplateIdentifier(
384  $this->_scopeConfig->getValue(
385  self::XML_PATH_ERROR_TEMPLATE,
387  )
388  )->setTemplateOptions(
389  [
390  'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
391  'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
392  ]
393  )->setTemplateVars(
394  ['warnings' => join("\n", $this->_errors)]
395  )->setFrom(
396  $this->_scopeConfig->getValue(
397  self::XML_PATH_ERROR_IDENTITY,
399  )
400  )->addTo(
401  $this->_scopeConfig->getValue(
402  self::XML_PATH_ERROR_RECIPIENT,
404  )
405  )->getTransport();
406 
407  $transport->sendMessage();
408 
409  $this->inlineTranslation->resume();
410  $this->_errors[] = [];
411  }
412  return $this;
413  }

◆ process()

process ( )

Run process send product alerts

Returns
$this

Definition at line 420 of file Observer.php.

421  {
422  /* @var $email \Magento\ProductAlert\Model\Email */
423  $email = $this->_emailFactory->create();
424  $this->_processPrice($email);
425  $this->_processStock($email);
426  $this->_sendErrorEmail();
427 
428  return $this;
429  }
$email
Definition: details.phtml:13
_processPrice(\Magento\ProductAlert\Model\Email $email)
Definition: Observer.php:193
_processStock(\Magento\ProductAlert\Model\Email $email)
Definition: Observer.php:282

Field Documentation

◆ $_catalogData

$_catalogData = null
protected

Definition at line 62 of file Observer.php.

◆ $_dateFactory

$_dateFactory
protected

Definition at line 94 of file Observer.php.

◆ $_emailFactory

$_emailFactory
protected

Definition at line 109 of file Observer.php.

◆ $_errors

$_errors = []
protected

Definition at line 55 of file Observer.php.

◆ $_priceColFactory

$_priceColFactory
protected

Definition at line 79 of file Observer.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 69 of file Observer.php.

◆ $_stockColFactory

$_stockColFactory
protected

Definition at line 99 of file Observer.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 74 of file Observer.php.

◆ $_transportBuilder

$_transportBuilder
protected

Definition at line 104 of file Observer.php.

◆ $_websites

$_websites
protected

Definition at line 48 of file Observer.php.

◆ $customerRepository

$customerRepository
protected

Definition at line 84 of file Observer.php.

◆ $inlineTranslation

$inlineTranslation
protected

Definition at line 114 of file Observer.php.

◆ $productRepository

$productRepository
protected

Definition at line 89 of file Observer.php.

◆ $productSalability

$productSalability
protected

Definition at line 119 of file Observer.php.

◆ XML_PATH_ERROR_IDENTITY

const XML_PATH_ERROR_IDENTITY = 'catalog/productalert_cron/error_email_identity'

Error email identity configuration

Definition at line 24 of file Observer.php.

◆ XML_PATH_ERROR_RECIPIENT

const XML_PATH_ERROR_RECIPIENT = 'catalog/productalert_cron/error_email'

'Send error emails to' configuration

Definition at line 29 of file Observer.php.

◆ XML_PATH_ERROR_TEMPLATE

const XML_PATH_ERROR_TEMPLATE = 'catalog/productalert_cron/error_email_template'

Error email template configuration

Definition at line 19 of file Observer.php.

◆ XML_PATH_PRICE_ALLOW

const XML_PATH_PRICE_ALLOW = 'catalog/productalert/allow_price'

Allow price alert

Definition at line 35 of file Observer.php.

◆ XML_PATH_STOCK_ALLOW

const XML_PATH_STOCK_ALLOW = 'catalog/productalert/allow_stock'

Allow stock alert

Definition at line 41 of file Observer.php.


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