Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Category.php
Go to the documentation of this file.
1 <?php
7 
11 
18 {
22  protected $categoryFactory;
23 
27  protected $imageHelper;
28 
32  protected $customerSession;
33 
37  protected $rssModel;
38 
42  protected $storeManager;
43 
47  protected $rssUrlBuilder;
48 
53 
65  public function __construct(
66  \Magento\Framework\View\Element\Template\Context $context,
67  \Magento\Catalog\Model\CategoryFactory $categoryFactory,
68  \Magento\Catalog\Model\Rss\Category $rssModel,
69  \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder,
70  \Magento\Catalog\Helper\Image $imageHelper,
71  \Magento\Customer\Model\Session $customerSession,
73  array $data = []
74  ) {
75  $this->imageHelper = $imageHelper;
76  $this->categoryFactory = $categoryFactory;
77  $this->customerSession = $customerSession;
78  $this->rssModel = $rssModel;
79  $this->rssUrlBuilder = $rssUrlBuilder;
80  $this->storeManager = $context->getStoreManager();
81  $this->categoryRepository = $categoryRepository;
82  parent::__construct($context, $data);
83  }
84 
88  protected function _construct()
89  {
90  $this->setCacheKey(
91  'rss_catalog_category_'
92  . $this->getRequest()->getParam('cid') . '_'
93  . $this->getStoreId() . '_'
94  . $this->customerSession->getId()
95  );
96  parent::_construct();
97  }
98 
102  public function getRssData()
103  {
104  try {
105  $category = $this->categoryRepository->get($this->getRequest()->getParam('cid'));
106  } catch (NoSuchEntityException $e) {
107  return [
108  'title' => 'Category Not Found',
109  'description' => 'Category Not Found',
110  'link' => $this->getUrl(''),
111  'charset' => 'UTF-8'
112  ];
113  }
114 
115  $category->setIsAnchor(true);
116  $newUrl = $category->getUrl();
117  $title = $category->getName();
118  $data = ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8'];
119 
121  foreach ($this->rssModel->getProductCollection($category, $this->getStoreId()) as $product) {
122  $product->setAllowedInRss(true);
123  $product->setAllowedPriceInRss(true);
124 
125  $this->_eventManager->dispatch('rss_catalog_category_xml_callback', ['product' => $product]);
126 
127  if (!$product->getAllowedInRss()) {
128  continue;
129  }
130 
131  $description = '
132  <table><tr>
133  <td><a href="%s"><img src="%s" border="0" align="left" height="75" width="75"></a></td>
134  <td style="text-decoration:none;">%s %s</td>
135  </tr></table>
136  ';
137 
138  $description = sprintf(
139  $description,
140  $product->getProductUrl(),
141  $this->imageHelper->init($product, 'rss_thumbnail')->getUrl(),
142  $product->getDescription(),
143  $product->getAllowedPriceInRss() ? $this->renderPriceHtml($product) : ''
144  );
145 
146  $data['entries'][] = [
147  'title' => $product->getName(),
148  'link' => $product->getProductUrl(),
149  'description' => $description,
150  ];
151  }
152  return $data;
153  }
154 
161  protected function renderPriceHtml(\Magento\Catalog\Model\Product $product)
162  {
164  $priceRender = $this->getLayout()->getBlock('product.price.render.default');
165  if (!$priceRender) {
166  $priceRender = $this->getLayout()->createBlock(
167  \Magento\Framework\Pricing\Render::class,
168  'product.price.render.default',
169  ['data' => ['price_render_handle' => 'catalog_product_prices']]
170  );
171  }
172 
173  $price = '';
174  if ($priceRender) {
175  $price = $priceRender->render(
176  \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
177  $product,
178  [
179  'display_minimal_price' => true,
180  'use_link_for_as_low_as' => true,
181  'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
182  ]
183  );
184  }
185 
186  return $price;
187  }
188 
192  protected function getStoreId()
193  {
194  $storeId = (int)$this->getRequest()->getParam('store_id');
195  if ($storeId == null) {
196  $storeId = $this->storeManager->getStore()->getId();
197  }
198  return $storeId;
199  }
200 
204  public function getCacheLifetime()
205  {
206  return 600;
207  }
208 
212  public function isAllowed()
213  {
214  return $this->_scopeConfig->isSetFlag(
215  'rss/catalog/category',
217  );
218  }
219 
223  public function getFeeds()
224  {
225  $result = [];
226  if ($this->isAllowed()) {
228  $category = $this->categoryFactory->create();
229  $treeModel = $category->getTreeModel()->loadNode($this->storeManager->getStore()->getRootCategoryId());
230  $nodes = $treeModel->loadChildren()->getChildren();
231 
232  $nodeIds = [];
233  foreach ($nodes as $node) {
234  $nodeIds[] = $node->getId();
235  }
236 
237  /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
238  $collection = $category->getResourceCollection();
239  $collection->addIdFilter($nodeIds)
240  ->addAttributeToSelect('url_key')
241  ->addAttributeToSelect('name')
242  ->addAttributeToSelect('is_anchor')
243  ->addAttributeToFilter('is_active', 1)
244  ->addAttributeToSort('name')
245  ->load();
246 
247  $feeds = [];
248  foreach ($collection as $category) {
249  $feeds[] = [
250  'label' => $category->getName(),
251  'link' => $this->rssUrlBuilder->getUrl(['type' => 'category', 'cid' => $category->getId()]),
252  ];
253  }
254  $result = ['group' => 'Categories', 'feeds' => $feeds];
255  }
256  return $result;
257  }
258 
262  public function isAuthRequired()
263  {
264  return false;
265  }
266 }
$title
Definition: default.phtml:14
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Catalog\Model\Rss\Category $rssModel, \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder, \Magento\Catalog\Helper\Image $imageHelper, \Magento\Customer\Model\Session $customerSession, CategoryRepositoryInterface $categoryRepository, array $data=[])
Definition: Category.php:65
$price