Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Discounts.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
19  protected $storeManager;
20 
24  protected $rssModel;
25 
29  protected $httpContext;
30 
38  public function __construct(
39  \Magento\Framework\View\Element\Template\Context $context,
40  \Magento\Framework\App\Http\Context $httpContext,
41  \Magento\SalesRule\Model\Rss\Discounts $rssModel,
42  \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder,
43  array $data = []
44  ) {
45  $this->storeManager = $context->getStoreManager();
46  $this->rssModel = $rssModel;
47  $this->httpContext = $httpContext;
48  $this->rssUrlBuilder = $rssUrlBuilder;
49  parent::__construct($context, $data);
50  }
51 
55  protected function _construct()
56  {
57  $this->setCacheKey('rss_catalog_salesrule_' . $this->getStoreId() . '_' . $this->getCustomerGroupId());
58  parent::_construct();
59  }
60 
64  public function getRssData()
65  {
66  $storeId = $this->getStoreId();
67  $storeModel = $this->storeManager->getStore($storeId);
68  $websiteId = $storeModel->getWebsiteId();
69  $customerGroupId = $this->getCustomerGroupId();
70  $url = $this->_urlBuilder->getUrl('');
71  $newUrl = $this->rssUrlBuilder->getUrl([
72  'type' => 'discounts',
73  'store_id' => $storeId,
74  'cid' => $customerGroupId,
75  ]);
76  $title = __('%1 - Discounts and Coupons', $storeModel->getFrontendName());
77  $lang = $this->_scopeConfig->getValue(
78  'general/locale/code',
79  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
80  $storeModel
81  );
82 
83  $data = [
84  'title' => $title,
85  'description' => $title,
86  'link' => $newUrl,
87  'charset' => 'UTF-8',
88  'language' => $lang,
89  ];
90 
92  foreach ($this->rssModel->getDiscountCollection($websiteId, $customerGroupId) as $rule) {
93  $toDate = $rule->getToDate()
94  ? '<br/>Discount End Date: ' . $this->formatDate($rule->getToDate(), \IntlDateFormatter::MEDIUM)
95  : '';
96  $couponCode = $rule->getCouponCode() ? '<br/> Coupon Code: ' . $rule->getCouponCode() : '';
97 
98  $description = sprintf(
99  '<table><tr><td style="text-decoration:none;">%s<br/>Discount Start Date: %s %s %s</td></tr></table>',
100  $rule->getDescription(),
101  $this->formatDate($rule->getFromDate(), \IntlDateFormatter::MEDIUM),
102  $toDate,
104  );
105 
106  $data['entries'][] = ['title' => $rule->getName(), 'description' => $description, 'link' => $url];
107  }
108 
109  return $data;
110  }
111 
117  protected function getCustomerGroupId()
118  {
119  $customerGroupId = (int) $this->getRequest()->getParam('cid');
120  if ($customerGroupId == null) {
121  $customerGroupId = $this->httpContext->getValue(Context::CONTEXT_GROUP);
122  }
123  return $customerGroupId;
124  }
125 
129  protected function getStoreId()
130  {
131  $storeId = (int)$this->getRequest()->getParam('store_id');
132  if ($storeId == null) {
133  $storeId = $this->storeManager->getStore()->getId();
134  }
135  return $storeId;
136  }
137 
141  public function getCacheLifetime()
142  {
143  return 0;
144  }
145 
149  public function isAllowed()
150  {
151  return $this->_scopeConfig->isSetFlag(
152  'rss/catalog/discounts',
153  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
154  );
155  }
156 
160  public function getFeeds()
161  {
162  $data = [];
163  if ($this->isAllowed()) {
164  $url = $this->rssUrlBuilder->getUrl([
165  'type' => 'discounts',
166  'store_id' => $this->getStoreId(),
167  'cid' => $this->getCustomerGroupId(),
168  ]);
169  $data = ['label' => __('Coupons/Discounts'), 'link' => $url];
170  }
171  return $data;
172  }
173 
177  public function isAuthRequired()
178  {
179  return false;
180  }
181 }
$title
Definition: default.phtml:14
formatDate( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
__()
Definition: __.php:13
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Http\Context $httpContext, \Magento\SalesRule\Model\Rss\Discounts $rssModel, \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder, array $data=[])
Definition: Discounts.php:38