Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ListProduct.php
Go to the documentation of this file.
1 <?php
8 
26 
34 {
40  protected $_defaultToolbarBlock = Toolbar::class;
41 
48 
54  protected $_catalogLayer;
55 
59  protected $_postDataHelper;
60 
64  protected $urlHelper;
65 
70 
79  public function __construct(
80  Context $context,
81  PostHelper $postDataHelper,
82  Resolver $layerResolver,
85  array $data = []
86  ) {
87  $this->_catalogLayer = $layerResolver->get();
88  $this->_postDataHelper = $postDataHelper;
89  $this->categoryRepository = $categoryRepository;
90  $this->urlHelper = $urlHelper;
91  parent::__construct(
92  $context,
93  $data
94  );
95  }
96 
113  protected function _getProductCollection()
114  {
115  if ($this->_productCollection === null) {
116  $this->_productCollection = $this->initializeProductCollection();
117  }
118 
120  }
121 
127  public function getLayer()
128  {
129  return $this->_catalogLayer;
130  }
131 
137  public function getLoadedProductCollection()
138  {
139  return $this->_getProductCollection();
140  }
141 
147  public function getMode()
148  {
149  if ($this->getChildBlock('toolbar')) {
150  return $this->getChildBlock('toolbar')->getCurrentMode();
151  }
152 
153  return $this->getDefaultListingMode();
154  }
155 
163  private function getDefaultListingMode()
164  {
165  // default Toolbar when the toolbar layout is not used
166  $defaultToolbar = $this->getToolbarBlock();
167  $availableModes = $defaultToolbar->getModes();
168 
169  // layout config mode
170  $mode = $this->getData('mode');
171 
172  if (!$mode || !isset($availableModes[$mode])) {
173  // default config mode
174  $mode = $defaultToolbar->getCurrentMode();
175  }
176 
177  return $mode;
178  }
179 
185  protected function _beforeToHtml()
186  {
188 
189  $this->addToolbarBlock($collection);
190 
191  $collection->load();
192 
193  return parent::_beforeToHtml();
194  }
195 
201  private function addToolbarBlock(Collection $collection)
202  {
203  $toolbarLayout = $this->getToolbarFromLayout();
204 
205  if ($toolbarLayout) {
206  $this->configureToolbar($toolbarLayout, $collection);
207  }
208  }
209 
215  public function getToolbarBlock()
216  {
217  $block = $this->getToolbarFromLayout();
218 
219  if (!$block) {
220  $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
221  }
222 
223  return $block;
224  }
225 
231  private function getToolbarFromLayout()
232  {
233  $blockName = $this->getToolbarBlockName();
234 
235  $toolbarLayout = false;
236 
237  if ($blockName) {
238  $toolbarLayout = $this->getLayout()->getBlock($blockName);
239  }
240 
241  return $toolbarLayout;
242  }
243 
249  public function getAdditionalHtml()
250  {
251  return $this->getChildHtml('additional');
252  }
253 
259  public function getToolbarHtml()
260  {
261  return $this->getChildHtml('toolbar');
262  }
263 
268  public function setCollection($collection)
269  {
270  $this->_productCollection = $collection;
271  return $this;
272  }
273 
278  public function addAttribute($code)
279  {
280  $this->_getProductCollection()->addAttributeToSelect($code);
281  return $this;
282  }
283 
287  public function getPriceBlockTemplate()
288  {
289  return $this->_getData('price_block_template');
290  }
291 
297  protected function _getConfig()
298  {
299  return $this->_catalogConfig;
300  }
301 
309  {
310  if (!$this->getAvailableOrders()) {
311  $this->setAvailableOrders($category->getAvailableSortByOptions());
312  }
313  $availableOrders = $this->getAvailableOrders();
314  if (!$this->getSortBy()) {
315  $categorySortBy = $this->getDefaultSortBy() ?: $category->getDefaultSortBy();
316  if ($categorySortBy) {
317  if (!$availableOrders) {
318  $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
319  }
320  if (isset($availableOrders[$categorySortBy])) {
321  $this->setSortBy($categorySortBy);
322  }
323  }
324  }
325 
326  return $this;
327  }
328 
334  public function getIdentities()
335  {
336  $identities = [];
337 
338  $category = $this->getLayer()->getCurrentCategory();
339  if ($category) {
340  $identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $category->getId();
341  }
342 
343  //Check if category page shows only static block (No products)
344  if ($category->getData('display_mode') == Category::DM_PAGE) {
345  return $identities;
346  }
347 
348  foreach ($this->_getProductCollection() as $item) {
349  $identities = array_merge($identities, $item->getIdentities());
350  }
351 
352  return $identities;
353  }
354 
362  {
363  $url = $this->getAddToCartUrl($product);
364  return [
365  'action' => $url,
366  'data' => [
367  'product' => $product->getEntityId(),
368  ActionInterface::PARAM_NAME_URL_ENCODED => $this->urlHelper->getEncodedUrl($url),
369  ]
370  ];
371  }
372 
378  {
379  $priceRender = $this->getPriceRender();
380 
381  $price = '';
382  if ($priceRender) {
383  $price = $priceRender->render(
385  $product,
386  [
387  'include_container' => true,
388  'display_minimal_price' => true,
389  'zone' => Render::ZONE_ITEM_LIST,
390  'list_category_page' => true
391  ]
392  );
393  }
394 
395  return $price;
396  }
397 
404  protected function getPriceRender()
405  {
406  return $this->getLayout()->getBlock('product.price.render.default')
407  ->setData('is_product_list', true);
408  }
409 
425  private function initializeProductCollection()
426  {
427  $layer = $this->getLayer();
428  /* @var $layer Layer */
429  if ($this->getShowRootCategory()) {
430  $this->setCategoryId($this->_storeManager->getStore()->getRootCategoryId());
431  }
432 
433  // if this is a product view page
434  if ($this->_coreRegistry->registry('product')) {
435  // get collection of categories this product is associated with
436  $categories = $this->_coreRegistry->registry('product')
437  ->getCategoryCollection()->setPage(1, 1)
438  ->load();
439  // if the product is associated with any category
440  if ($categories->count()) {
441  // show products from this category
442  $this->setCategoryId(current($categories->getIterator())->getId());
443  }
444  }
445 
446  $origCategory = null;
447  if ($this->getCategoryId()) {
448  try {
449  $category = $this->categoryRepository->get($this->getCategoryId());
450  } catch (NoSuchEntityException $e) {
451  $category = null;
452  }
453 
454  if ($category) {
455  $origCategory = $layer->getCurrentCategory();
456  $layer->setCurrentCategory($category);
457  }
458  }
459  $collection = $layer->getProductCollection();
460 
461  $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
462 
463  if ($origCategory) {
464  $layer->setCurrentCategory($origCategory);
465  }
466 
467  $this->addToolbarBlock($collection);
468 
469  $this->_eventManager->dispatch(
470  'catalog_block_product_list_collection',
471  ['collection' => $collection]
472  );
473 
474  return $collection;
475  }
476 
488  private function configureToolbar(Toolbar $toolbar, Collection $collection)
489  {
490  // use sortable parameters
491  $orders = $this->getAvailableOrders();
492  if ($orders) {
493  $toolbar->setAvailableOrders($orders);
494  }
495  $sort = $this->getSortBy();
496  if ($sort) {
497  $toolbar->setDefaultOrder($sort);
498  }
499  $dir = $this->getDefaultDirection();
500  if ($dir) {
501  $toolbar->setDefaultDirection($dir);
502  }
503  $modes = $this->getModes();
504  if ($modes) {
505  $toolbar->setModes($modes);
506  }
507  // set collection to toolbar and apply sort
508  $toolbar->setCollection($collection);
509  $this->setChild('toolbar', $toolbar);
510  }
511 }
getData($key='', $index=null)
Definition: DataObject.php:119
$price
$block
Definition: block.php:8
__construct(Context $context, PostHelper $postDataHelper, Resolver $layerResolver, CategoryRepositoryInterface $categoryRepository, Data $urlHelper, array $data=[])
Definition: ListProduct.php:79
Definition: PostHelper.php:15
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$categories
$code
Definition: info.phtml:12