Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
AbstractProduct Class Reference
Inheritance diagram for AbstractProduct:
Template AbstractBlock DataObject BlockInterface ListCompare ListProduct NewProduct Crosssell Block Related Upsell View AbstractView ProductsList Crosssell Links Samples AbstractProduct Item View View AbstractBlock

Public Member Functions

 __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

 _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

 $_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

Class AbstractProduct @api

Deprecated:
102.0.0 @SuppressWarnings(PHPMD.NumberOfChildren) @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Since
100.0.2

Definition at line 16 of file AbstractProduct.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Block\Product\Context  $context,
array  $data = [] 
)
Parameters
Context$context
array$data

Definition at line 110 of file AbstractProduct.php.

111  {
112  $this->_imageHelper = $context->getImageHelper();
113  $this->imageBuilder = $context->getImageBuilder();
114  $this->_compareProduct = $context->getCompareProduct();
115  $this->_wishlistHelper = $context->getWishlistHelper();
116  $this->_cartHelper = $context->getCartHelper();
117  $this->_catalogConfig = $context->getCatalogConfig();
118  $this->_coreRegistry = $context->getRegistry();
119  $this->_taxData = $context->getTaxData();
120  $this->_mathRandom = $context->getMathRandom();
121  $this->reviewRenderer = $context->getReviewRenderer();
122  $this->stockRegistry = $context->getStockRegistry();
123  parent::__construct($context, $data);
124  }

Member Function Documentation

◆ _addProductAttributesAndPrices()

_addProductAttributesAndPrices ( \Magento\Catalog\Model\ResourceModel\Product\Collection  $collection)
protected

Add all attributes and apply pricing logic to products collection to get correct values in different products lists. E.g. crosssells, upsells, new products, recently viewed

Parameters
\Magento\Catalog\Model\ResourceModel\Product\Collection$collection
Returns
\Magento\Catalog\Model\ResourceModel\Product\Collection

Definition at line 244 of file AbstractProduct.php.

246  {
247  return $collection
248  ->addMinimalPrice()
249  ->addFinalPrice()
250  ->addTaxPercents()
251  ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
252  ->addUrlRewrite();
253  }

◆ addColumnCountLayoutDepend()

addColumnCountLayoutDepend (   $pageLayout,
  $columnCount 
)

Add row size depends on page layout

Parameters
string$pageLayout
int$columnCount
Returns
\Magento\Catalog\Block\Product\ListProduct

Definition at line 319 of file AbstractProduct.php.

320  {
321  $this->_columnCountLayoutDepend[$pageLayout] = $columnCount;
322  return $this;
323  }

◆ displayProductStockStatus()

displayProductStockStatus ( )

Get if it is necessary to show product stock status

Returns
bool

Definition at line 383 of file AbstractProduct.php.

384  {
385  $statusInfo = new \Magento\Framework\DataObject(['display_status' => true]);
386  $this->_eventManager->dispatch('catalog_block_product_status_display', ['status' => $statusInfo]);
387  return (bool) $statusInfo->getDisplayStatus();
388  }

◆ getAddToCartUrl()

getAddToCartUrl (   $product,
  $additional = [] 
)

Retrieve url for add product to cart Will return product view page URL if product has required options

Parameters
\Magento\Catalog\Model\Product$product
array$additional
Returns
string

Definition at line 134 of file AbstractProduct.php.

135  {
136  if (!$product->getTypeInstance()->isPossibleBuyFromList($product)) {
137  if (!isset($additional['_escape'])) {
138  $additional['_escape'] = true;
139  }
140  if (!isset($additional['_query'])) {
141  $additional['_query'] = [];
142  }
143  $additional['_query']['options'] = 'cart';
144 
145  return $this->getProductUrl($product, $additional);
146  }
147  return $this->_cartHelper->getAddUrl($product, $additional);
148  }

◆ getAddToCompareUrl()

getAddToCompareUrl ( )

Retrieve Add Product to Compare Products List URL

Returns
string

Definition at line 189 of file AbstractProduct.php.

190  {
191  return $this->_compareProduct->getAddUrl();
192  }

◆ getAddToWishlistParams()

getAddToWishlistParams (   $product)

Retrieve add to wishlist params

Parameters
\Magento\Catalog\Model\Product$product
Returns
string

Definition at line 179 of file AbstractProduct.php.

180  {
181  return $this->_wishlistHelper->getAddParams($product);
182  }

◆ getCanShowProductPrice()

getCanShowProductPrice (   $product)

Check whether the price can be shown for the specified product

Parameters
\Magento\Catalog\Model\Product$product
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 373 of file AbstractProduct.php.

374  {
375  return $product->getCanShowPrice() !== false;
376  }

◆ getColumnCount()

getColumnCount ( )

Retrieve product amount per row

Returns
int

Definition at line 299 of file AbstractProduct.php.

300  {
301  if (!$this->_getData('column_count')) {
302  $pageLayout = $this->getPageLayout();
303  if ($pageLayout && $this->getColumnCountLayoutDepend($pageLayout->getCode())) {
304  $this->setData('column_count', $this->getColumnCountLayoutDepend($pageLayout->getCode()));
305  } else {
306  $this->setData('column_count', $this->_defaultColumnCount);
307  }
308  }
309  return (int) $this->_getData('column_count');
310  }
setData($key, $value=null)
Definition: DataObject.php:72

◆ getColumnCountLayoutDepend()

getColumnCountLayoutDepend (   $pageLayout)

Retrieve row size depends on page layout

Parameters
string$pageLayout
Returns
int|boolean

Definition at line 346 of file AbstractProduct.php.

347  {
348  if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
349  return $this->_columnCountLayoutDepend[$pageLayout];
350  }
351 
352  return false;
353  }

◆ getDetailsRenderer()

getDetailsRenderer (   $type = null)
Parameters
null$type
Returns
bool|\Magento\Framework\View\Element\AbstractBlock

Definition at line 479 of file AbstractProduct.php.

480  {
481  if ($type === null) {
482  $type = 'default';
483  }
484  $rendererList = $this->getDetailsRendererList();
485  if ($rendererList) {
486  return $rendererList->getRenderer($type, 'default');
487  }
488  return null;
489  }
$type
Definition: item.phtml:13

◆ getDetailsRendererList()

getDetailsRendererList ( )
protected
Returns
\Magento\Framework\View\Element\RendererList

Definition at line 494 of file AbstractProduct.php.

495  {
496  return $this->getDetailsRendererListName() ? $this->getLayout()->getBlock(
497  $this->getDetailsRendererListName()
498  ) : $this->getChildBlock(
499  'details.renderers'
500  );
501  }

◆ getImage()

getImage (   $product,
  $imageId,
  $attributes = [] 
)

Retrieve product image

Parameters
\Magento\Catalog\Model\Product$product
string$imageId
array$attributes
Returns
\Magento\Catalog\Block\Product\Image

Definition at line 511 of file AbstractProduct.php.

512  {
513  return $this->imageBuilder->create($product, $imageId, $attributes);
514  }
$attributes
Definition: matrix.phtml:13

◆ getMinimalQty()

getMinimalQty (   $product)

Gets minimal sales quantity

Parameters
\Magento\Catalog\Model\Product$product
Returns
float|null

Definition at line 200 of file AbstractProduct.php.

201  {
202  $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
203  $minSaleQty = $stockItem->getMinSaleQty();
204  return $minSaleQty > 0 ? $minSaleQty : null;
205  }

◆ getPageLayout()

getPageLayout ( )

Retrieve current page layout

Returns
string

Definition at line 360 of file AbstractProduct.php.

361  {
362  // TODO: Implement of getting current page layout
363  return '';
364  }

◆ getProduct()

getProduct ( )

Retrieve currently viewed product object

Returns
\Magento\Catalog\Model\Product

Definition at line 228 of file AbstractProduct.php.

229  {
230  if (!$this->hasData('product')) {
231  $this->setData('product', $this->_coreRegistry->registry('product'));
232  }
233  return $this->getData('product');
234  }
getData($key='', $index=null)
Definition: DataObject.php:119
setData($key, $value=null)
Definition: DataObject.php:72

◆ getProductDetailsHtml()

getProductDetailsHtml ( \Magento\Catalog\Model\Product  $product)

Retrieve product details html

Parameters
\Magento\Catalog\Model\Product$product
Returns
mixed

Definition at line 465 of file AbstractProduct.php.

466  {
467  $renderer = $this->getDetailsRenderer($product->getTypeId());
468  if ($renderer) {
469  $renderer->setProduct($product);
470  return $renderer->toHtml();
471  }
472  return '';
473  }

◆ getProductPrice()

getProductPrice ( \Magento\Catalog\Model\Product  $product)

Return HTML block with price

Parameters
\Magento\Catalog\Model\Product$product
Returns
string

Definition at line 408 of file AbstractProduct.php.

409  {
410  return $this->getProductPriceHtml(
411  $product,
412  \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
413  \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
414  );
415  }

◆ getProductUrl()

getProductUrl (   $product,
  $additional = [] 
)

Retrieve Product URL using UrlDataObject

Parameters
\Magento\Catalog\Model\Product$product
array$additionalthe route params
Returns
string

Definition at line 262 of file AbstractProduct.php.

263  {
264  if ($this->hasProductUrl($product)) {
265  if (!isset($additional['_escape'])) {
266  $additional['_escape'] = true;
267  }
268  return $product->getUrlModel()->getUrl($product, $additional);
269  }
270 
271  return '#';
272  }

◆ getRandomString()

getRandomString (   $length,
  $chars = null 
)

Get random string

Parameters
int$length
string | null$chars
Returns
string

Definition at line 397 of file AbstractProduct.php.

398  {
399  return $this->_mathRandom->getRandomString($length, $chars);
400  }

◆ getReviewsSummaryHtml()

getReviewsSummaryHtml ( \Magento\Catalog\Model\Product  $product,
  $templateType = false,
  $displayIfNoReviews = false 
)

Get product reviews summary

Parameters
\Magento\Catalog\Model\Product$product
bool$templateType
bool$displayIfNoReviews
Returns
string

Definition at line 215 of file AbstractProduct.php.

219  {
220  return $this->reviewRenderer->getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
221  }
$templateType
Definition: list.phtml:37

◆ getSubmitUrl()

getSubmitUrl (   $product,
  $additional = [] 
)

Retrieves url for form submitting.

Some objects can use setSubmitRouteData() to set route and params for form submitting, otherwise default url will be used

Parameters
\Magento\Catalog\Model\Product$product
array$additional
Returns
string

Definition at line 160 of file AbstractProduct.php.

161  {
162  $submitRouteData = $this->getData('submit_route_data');
163  if ($submitRouteData) {
164  $route = $submitRouteData['route'];
165  $params = isset($submitRouteData['params']) ? $submitRouteData['params'] : [];
166  $submitUrl = $this->getUrl($route, array_merge($params, $additional));
167  } else {
168  $submitUrl = $this->getAddToCartUrl($product, $additional);
169  }
170  return $submitUrl;
171  }
getData($key='', $index=null)
Definition: DataObject.php:119
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ hasProductUrl()

hasProductUrl (   $product)

Check Product has URL

Parameters
\Magento\Catalog\Model\Product$product
Returns
bool

Definition at line 280 of file AbstractProduct.php.

281  {
282  if ($product->getVisibleInSiteVisibilities()) {
283  return true;
284  }
285  if ($product->hasUrlDataObject()) {
286  if (in_array($product->hasUrlDataObject()->getVisibility(), $product->getVisibleInSiteVisibilities())) {
287  return true;
288  }
289  }
290 
291  return false;
292  }

◆ isRedirectToCartEnabled()

isRedirectToCartEnabled ( )

Whether redirect to cart enabled

Returns
bool

Definition at line 451 of file AbstractProduct.php.

452  {
453  return $this->_scopeConfig->getValue(
454  'checkout/cart/redirect_to_cart',
456  );
457  }

◆ removeColumnCountLayoutDepend()

removeColumnCountLayoutDepend (   $pageLayout)

Remove row size depends on page layout

Parameters
string$pageLayout
Returns
\Magento\Catalog\Block\Product\ListProduct

Definition at line 331 of file AbstractProduct.php.

332  {
333  if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
334  unset($this->_columnCountLayoutDepend[$pageLayout]);
335  }
336 
337  return $this;
338  }

Field Documentation

◆ $_cartHelper

$_cartHelper
protected

Definition at line 73 of file AbstractProduct.php.

◆ $_catalogConfig

$_catalogConfig
protected

Definition at line 63 of file AbstractProduct.php.

◆ $_columnCountLayoutDepend

$_columnCountLayoutDepend = []
protected

Definition at line 42 of file AbstractProduct.php.

◆ $_compareProduct

$_compareProduct
protected

Definition at line 83 of file AbstractProduct.php.

◆ $_coreRegistry

$_coreRegistry
protected

Definition at line 49 of file AbstractProduct.php.

◆ $_defaultColumnCount

$_defaultColumnCount = 3
protected

Definition at line 35 of file AbstractProduct.php.

◆ $_imageHelper

$_imageHelper
protected

Definition at line 88 of file AbstractProduct.php.

◆ $_mathRandom

$_mathRandom
protected

Definition at line 68 of file AbstractProduct.php.

◆ $_priceBlock

$_priceBlock = []
protected

Definition at line 21 of file AbstractProduct.php.

◆ $_taxData

$_taxData
protected

Definition at line 56 of file AbstractProduct.php.

◆ $_useLinkForAsLowAs

$_useLinkForAsLowAs = true
protected

Definition at line 28 of file AbstractProduct.php.

◆ $_wishlistHelper

$_wishlistHelper
protected

Definition at line 78 of file AbstractProduct.php.

◆ $imageBuilder

$imageBuilder
protected

Definition at line 104 of file AbstractProduct.php.

◆ $reviewRenderer

$reviewRenderer
protected

Definition at line 93 of file AbstractProduct.php.

◆ $stockRegistry

$stockRegistry
protected

Definition at line 98 of file AbstractProduct.php.


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