Data.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Helper;
  7. use Magento\Store\Model\Store;
  8. /**
  9. * ProductAlert data helper
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Data extends \Magento\Framework\Url\Helper\Data
  17. {
  18. /**
  19. * Current product instance (override registry one)
  20. *
  21. * @var null|\Magento\Catalog\Model\Product
  22. */
  23. protected $_product = null;
  24. /**
  25. * Core registry
  26. *
  27. * @var \Magento\Framework\Registry
  28. */
  29. protected $_coreRegistry = null;
  30. /**
  31. * @var \Magento\Framework\View\LayoutInterface
  32. */
  33. protected $_layout;
  34. /**
  35. * @var \Magento\Store\Model\StoreManagerInterface
  36. */
  37. private $_storeManager;
  38. /**
  39. * @param \Magento\Framework\App\Helper\Context $context
  40. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  41. * @param \Magento\Framework\Registry $coreRegistry
  42. * @param \Magento\Framework\View\LayoutInterface $layout
  43. */
  44. public function __construct(
  45. \Magento\Framework\App\Helper\Context $context,
  46. \Magento\Store\Model\StoreManagerInterface $storeManager,
  47. \Magento\Framework\Registry $coreRegistry,
  48. \Magento\Framework\View\LayoutInterface $layout
  49. ) {
  50. $this->_coreRegistry = $coreRegistry;
  51. $this->_layout = $layout;
  52. $this->_storeManager = $storeManager;
  53. parent::__construct($context);
  54. }
  55. /**
  56. * Get current product instance
  57. *
  58. * @return \Magento\Catalog\Model\Product
  59. */
  60. public function getProduct()
  61. {
  62. if ($this->_product !== null) {
  63. return $this->_product;
  64. }
  65. return $this->_coreRegistry->registry('product');
  66. }
  67. /**
  68. * Set current product instance
  69. *
  70. * @param \Magento\Catalog\Model\Product $product
  71. * @return \Magento\ProductAlert\Helper\Data
  72. */
  73. public function setProduct($product)
  74. {
  75. $this->_product = $product;
  76. return $this;
  77. }
  78. /**
  79. * @return Store
  80. */
  81. public function getStore()
  82. {
  83. return $this->_storeManager->getStore();
  84. }
  85. /**
  86. * @param string $type
  87. * @return string
  88. */
  89. public function getSaveUrl($type)
  90. {
  91. return $this->_getUrl(
  92. 'productalert/add/' . $type,
  93. [
  94. 'product_id' => $this->getProduct()->getId(),
  95. \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
  96. ]
  97. );
  98. }
  99. /**
  100. * Create block instance
  101. *
  102. * @param string|\Magento\Framework\View\Element\AbstractBlock $block
  103. * @return \Magento\Framework\View\Element\AbstractBlock
  104. * @throws \Magento\Framework\Exception\LocalizedException
  105. */
  106. public function createBlock($block)
  107. {
  108. if (is_string($block)) {
  109. if (class_exists($block)) {
  110. $block = $this->_layout->createBlock($block);
  111. }
  112. }
  113. if (!$block instanceof \Magento\Framework\View\Element\AbstractBlock) {
  114. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid block type: %1', $block));
  115. }
  116. return $block;
  117. }
  118. /**
  119. * Check whether stock alert is allowed
  120. *
  121. * @return bool
  122. */
  123. public function isStockAlertAllowed()
  124. {
  125. return $this->scopeConfig->isSetFlag(
  126. \Magento\ProductAlert\Model\Observer::XML_PATH_STOCK_ALLOW,
  127. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  128. );
  129. }
  130. /**
  131. * Check whether price alert is allowed
  132. *
  133. * @return bool
  134. */
  135. public function isPriceAlertAllowed()
  136. {
  137. return $this->scopeConfig->isSetFlag(
  138. \Magento\ProductAlert\Model\Observer::XML_PATH_PRICE_ALLOW,
  139. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  140. );
  141. }
  142. }