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

Public Member Functions

 __construct (\Magento\Catalog\Block\Product\Context $context, \Magento\Checkout\Model\ResourceModel\Cart $checkoutCart, \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\Module\Manager $moduleManager, array $data=[])
 
 getItemCollection ()
 
 getItems ()
 
 getRowCount ()
 
 setColumnCount ($columns)
 
 getColumnCount ()
 
 resetItemsIterator ()
 
 getIterableItem ()
 
 setItemLimit ($type, $limit)
 
 getItemLimit ($type='')
 
 getIdentities ()
 
- 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

 _prepareData ()
 
 _beforeToHtml ()
 
- 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

 $_columnCount = 4
 
 $_items
 
 $_itemCollection
 
 $_itemLimits = []
 
 $_checkoutSession
 
 $_catalogProductVisibility
 
 $_checkoutCart
 
 $moduleManager
 
- 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

Catalog product upsell items block

@api @SuppressWarnings(PHPMD.LongVariable)

Since
100.0.2

Definition at line 18 of file Upsell.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Block\Product\Context  $context,
\Magento\Checkout\Model\ResourceModel\Cart  $checkoutCart,
\Magento\Catalog\Model\Product\Visibility  $catalogProductVisibility,
\Magento\Checkout\Model\Session  $checkoutSession,
\Magento\Framework\Module\Manager  $moduleManager,
array  $data = [] 
)
Parameters
\Magento\Catalog\Block\Product\Context$context
\Magento\Checkout\Model\ResourceModel\Cart$checkoutCart
\Magento\Catalog\Model\Product\Visibility$catalogProductVisibility
\Magento\Checkout\Model\Session$checkoutSession
\Magento\Framework\Module\Manager$moduleManager
array$data

Definition at line 75 of file Upsell.php.

82  {
83  $this->_checkoutCart = $checkoutCart;
84  $this->_catalogProductVisibility = $catalogProductVisibility;
85  $this->_checkoutSession = $checkoutSession;
86  $this->moduleManager = $moduleManager;
87  parent::__construct(
88  $context,
89  $data
90  );
91  }

Member Function Documentation

◆ _beforeToHtml()

_beforeToHtml ( )
protected
Returns
$this

Definition at line 126 of file Upsell.php.

127  {
128  $this->_prepareData();
129  return parent::_beforeToHtml();
130  }

◆ _prepareData()

_prepareData ( )
protected
Returns
$this

Updating collection with desired items

Definition at line 96 of file Upsell.php.

97  {
98  $product = $this->_coreRegistry->registry('product');
99  /* @var $product \Magento\Catalog\Model\Product */
100  $this->_itemCollection = $product->getUpSellProductCollection()->setPositionOrder()->addStoreFilter();
101  if ($this->moduleManager->isEnabled('Magento_Checkout')) {
102  $this->_addProductAttributesAndPrices($this->_itemCollection);
103  }
104  $this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
105 
106  $this->_itemCollection->load();
107 
111  $this->_eventManager->dispatch(
112  'catalog_product_upsell',
113  ['product' => $product, 'collection' => $this->_itemCollection, 'limit' => null]
114  );
115 
116  foreach ($this->_itemCollection as $product) {
117  $product->setDoNotUseCategoryId(true);
118  }
119 
120  return $this;
121  }
_addProductAttributesAndPrices(\Magento\Catalog\Model\ResourceModel\Product\Collection $collection)

◆ getColumnCount()

getColumnCount ( )
Returns
int

Definition at line 181 of file Upsell.php.

◆ getIdentities()

getIdentities ( )

Return identifiers for produced content

Returns
array

Implements IdentityInterface.

Definition at line 242 of file Upsell.php.

243  {
244  $identities = [];
245  foreach ($this->getItems() as $item) {
246  $identities = array_merge($identities, $item->getIdentities());
247  }
248  return $identities;
249  }

◆ getItemCollection()

getItemCollection ( )
Returns
Collection

getIdentities() depends on _itemCollection populated, but it can be empty if the block is hidden

See also
https://github.com/magento/magento2/issues/5897

Definition at line 135 of file Upsell.php.

136  {
141  if ($this->_itemCollection === null) {
142  $this->_prepareData();
143  }
144  return $this->_itemCollection;
145  }

◆ getItemLimit()

getItemLimit (   $type = '')
Parameters
string$type
Returns
array|int

Definition at line 225 of file Upsell.php.

226  {
227  if ($type == '') {
228  return $this->_itemLimits;
229  }
230  if (isset($this->_itemLimits[$type])) {
231  return $this->_itemLimits[$type];
232  } else {
233  return 0;
234  }
235  }
$type
Definition: item.phtml:13

◆ getItems()

getItems ( )
Returns
\Magento\Framework\DataObject[]

Definition at line 150 of file Upsell.php.

151  {
152  if ($this->_items === null) {
153  $this->_items = $this->getItemCollection()->getItems();
154  }
155  return $this->_items;
156  }

◆ getIterableItem()

getIterableItem ( )
Returns
mixed

Definition at line 198 of file Upsell.php.

199  {
200  $item = current($this->_items);
201  next($this->_items);
202  return $item;
203  }

◆ getRowCount()

getRowCount ( )
Returns
float

Definition at line 161 of file Upsell.php.

◆ resetItemsIterator()

resetItemsIterator ( )
Returns
void

Definition at line 189 of file Upsell.php.

190  {
191  $this->getItems();
192  reset($this->_items);
193  }

◆ setColumnCount()

setColumnCount (   $columns)
Parameters
string$columns
Returns
$this

Definition at line 170 of file Upsell.php.

171  {
172  if ((int) $columns > 0) {
173  $this->_columnCount = (int) $columns;
174  }
175  return $this;
176  }
$columns
Definition: default.phtml:15

◆ setItemLimit()

setItemLimit (   $type,
  $limit 
)

Set how many items we need to show in upsell block Notice: this parameter will be also applied

Parameters
string$type
int$limit
Returns
\Magento\Catalog\Block\Product\ProductList\Upsell

Definition at line 213 of file Upsell.php.

214  {
215  if ((int) $limit > 0) {
216  $this->_itemLimits[$type] = (int) $limit;
217  }
218  return $this;
219  }
$type
Definition: item.phtml:13

Field Documentation

◆ $_catalogProductVisibility

$_catalogProductVisibility
protected

Definition at line 53 of file Upsell.php.

◆ $_checkoutCart

$_checkoutCart
protected

Definition at line 60 of file Upsell.php.

◆ $_checkoutSession

$_checkoutSession
protected

Definition at line 46 of file Upsell.php.

◆ $_columnCount

$_columnCount = 4
protected

Definition at line 24 of file Upsell.php.

◆ $_itemCollection

$_itemCollection
protected

Definition at line 34 of file Upsell.php.

◆ $_itemLimits

$_itemLimits = []
protected

Definition at line 39 of file Upsell.php.

◆ $_items

$_items
protected

Definition at line 29 of file Upsell.php.

◆ $moduleManager

$moduleManager
protected

Definition at line 65 of file Upsell.php.


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