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

Public Member Functions

 __construct (\Magento\Catalog\Block\Product\Context $context, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory $itemCollectionFactory, \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility, \Magento\Customer\Model\Visitor $customerVisitor, \Magento\Framework\App\Http\Context $httpContext, \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, array $data=[])
 
 getAddToWishlistParams ($product)
 
 getItems ()
 
 getAttributes ()
 
 getProductAttributeValue ($product, $attribute)
 
 hasAttributeValueForProducts ($attribute)
 
 getPrintUrl ()
 
 setCustomerId ($id)
 
- 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

 _prepareLayout ()
 
- 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

 $_items
 
 $_attributes
 
 $_useLinkForAsLowAs = false
 
 $_customerId = null
 
 $httpContext
 
 $_customerVisitor
 
 $_catalogProductVisibility
 
 $_itemCollectionFactory
 
 $urlEncoder
 
 $currentCustomer
 
- 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 products compare block @api @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 19 of file ListCompare.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Block\Product\Context  $context,
\Magento\Framework\Url\EncoderInterface  $urlEncoder,
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory  $itemCollectionFactory,
\Magento\Catalog\Model\Product\Visibility  $catalogProductVisibility,
\Magento\Customer\Model\Visitor  $customerVisitor,
\Magento\Framework\App\Http\Context  $httpContext,
\Magento\Customer\Helper\Session\CurrentCustomer  $currentCustomer,
array  $data = [] 
)
Parameters
\Magento\Catalog\Block\Product\Context$context
\Magento\Framework\Url\EncoderInterface$urlEncoder
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory$itemCollectionFactory
Product\Visibility$catalogProductVisibility
\Magento\Customer\Model\Visitor$customerVisitor
\Magento\Framework\App\Http\Context$httpContext
\Magento\Customer\Helper\Session\CurrentCustomer$currentCustomer
array$data

Definition at line 95 of file ListCompare.php.

104  {
105  $this->urlEncoder = $urlEncoder;
106  $this->_itemCollectionFactory = $itemCollectionFactory;
107  $this->_catalogProductVisibility = $catalogProductVisibility;
108  $this->_customerVisitor = $customerVisitor;
109  $this->httpContext = $httpContext;
110  $this->currentCustomer = $currentCustomer;
111  parent::__construct(
112  $context,
113  $data
114  );
115  }

Member Function Documentation

◆ _prepareLayout()

_prepareLayout ( )
protected

Preparing layout

Returns
\Magento\Catalog\Block\Product\Compare\ListCompare

Definition at line 138 of file ListCompare.php.

139  {
140  $this->pageConfig->getTitle()->set(
141  __('Products Comparison List') . ' - ' . $this->pageConfig->getTitle()->getDefault()
142  );
143  return parent::_prepareLayout();
144  }
__()
Definition: __.php:13

◆ getAddToWishlistParams()

getAddToWishlistParams (   $product)

Get add to wishlist params

Parameters
Product$product
Returns
string

Definition at line 123 of file ListCompare.php.

124  {
125  $continueUrl = $this->urlEncoder->encode($this->getUrl('customer/account'));
126  $urlParamName = Action::PARAM_NAME_URL_ENCODED;
127 
128  $continueUrlParams = [$urlParamName => $continueUrl];
129 
130  return $this->_wishlistHelper->getAddParams($product, $continueUrlParams);
131  }

◆ getAttributes()

getAttributes ( )

Retrieve Product Compare Attributes

Returns
array

Definition at line 182 of file ListCompare.php.

183  {
184  if ($this->_attributes === null) {
185  $this->_attributes = $this->getItems()->getComparableAttributes();
186  }
187 
188  return $this->_attributes;
189  }

◆ getItems()

getItems ( )

Retrieve Product Compare items collection

Returns
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection

Definition at line 151 of file ListCompare.php.

152  {
153  if ($this->_items === null) {
154  $this->_compareProduct->setAllowUsedFlat(false);
155 
156  $this->_items = $this->_itemCollectionFactory->create();
157  $this->_items->useProductItem(true)->setStoreId($this->_storeManager->getStore()->getId());
158 
159  if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
160  $this->_items->setCustomerId($this->currentCustomer->getCustomerId());
161  } elseif ($this->_customerId) {
162  $this->_items->setCustomerId($this->_customerId);
163  } else {
164  $this->_items->setVisitorId($this->_customerVisitor->getId());
165  }
166 
167  $this->_items->addAttributeToSelect(
168  $this->_catalogConfig->getProductAttributes()
169  )->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility(
170  $this->_catalogProductVisibility->getVisibleInSiteIds()
171  );
172  }
173 
174  return $this->_items;
175  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getPrintUrl()

getPrintUrl ( )

Retrieve Print URL

Returns
string

Definition at line 238 of file ListCompare.php.

239  {
240  return $this->getUrl('*/*/*', ['_current' => true, 'print' => 1]);
241  }

◆ getProductAttributeValue()

getProductAttributeValue (   $product,
  $attribute 
)

Retrieve Product Attribute Value

Parameters
Product$product
\Magento\Catalog\Model\ResourceModel\Eav\Attribute$attribute
Returns
\Magento\Framework\Phrase|string

Definition at line 198 of file ListCompare.php.

199  {
200  if (!$product->hasData($attribute->getAttributeCode())) {
201  return __('N/A');
202  }
203 
204  if ($attribute->getSourceModel() || in_array(
205  $attribute->getFrontendInput(),
206  ['select', 'boolean', 'multiselect']
207  )
208  ) {
209  $value = $attribute->getFrontend()->getValue($product);
210  } else {
211  $value = $product->getData($attribute->getAttributeCode());
212  }
213  return (string)$value == '' ? __('No') : $value;
214  }
__()
Definition: __.php:13
$value
Definition: gender.phtml:16

◆ hasAttributeValueForProducts()

hasAttributeValueForProducts (   $attribute)

Check if any of the products has a value set for the attribute

Parameters
\Magento\Catalog\Model\ResourceModel\Eav\Attribute$attribute
Returns
bool
Since
102.0.6

Definition at line 223 of file ListCompare.php.

224  {
225  foreach ($this->getItems() as $item) {
226  if ($item->hasData($attribute->getAttributeCode())) {
227  return true;
228  }
229  }
230  return false;
231  }

◆ setCustomerId()

setCustomerId (   $id)

Setter for customer id

Parameters
int$id
Returns
\Magento\Catalog\Block\Product\Compare\ListCompare

Definition at line 249 of file ListCompare.php.

250  {
251  $this->_customerId = $id;
252  return $this;
253  }
$id
Definition: fieldset.phtml:14

Field Documentation

◆ $_attributes

$_attributes
protected

Definition at line 33 of file ListCompare.php.

◆ $_catalogProductVisibility

$_catalogProductVisibility
protected

Definition at line 66 of file ListCompare.php.

◆ $_customerId

$_customerId = null
protected

Definition at line 47 of file ListCompare.php.

◆ $_customerVisitor

$_customerVisitor
protected

Definition at line 59 of file ListCompare.php.

◆ $_itemCollectionFactory

$_itemCollectionFactory
protected

Definition at line 73 of file ListCompare.php.

◆ $_items

$_items
protected

Definition at line 26 of file ListCompare.php.

◆ $_useLinkForAsLowAs

$_useLinkForAsLowAs = false
protected

Definition at line 40 of file ListCompare.php.

◆ $currentCustomer

$currentCustomer
protected

Definition at line 83 of file ListCompare.php.

◆ $httpContext

$httpContext
protected

Definition at line 52 of file ListCompare.php.

◆ $urlEncoder

$urlEncoder
protected

Definition at line 78 of file ListCompare.php.


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