Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
ListProduct Class Reference
Inheritance diagram for ListProduct:
AbstractProduct IdentityInterface Template AbstractBlock DataObject BlockInterface Promotion Random

Public Member Functions

 __construct (Context $context, PostHelper $postDataHelper, Resolver $layerResolver, CategoryRepositoryInterface $categoryRepository, Data $urlHelper, array $data=[])
 
 getLayer ()
 
 getLoadedProductCollection ()
 
 getMode ()
 
 getToolbarBlock ()
 
 getAdditionalHtml ()
 
 getToolbarHtml ()
 
 setCollection ($collection)
 
 addAttribute ($code)
 
 getPriceBlockTemplate ()
 
 prepareSortableFieldsByCategory ($category)
 
 getIdentities ()
 
 getAddToCartPostParams (Product $product)
 
 getProductPrice (Product $product)
 
- Public Member Functions inherited from AbstractProduct
 __construct (\Magento\Catalog\Block\Product\Context $context, array $data=[])
 
 getAddToCartUrl ($product, $additional=[])
 
 getSubmitUrl ($product, $additional=[])
 
 getAddToWishlistParams ($product)
 
 getAddToCompareUrl ()
 
 getMinimalQty ($product)
 
 getReviewsSummaryHtml (\Magento\Catalog\Model\Product $product, $templateType=false, $displayIfNoReviews=false)
 
 getProduct ()
 
 getProductUrl ($product, $additional=[])
 
 hasProductUrl ($product)
 
 getColumnCount ()
 
 addColumnCountLayoutDepend ($pageLayout, $columnCount)
 
 removeColumnCountLayoutDepend ($pageLayout)
 
 getColumnCountLayoutDepend ($pageLayout)
 
 getPageLayout ()
 
 getCanShowProductPrice ($product)
 
 displayProductStockStatus ()
 
 getRandomString ($length, $chars=null)
 
 getProductPrice (\Magento\Catalog\Model\Product $product)
 
 isRedirectToCartEnabled ()
 
 getProductDetailsHtml (\Magento\Catalog\Model\Product $product)
 
 getDetailsRenderer ($type=null)
 
 getImage ($product, $imageId, $attributes=[])
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Member Functions

 _getProductCollection ()
 
 _beforeToHtml ()
 
 _getConfig ()
 
 getPriceRender ()
 
- Protected Member Functions inherited from AbstractProduct
 _addProductAttributesAndPrices (\Magento\Catalog\Model\ResourceModel\Product\Collection $collection)
 
 getDetailsRendererList ()
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_defaultToolbarBlock = Toolbar::class
 
 $_productCollection
 
 $_catalogLayer
 
 $_postDataHelper
 
 $urlHelper
 
 $categoryRepository
 
- Protected Attributes inherited from AbstractProduct
 $_priceBlock = []
 
 $_useLinkForAsLowAs = true
 
 $_defaultColumnCount = 3
 
 $_columnCountLayoutDepend = []
 
 $_coreRegistry
 
 $_taxData
 
 $_catalogConfig
 
 $_mathRandom
 
 $_cartHelper
 
 $_wishlistHelper
 
 $_compareProduct
 
 $_imageHelper
 
 $reviewRenderer
 
 $stockRegistry
 
 $imageBuilder
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Product list @api @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 33 of file ListProduct.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Context  $context,
PostHelper  $postDataHelper,
Resolver  $layerResolver,
CategoryRepositoryInterface  $categoryRepository,
Data  $urlHelper,
array  $data = [] 
)
Parameters
Context$context
PostHelper$postDataHelper
Resolver$layerResolver
CategoryRepositoryInterface$categoryRepository
Data$urlHelper
array$data

Definition at line 79 of file ListProduct.php.

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  }

Member Function Documentation

◆ _beforeToHtml()

_beforeToHtml ( )
protected

Need use as _prepareLayout - but problem in declaring collection from another block (was problem with search result)

Returns
$this

Definition at line 185 of file ListProduct.php.

186  {
188 
189  $this->addToolbarBlock($collection);
190 
191  $collection->load();
192 
193  return parent::_beforeToHtml();
194  }

◆ _getConfig()

_getConfig ( )
protected

Retrieve Catalog Config object

Returns
Config

Definition at line 297 of file ListProduct.php.

◆ _getProductCollection()

_getProductCollection ( )
protected

Retrieve loaded product collection

The goal of this method is to choose whether the existing collection should be returned or a new one should be initialized.

It is not just a caching logic, but also is a real logical check because there are two ways how collection may be stored inside the block:

  • Product collection may be passed externally by 'setCollection' method
  • Product collection may be requested internally from the current Catalog Layer.

And this method will return collection anyway, even when it did not pass externally and therefore isn't cached yet

Returns
AbstractCollection

Definition at line 113 of file ListProduct.php.

114  {
115  if ($this->_productCollection === null) {
116  $this->_productCollection = $this->initializeProductCollection();
117  }
118 
120  }

◆ addAttribute()

addAttribute (   $code)
Parameters
array | string | integer |  Element$code
Returns
$this

Definition at line 278 of file ListProduct.php.

279  {
280  $this->_getProductCollection()->addAttributeToSelect($code);
281  return $this;
282  }
$code
Definition: info.phtml:12

◆ getAdditionalHtml()

getAdditionalHtml ( )

Retrieve additional blocks html

Returns
string

Definition at line 249 of file ListProduct.php.

250  {
251  return $this->getChildHtml('additional');
252  }

◆ getAddToCartPostParams()

getAddToCartPostParams ( Product  $product)

Get post parameters

Parameters
Product$product
Returns
array

Definition at line 361 of file ListProduct.php.

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  }

◆ getIdentities()

getIdentities ( )

Return identifiers for produced content

Returns
array

Implements IdentityInterface.

Definition at line 334 of file ListProduct.php.

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  }

◆ getLayer()

getLayer ( )

Get catalog layer model

Returns
Layer

Definition at line 127 of file ListProduct.php.

◆ getLoadedProductCollection()

getLoadedProductCollection ( )

Retrieve loaded category collection

Returns
AbstractCollection

Definition at line 137 of file ListProduct.php.

138  {
139  return $this->_getProductCollection();
140  }

◆ getMode()

getMode ( )

Retrieve current view mode

Returns
string

Definition at line 147 of file ListProduct.php.

148  {
149  if ($this->getChildBlock('toolbar')) {
150  return $this->getChildBlock('toolbar')->getCurrentMode();
151  }
152 
153  return $this->getDefaultListingMode();
154  }

◆ getPriceBlockTemplate()

getPriceBlockTemplate ( )
Returns
mixed

Definition at line 287 of file ListProduct.php.

288  {
289  return $this->_getData('price_block_template');
290  }

◆ getPriceRender()

getPriceRender ( )
protected

Specifies that price rendering should be done for the list of products i.e. rendering happens in the scope of product list, but not single product

Returns
Render

Definition at line 404 of file ListProduct.php.

405  {
406  return $this->getLayout()->getBlock('product.price.render.default')
407  ->setData('is_product_list', true);
408  }

◆ getProductPrice()

getProductPrice ( Product  $product)
Parameters
Product$product
Returns
string

Definition at line 377 of file ListProduct.php.

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  }
$price

◆ getToolbarBlock()

getToolbarBlock ( )

Retrieve Toolbar block from layout or a default Toolbar

Returns
Toolbar

Definition at line 215 of file ListProduct.php.

216  {
217  $block = $this->getToolbarFromLayout();
218 
219  if (!$block) {
220  $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
221  }
222 
223  return $block;
224  }
$block
Definition: block.php:8

◆ getToolbarHtml()

getToolbarHtml ( )

Retrieve list toolbar HTML

Returns
string

Definition at line 259 of file ListProduct.php.

260  {
261  return $this->getChildHtml('toolbar');
262  }

◆ prepareSortableFieldsByCategory()

prepareSortableFieldsByCategory (   $category)

Prepare Sort By fields from Category Data

Parameters
Category$category
Returns
$this

Definition at line 308 of file ListProduct.php.

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  }

◆ setCollection()

setCollection (   $collection)
Parameters
AbstractCollection$collection
Returns
$this

Definition at line 268 of file ListProduct.php.

269  {
270  $this->_productCollection = $collection;
271  return $this;
272  }

Field Documentation

◆ $_catalogLayer

$_catalogLayer
protected

Definition at line 54 of file ListProduct.php.

◆ $_defaultToolbarBlock

$_defaultToolbarBlock = Toolbar::class
protected

Definition at line 40 of file ListProduct.php.

◆ $_postDataHelper

$_postDataHelper
protected

Definition at line 59 of file ListProduct.php.

◆ $_productCollection

$_productCollection
protected

Definition at line 47 of file ListProduct.php.

◆ $categoryRepository

$categoryRepository
protected

Definition at line 69 of file ListProduct.php.

◆ $urlHelper

$urlHelper
protected

Definition at line 64 of file ListProduct.php.


The documentation for this class was generated from the following file: