Discounts.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Block\Rss;
  7. use Magento\Customer\Model\Context;
  8. use Magento\Framework\App\Rss\DataProviderInterface;
  9. /**
  10. * Review form block
  11. */
  12. class Discounts extends \Magento\Framework\View\Element\AbstractBlock implements DataProviderInterface
  13. {
  14. /**
  15. * @var \Magento\Store\Model\StoreManagerInterface
  16. */
  17. protected $storeManager;
  18. /**
  19. * @var \Magento\SalesRule\Model\Rss\Discounts
  20. */
  21. protected $rssModel;
  22. /**
  23. * @var \Magento\Framework\App\Http\Context
  24. */
  25. protected $httpContext;
  26. /**
  27. * @param \Magento\Framework\View\Element\Template\Context $context
  28. * @param \Magento\Framework\App\Http\Context $httpContext
  29. * @param \Magento\SalesRule\Model\Rss\Discounts $rssModel
  30. * @param \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder
  31. * @param array $data
  32. */
  33. public function __construct(
  34. \Magento\Framework\View\Element\Template\Context $context,
  35. \Magento\Framework\App\Http\Context $httpContext,
  36. \Magento\SalesRule\Model\Rss\Discounts $rssModel,
  37. \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder,
  38. array $data = []
  39. ) {
  40. $this->storeManager = $context->getStoreManager();
  41. $this->rssModel = $rssModel;
  42. $this->httpContext = $httpContext;
  43. $this->rssUrlBuilder = $rssUrlBuilder;
  44. parent::__construct($context, $data);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function _construct()
  50. {
  51. $this->setCacheKey('rss_catalog_salesrule_' . $this->getStoreId() . '_' . $this->getCustomerGroupId());
  52. parent::_construct();
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getRssData()
  58. {
  59. $storeId = $this->getStoreId();
  60. $storeModel = $this->storeManager->getStore($storeId);
  61. $websiteId = $storeModel->getWebsiteId();
  62. $customerGroupId = $this->getCustomerGroupId();
  63. $url = $this->_urlBuilder->getUrl('');
  64. $newUrl = $this->rssUrlBuilder->getUrl([
  65. 'type' => 'discounts',
  66. 'store_id' => $storeId,
  67. 'cid' => $customerGroupId,
  68. ]);
  69. $title = __('%1 - Discounts and Coupons', $storeModel->getFrontendName());
  70. $lang = $this->_scopeConfig->getValue(
  71. 'general/locale/code',
  72. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  73. $storeModel
  74. );
  75. $data = [
  76. 'title' => $title,
  77. 'description' => $title,
  78. 'link' => $newUrl,
  79. 'charset' => 'UTF-8',
  80. 'language' => $lang,
  81. ];
  82. /** @var $rule \Magento\SalesRule\Model\Rule */
  83. foreach ($this->rssModel->getDiscountCollection($websiteId, $customerGroupId) as $rule) {
  84. $toDate = $rule->getToDate()
  85. ? '<br/>Discount End Date: ' . $this->formatDate($rule->getToDate(), \IntlDateFormatter::MEDIUM)
  86. : '';
  87. $couponCode = $rule->getCouponCode() ? '<br/> Coupon Code: ' . $rule->getCouponCode() : '';
  88. $description = sprintf(
  89. '<table><tr><td style="text-decoration:none;">%s<br/>Discount Start Date: %s %s %s</td></tr></table>',
  90. $rule->getDescription(),
  91. $this->formatDate($rule->getFromDate(), \IntlDateFormatter::MEDIUM),
  92. $toDate,
  93. $couponCode
  94. );
  95. $data['entries'][] = ['title' => $rule->getName(), 'description' => $description, 'link' => $url];
  96. }
  97. return $data;
  98. }
  99. /**
  100. * Get customer group id
  101. *
  102. * @return int
  103. */
  104. protected function getCustomerGroupId()
  105. {
  106. $customerGroupId = (int) $this->getRequest()->getParam('cid');
  107. if ($customerGroupId == null) {
  108. $customerGroupId = $this->httpContext->getValue(Context::CONTEXT_GROUP);
  109. }
  110. return $customerGroupId;
  111. }
  112. /**
  113. * @return int
  114. */
  115. protected function getStoreId()
  116. {
  117. $storeId = (int)$this->getRequest()->getParam('store_id');
  118. if ($storeId == null) {
  119. $storeId = $this->storeManager->getStore()->getId();
  120. }
  121. return $storeId;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getCacheLifetime()
  127. {
  128. return 0;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function isAllowed()
  134. {
  135. return $this->_scopeConfig->isSetFlag(
  136. 'rss/catalog/discounts',
  137. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  138. );
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function getFeeds()
  144. {
  145. $data = [];
  146. if ($this->isAllowed()) {
  147. $url = $this->rssUrlBuilder->getUrl([
  148. 'type' => 'discounts',
  149. 'store_id' => $this->getStoreId(),
  150. 'cid' => $this->getCustomerGroupId(),
  151. ]);
  152. $data = ['label' => __('Coupons/Discounts'), 'link' => $url];
  153. }
  154. return $data;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function isAuthRequired()
  160. {
  161. return false;
  162. }
  163. }