Email.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Model;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Helper\View;
  11. use Magento\Framework\App\Area;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\Framework\Data\Collection\AbstractDb;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Magento\Framework\Exception\MailException;
  16. use Magento\Framework\Exception\NoSuchEntityException;
  17. use Magento\Framework\Mail\Template\TransportBuilder;
  18. use Magento\Framework\Model\AbstractModel;
  19. use Magento\Framework\Model\Context;
  20. use Magento\Framework\Model\ResourceModel\AbstractResource;
  21. use Magento\Framework\Registry;
  22. use Magento\ProductAlert\Block\Email\AbstractEmail;
  23. use Magento\ProductAlert\Block\Email\Price;
  24. use Magento\ProductAlert\Block\Email\Stock;
  25. use Magento\ProductAlert\Helper\Data;
  26. use Magento\Store\Api\Data\StoreInterface;
  27. use Magento\Store\Model\App\Emulation;
  28. use Magento\Store\Model\ScopeInterface;
  29. use Magento\Store\Model\StoreManagerInterface;
  30. use Magento\Store\Model\Website;
  31. /**
  32. * ProductAlert Email processor
  33. *
  34. * @author Magento Core Team <core@magentocommerce.com>
  35. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  36. *
  37. * @api
  38. * @since 100.0.2
  39. * @method int getStoreId()
  40. * @method $this setStoreId()
  41. */
  42. class Email extends AbstractModel
  43. {
  44. const XML_PATH_EMAIL_PRICE_TEMPLATE = 'catalog/productalert/email_price_template';
  45. const XML_PATH_EMAIL_STOCK_TEMPLATE = 'catalog/productalert/email_stock_template';
  46. const XML_PATH_EMAIL_IDENTITY = 'catalog/productalert/email_identity';
  47. /**
  48. * Type
  49. *
  50. * @var string
  51. */
  52. protected $_type = 'price';
  53. /**
  54. * Website Model
  55. *
  56. * @var Website
  57. */
  58. protected $_website;
  59. /**
  60. * Customer model
  61. *
  62. * @var CustomerInterface
  63. */
  64. protected $_customer;
  65. /**
  66. * Products collection where changed price
  67. *
  68. * @var array
  69. */
  70. protected $_priceProducts = [];
  71. /**
  72. * Product collection which of back in stock
  73. *
  74. * @var array
  75. */
  76. protected $_stockProducts = [];
  77. /**
  78. * Price block
  79. *
  80. * @var Price
  81. */
  82. protected $_priceBlock;
  83. /**
  84. * Stock block
  85. *
  86. * @var Stock
  87. */
  88. protected $_stockBlock;
  89. /**
  90. * Product alert data
  91. *
  92. * @var Data
  93. */
  94. protected $_productAlertData = null;
  95. /**
  96. * Core store config
  97. *
  98. * @var ScopeConfigInterface
  99. */
  100. protected $_scopeConfig;
  101. /**
  102. * @var StoreManagerInterface
  103. */
  104. protected $_storeManager;
  105. /**
  106. * @var CustomerRepositoryInterface
  107. */
  108. protected $customerRepository;
  109. /**
  110. * @var Emulation
  111. */
  112. protected $_appEmulation;
  113. /**
  114. * @var TransportBuilder
  115. */
  116. protected $_transportBuilder;
  117. /**
  118. * @var View
  119. */
  120. protected $_customerHelper;
  121. /**
  122. * @param Context $context
  123. * @param Registry $registry
  124. * @param Data $productAlertData
  125. * @param ScopeConfigInterface $scopeConfig
  126. * @param StoreManagerInterface $storeManager
  127. * @param CustomerRepositoryInterface $customerRepository
  128. * @param View $customerHelper
  129. * @param Emulation $appEmulation
  130. * @param TransportBuilder $transportBuilder
  131. * @param AbstractResource $resource
  132. * @param AbstractDb $resourceCollection
  133. * @param array $data
  134. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  135. */
  136. public function __construct(
  137. Context $context,
  138. Registry $registry,
  139. Data $productAlertData,
  140. ScopeConfigInterface $scopeConfig,
  141. StoreManagerInterface $storeManager,
  142. CustomerRepositoryInterface $customerRepository,
  143. View $customerHelper,
  144. Emulation $appEmulation,
  145. TransportBuilder $transportBuilder,
  146. AbstractResource $resource = null,
  147. AbstractDb $resourceCollection = null,
  148. array $data = []
  149. ) {
  150. $this->_productAlertData = $productAlertData;
  151. $this->_scopeConfig = $scopeConfig;
  152. $this->_storeManager = $storeManager;
  153. $this->customerRepository = $customerRepository;
  154. $this->_appEmulation = $appEmulation;
  155. $this->_transportBuilder = $transportBuilder;
  156. $this->_customerHelper = $customerHelper;
  157. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  158. }
  159. /**
  160. * Set model type
  161. *
  162. * @param string $type
  163. *
  164. * @return void
  165. */
  166. public function setType($type)
  167. {
  168. $this->_type = $type;
  169. }
  170. /**
  171. * Retrieve model type
  172. *
  173. * @return string
  174. */
  175. public function getType()
  176. {
  177. return $this->_type;
  178. }
  179. /**
  180. * Set website model
  181. *
  182. * @param Website $website
  183. *
  184. * @return $this
  185. */
  186. public function setWebsite(\Magento\Store\Model\Website $website)
  187. {
  188. $this->_website = $website;
  189. return $this;
  190. }
  191. /**
  192. * Set website id
  193. *
  194. * @param int $websiteId
  195. *
  196. * @return $this
  197. * @throws LocalizedException
  198. */
  199. public function setWebsiteId($websiteId)
  200. {
  201. $this->_website = $this->_storeManager->getWebsite($websiteId);
  202. return $this;
  203. }
  204. /**
  205. * Set customer by id
  206. *
  207. * @param int $customerId
  208. *
  209. * @return $this
  210. * @throws LocalizedException
  211. * @throws NoSuchEntityException
  212. */
  213. public function setCustomerId($customerId)
  214. {
  215. $this->_customer = $this->customerRepository->getById($customerId);
  216. return $this;
  217. }
  218. /**
  219. * Set customer model
  220. *
  221. * @param CustomerInterface $customer
  222. *
  223. * @return $this
  224. */
  225. public function setCustomerData($customer)
  226. {
  227. $this->_customer = $customer;
  228. return $this;
  229. }
  230. /**
  231. * Clean data
  232. *
  233. * @return $this
  234. */
  235. public function clean()
  236. {
  237. $this->_customer = null;
  238. $this->_priceProducts = [];
  239. $this->_stockProducts = [];
  240. return $this;
  241. }
  242. /**
  243. * Add product (price change) to collection
  244. *
  245. * @param Product $product
  246. *
  247. * @return $this
  248. */
  249. public function addPriceProduct(\Magento\Catalog\Model\Product $product)
  250. {
  251. $this->_priceProducts[$product->getId()] = $product;
  252. return $this;
  253. }
  254. /**
  255. * Add product (back in stock) to collection
  256. *
  257. * @param Product $product
  258. *
  259. * @return $this
  260. */
  261. public function addStockProduct(\Magento\Catalog\Model\Product $product)
  262. {
  263. $this->_stockProducts[$product->getId()] = $product;
  264. return $this;
  265. }
  266. /**
  267. * Retrieve price block
  268. *
  269. * @return Price
  270. * @throws LocalizedException
  271. */
  272. protected function _getPriceBlock()
  273. {
  274. if ($this->_priceBlock === null) {
  275. $this->_priceBlock = $this->_productAlertData->createBlock(Price::class);
  276. }
  277. return $this->_priceBlock;
  278. }
  279. /**
  280. * Retrieve stock block
  281. *
  282. * @return Stock
  283. * @throws LocalizedException
  284. */
  285. protected function _getStockBlock()
  286. {
  287. if ($this->_stockBlock === null) {
  288. $this->_stockBlock = $this->_productAlertData->createBlock(Stock::class);
  289. }
  290. return $this->_stockBlock;
  291. }
  292. /**
  293. * Send customer email
  294. *
  295. * @return bool
  296. * @throws MailException
  297. * @throws NoSuchEntityException
  298. * @throws LocalizedException
  299. */
  300. public function send()
  301. {
  302. if ($this->_website === null || $this->_customer === null || !$this->isExistDefaultStore()) {
  303. return false;
  304. }
  305. $products = $this->getProducts();
  306. $templateConfigPath = $this->getTemplateConfigPath();
  307. if (!in_array($this->_type, ['price', 'stock']) || count($products) === 0 || !$templateConfigPath) {
  308. return false;
  309. }
  310. $storeId = $this->getStoreId() ?: (int) $this->_customer->getStoreId();
  311. $store = $this->getStore($storeId);
  312. $this->_appEmulation->startEnvironmentEmulation($storeId);
  313. $block = $this->getBlock();
  314. $block->setStore($store)->reset();
  315. // Add products to the block
  316. foreach ($products as $product) {
  317. $product->setCustomerGroupId($this->_customer->getGroupId());
  318. $block->addProduct($product);
  319. }
  320. $templateId = $this->_scopeConfig->getValue(
  321. $templateConfigPath,
  322. ScopeInterface::SCOPE_STORE,
  323. $storeId
  324. );
  325. $alertGrid = $this->_appState->emulateAreaCode(
  326. Area::AREA_FRONTEND,
  327. [$block, 'toHtml']
  328. );
  329. $this->_appEmulation->stopEnvironmentEmulation();
  330. $customerName = $this->_customerHelper->getCustomerName($this->_customer);
  331. $this->_transportBuilder->setTemplateIdentifier(
  332. $templateId
  333. )->setTemplateOptions(
  334. ['area' => Area::AREA_FRONTEND, 'store' => $storeId]
  335. )->setTemplateVars(
  336. [
  337. 'customerName' => $customerName,
  338. 'alertGrid' => $alertGrid,
  339. ]
  340. )->setFrom(
  341. $this->_scopeConfig->getValue(
  342. self::XML_PATH_EMAIL_IDENTITY,
  343. ScopeInterface::SCOPE_STORE,
  344. $storeId
  345. )
  346. )->addTo(
  347. $this->_customer->getEmail(),
  348. $customerName
  349. )->getTransport()->sendMessage();
  350. return true;
  351. }
  352. /**
  353. * Retrieve the store for the email
  354. *
  355. * @param int $storeId
  356. * @return StoreInterface
  357. * @throws NoSuchEntityException
  358. */
  359. private function getStore(int $storeId): StoreInterface
  360. {
  361. return $this->_storeManager->getStore($storeId);
  362. }
  363. /**
  364. * Retrieve the block for the email based on type
  365. *
  366. * @return Price|Stock
  367. * @throws LocalizedException
  368. */
  369. private function getBlock(): AbstractEmail
  370. {
  371. return $this->_type === 'price'
  372. ? $this->_getPriceBlock()
  373. : $this->_getStockBlock();
  374. }
  375. /**
  376. * Retrieve the products for the email based on type
  377. *
  378. * @return array
  379. */
  380. private function getProducts(): array
  381. {
  382. return $this->_type === 'price'
  383. ? $this->_priceProducts
  384. : $this->_stockProducts;
  385. }
  386. /**
  387. * Retrieve template config path based on type
  388. *
  389. * @return string
  390. */
  391. private function getTemplateConfigPath(): string
  392. {
  393. return $this->_type === 'price'
  394. ? self::XML_PATH_EMAIL_PRICE_TEMPLATE
  395. : self::XML_PATH_EMAIL_STOCK_TEMPLATE;
  396. }
  397. /**
  398. * Check if exists default store.
  399. *
  400. * @return bool
  401. */
  402. private function isExistDefaultStore(): bool
  403. {
  404. if (!$this->_website->getDefaultGroup() || !$this->_website->getDefaultGroup()->getDefaultStore()) {
  405. return false;
  406. }
  407. return true;
  408. }
  409. }