AbstractEmail.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Block\Email;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\ProductAlert\Block\Product\ImageProvider;
  10. /**
  11. * Product Alert Abstract Email Block
  12. */
  13. abstract class AbstractEmail extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Product collection array
  17. *
  18. * @var \Magento\Catalog\Model\Product[]
  19. */
  20. protected $_products = [];
  21. /**
  22. * Current Store scope object
  23. *
  24. * @var \Magento\Store\Model\Store
  25. */
  26. protected $_store;
  27. /**
  28. * @var \Magento\Framework\Filter\Input\MaliciousCode
  29. */
  30. protected $_maliciousCode;
  31. /**
  32. * @var PriceCurrencyInterface
  33. */
  34. protected $priceCurrency;
  35. /**
  36. * @var \Magento\Catalog\Block\Product\ImageBuilder
  37. */
  38. protected $imageBuilder;
  39. /**
  40. * @var ImageProvider
  41. */
  42. private $imageProvider;
  43. /**
  44. * @param \Magento\Framework\View\Element\Template\Context $context
  45. * @param \Magento\Framework\Filter\Input\MaliciousCode $maliciousCode
  46. * @param PriceCurrencyInterface $priceCurrency
  47. * @param \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder
  48. * @param array $data
  49. * @param ImageProvider $imageProvider
  50. */
  51. public function __construct(
  52. \Magento\Framework\View\Element\Template\Context $context,
  53. \Magento\Framework\Filter\Input\MaliciousCode $maliciousCode,
  54. PriceCurrencyInterface $priceCurrency,
  55. \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
  56. array $data = [],
  57. ImageProvider $imageProvider = null
  58. ) {
  59. $this->imageBuilder = $imageBuilder;
  60. $this->priceCurrency = $priceCurrency;
  61. $this->_maliciousCode = $maliciousCode;
  62. $this->imageProvider = $imageProvider ?: ObjectManager::getInstance()->get(ImageProvider::class);
  63. parent::__construct($context, $data);
  64. }
  65. /**
  66. * Filter malicious code before insert content to email
  67. *
  68. * @param string|array $content
  69. * @return string|array
  70. */
  71. public function getFilteredContent($content)
  72. {
  73. return $this->_maliciousCode->filter($content);
  74. }
  75. /**
  76. * Set Store scope
  77. *
  78. * @param int|string|\Magento\Store\Model\Website|\Magento\Store\Model\Store $store
  79. * @return $this
  80. */
  81. public function setStore($store)
  82. {
  83. if ($store instanceof \Magento\Store\Model\Website) {
  84. $store = $store->getDefaultStore();
  85. }
  86. if (!$store instanceof \Magento\Store\Model\Store) {
  87. $store = $this->_storeManager->getStore($store);
  88. }
  89. $this->_store = $store;
  90. return $this;
  91. }
  92. /**
  93. * Retrieve current store object
  94. *
  95. * @return \Magento\Store\Model\Store
  96. */
  97. public function getStore()
  98. {
  99. if ($this->_store === null) {
  100. $this->_store = $this->_storeManager->getStore();
  101. }
  102. return $this->_store;
  103. }
  104. /**
  105. * Convert price from default currency to current currency
  106. *
  107. * @param float $price
  108. * @param bool $format Format price to currency format
  109. * @param bool $includeContainer Enclose into <span class="price"><span>
  110. * @return float|string
  111. */
  112. public function formatPrice($price, $format = true, $includeContainer = true)
  113. {
  114. return $format
  115. ? $this->priceCurrency->convertAndFormat($price, $includeContainer)
  116. : $this->priceCurrency->convert($price);
  117. }
  118. /**
  119. * Reset product collection
  120. *
  121. * @return void
  122. */
  123. public function reset()
  124. {
  125. $this->_products = [];
  126. }
  127. /**
  128. * Add product to collection
  129. *
  130. * @param \Magento\Catalog\Model\Product $product
  131. * @return void
  132. */
  133. public function addProduct(\Magento\Catalog\Model\Product $product)
  134. {
  135. $this->_products[$product->getId()] = $product;
  136. }
  137. /**
  138. * Retrieve product collection array
  139. *
  140. * @return \Magento\Catalog\Model\Product[]
  141. */
  142. public function getProducts()
  143. {
  144. return $this->_products;
  145. }
  146. /**
  147. * Get store url params
  148. *
  149. * @return array
  150. */
  151. protected function _getUrlParams()
  152. {
  153. return ['_scope' => $this->getStore(), '_scope_to_url' => true];
  154. }
  155. /**
  156. * @return \Magento\Framework\Pricing\Render
  157. */
  158. protected function getPriceRender()
  159. {
  160. return $this->_layout->createBlock(
  161. \Magento\Framework\Pricing\Render::class,
  162. '',
  163. ['data' => ['price_render_handle' => 'catalog_product_prices']]
  164. );
  165. }
  166. /**
  167. * Return HTML block with tier price
  168. *
  169. * @param \Magento\Catalog\Model\Product $product
  170. * @param string $priceType
  171. * @param string $renderZone
  172. * @param array $arguments
  173. * @return string
  174. */
  175. public function getProductPriceHtml(
  176. \Magento\Catalog\Model\Product $product,
  177. $priceType,
  178. $renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
  179. array $arguments = []
  180. ) {
  181. if (!isset($arguments['zone'])) {
  182. $arguments['zone'] = $renderZone;
  183. }
  184. /** @var \Magento\Framework\Pricing\Render $priceRender */
  185. $priceRender = $this->getPriceRender();
  186. $price = '';
  187. if ($priceRender) {
  188. $price = $priceRender->render(
  189. $priceType,
  190. $product,
  191. $arguments
  192. );
  193. }
  194. return $price;
  195. }
  196. /**
  197. * Retrieve product image.
  198. *
  199. * @param \Magento\Catalog\Model\Product $product
  200. * @param string $imageId
  201. * @param array $attributes
  202. * @return \Magento\Catalog\Block\Product\Image
  203. */
  204. public function getImage($product, $imageId, $attributes = [])
  205. {
  206. return $this->imageProvider->getImage($product, $imageId, $attributes);
  207. }
  208. }