Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Compare Class Reference
Inheritance diagram for Compare:
Data AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory $itemCollectionFactory, \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility, \Magento\Customer\Model\Visitor $customerVisitor, \Magento\Customer\Model\Session $customerSession, \Magento\Catalog\Model\Session $catalogSession, \Magento\Framework\Data\Form\FormKey $formKey, \Magento\Wishlist\Helper\Data $wishlistHelper, \Magento\Framework\Data\Helper\PostHelper $postHelper)
 
 getListUrl ()
 
 getPostDataParams ($product)
 
 getAddUrl ()
 
 getAddToWishlistParams ($product)
 
 getAddToCartUrl ($product)
 
 getRemoveUrl ()
 
 getPostDataRemove ($product)
 
 getClearListUrl ()
 
 getPostDataClearList ()
 
 getItemCollection ()
 
 getItemCount ()
 
 hasItems ()
 
 setAllowUsedFlat ($flag)
 
 getAllowUsedFlat ()
 
 setCustomerId ($id)
 
- Public Member Functions inherited from Data
 getCurrentBase64Url ()
 
 getEncodedUrl ($url=null)
 
 addRequestParam ($url, $param)
 
 removeRequestParam ($url, $paramKey, $caseSensitive=false)
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Protected Attributes

 $_itemCollection
 
 $_hasItems
 
 $_allowUsedFlat = true
 
 $_customerId = null
 
 $_catalogSession
 
 $_customerSession
 
 $_customerVisitor
 
 $_catalogProductVisibility
 
 $_itemCollectionFactory
 
 $_formKey
 
 $_wishlistHelper
 
 $postHelper
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Additional Inherited Members

- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Detailed Description

Catalog Product Compare Helper

@api @SuppressWarnings(PHPMD.LongVariable) @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 19 of file Compare.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Helper\Context  $context,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory  $itemCollectionFactory,
\Magento\Catalog\Model\Product\Visibility  $catalogProductVisibility,
\Magento\Customer\Model\Visitor  $customerVisitor,
\Magento\Customer\Model\Session  $customerSession,
\Magento\Catalog\Model\Session  $catalogSession,
\Magento\Framework\Data\Form\FormKey  $formKey,
\Magento\Wishlist\Helper\Data  $wishlistHelper,
\Magento\Framework\Data\Helper\PostHelper  $postHelper 
)
Parameters
\Magento\Framework\App\Helper\Context$context
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory$itemCollectionFactory
\Magento\Catalog\Model\Product\Visibility$catalogProductVisibility
\Magento\Customer\Model\Visitor$customerVisitor
\Magento\Customer\Model\Session$customerSession
\Magento\Catalog\Model\Session$catalogSession
\Magento\Framework\Data\Form\FormKey$formKey
\Magento\Wishlist\Helper\Data$wishlistHelper
\Magento\Framework\Data\Helper\PostHelper$postHelper@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 117 of file Compare.php.

128  {
129  $this->_itemCollectionFactory = $itemCollectionFactory;
130  $this->_catalogProductVisibility = $catalogProductVisibility;
131  $this->_customerVisitor = $customerVisitor;
132  $this->_customerSession = $customerSession;
133  $this->_catalogSession = $catalogSession;
134  $this->_formKey = $formKey;
135  $this->_wishlistHelper = $wishlistHelper;
136  $this->postHelper = $postHelper;
137  $this->_storeManager = $storeManager;
138  parent::__construct($context);
139  }
$wishlistHelper
Definition: sidebar.phtml:12
$storeManager

Member Function Documentation

◆ getAddToCartUrl()

getAddToCartUrl (   $product)

Retrieve add to cart url

Parameters
Product$product
Returns
string

Definition at line 205 of file Compare.php.

206  {
207  $beforeCompareUrl = $this->_catalogSession->getBeforeCompareUrl();
208  $params = [
209  'product' => $product->getId(),
211  '_secure' => $this->_getRequest()->isSecure()
212  ];
213 
214  return $this->_getUrl('checkout/cart/add', $params);
215  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getAddToWishlistParams()

getAddToWishlistParams (   $product)

Retrieve add to wishlist params

Parameters
Product$product
Returns
string

Definition at line 188 of file Compare.php.

189  {
190  $beforeCompareUrl = $this->_catalogSession->getBeforeCompareUrl();
191 
192  $encodedUrl = [
194  ];
195 
196  return $this->_wishlistHelper->getAddParams($product, $encodedUrl);
197  }

◆ getAddUrl()

getAddUrl ( )

Retrieve url for adding product to compare list

Returns
string

Definition at line 177 of file Compare.php.

178  {
179  return $this->_getUrl('catalog/product_compare/add');
180  }

◆ getAllowUsedFlat()

getAllowUsedFlat ( )

Retrieve is allow used flat (for collection)

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 375 of file Compare.php.

◆ getClearListUrl()

getClearListUrl ( )

Retrieve clear compare list url

Returns
string

Definition at line 249 of file Compare.php.

250  {
251  return $this->_getUrl('catalog/product_compare/clear');
252  }

◆ getItemCollection()

getItemCollection ( )

Retrieve compare list items collection

Returns
Collection

Definition at line 274 of file Compare.php.

275  {
276  if (!$this->_itemCollection) {
277  // cannot be placed in constructor because of the cyclic dependency which cannot be fixed with proxy class
278  // collection uses this helper in constructor when calling isEnabledFlat() method
279  $this->_itemCollection = $this->_itemCollectionFactory->create();
280  $this->_itemCollection->useProductItem(true)->setStoreId($this->_storeManager->getStore()->getId());
281 
282  if ($this->_customerSession->isLoggedIn()) {
283  $this->_itemCollection->setCustomerId($this->_customerSession->getCustomerId());
284  } elseif ($this->_customerId) {
285  $this->_itemCollection->setCustomerId($this->_customerId);
286  } else {
287  $this->_itemCollection->setVisitorId($this->_customerVisitor->getId());
288  }
289 
290  $this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
291 
292  /* Price data is added to consider item stock status using price index */
293  $this->_itemCollection->addPriceData();
294 
295  $this->_itemCollection->addAttributeToSelect('name')->addUrlRewrite()->load();
296 
297  /* update compare items count */
298  $this->_catalogSession->setCatalogCompareItemsCount(count($this->_itemCollection));
299  }
300 
301  return $this->_itemCollection;
302  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getItemCount()

getItemCount ( )

Retrieve count of items in compare list

Returns
int

Definition at line 338 of file Compare.php.

339  {
340  if (!$this->_catalogSession->hasCatalogCompareItemsCount()) {
341  $this->calculate();
342  }
343 
344  return $this->_catalogSession->getCatalogCompareItemsCount();
345  }

◆ getListUrl()

getListUrl ( )

Retrieve compare list url

Returns
string

Definition at line 146 of file Compare.php.

147  {
148  $itemIds = [];
149  foreach ($this->getItemCollection() as $item) {
150  $itemIds[] = $item->getId();
151  }
152 
153  $params = [
154  'items' => implode(',', $itemIds),
156  ];
157 
158  return $this->_getUrl('catalog/product_compare', $params);
159  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getPostDataClearList()

getPostDataClearList ( )

Get parameters to clear compare list

Returns
string

Definition at line 259 of file Compare.php.

260  {
261  $params = [
263  'confirmation' => true,
264  'confirmationMessage' => __('Are you sure you want to remove all items from your Compare Products list?'),
265  ];
266  return $this->postHelper->getPostData($this->getClearListUrl(), $params);
267  }
__()
Definition: __.php:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getPostDataParams()

getPostDataParams (   $product)

Get parameters used for build add product to compare list urls

Parameters
Product$product
Returns
string

Definition at line 167 of file Compare.php.

168  {
169  return $this->postHelper->getPostData($this->getAddUrl(), ['product' => $product->getId()]);
170  }

◆ getPostDataRemove()

getPostDataRemove (   $product)

Get parameters to remove products from compare list

Parameters
Product$product
Returns
string

Definition at line 233 of file Compare.php.

234  {
235  $data = [
237  'product' => $product->getId(),
238  'confirmation' => true,
239  'confirmationMessage' => __('Are you sure you want to remove this item from your Compare Products list?')
240  ];
241  return $this->postHelper->getPostData($this->getRemoveUrl(), $data);
242  }
__()
Definition: __.php:13

◆ getRemoveUrl()

getRemoveUrl ( )

Retrieve remove item from compare list url

Returns
string

Definition at line 222 of file Compare.php.

223  {
224  return $this->_getUrl('catalog/product_compare/remove');
225  }

◆ hasItems()

hasItems ( )

Check has items

Returns
bool

Definition at line 352 of file Compare.php.

353  {
354  return $this->getItemCount() > 0;
355  }

◆ setAllowUsedFlat()

setAllowUsedFlat (   $flag)

Set is allow used flat (for collection)

Parameters
bool$flag
Returns
$this

Definition at line 363 of file Compare.php.

364  {
365  $this->_allowUsedFlat = (bool)$flag;
366  return $this;
367  }

◆ setCustomerId()

setCustomerId (   $id)

Setter for customer id

Parameters
int$id
Returns
$this

Definition at line 386 of file Compare.php.

387  {
388  $this->_customerId = $id;
389  return $this;
390  }
$id
Definition: fieldset.phtml:14

Field Documentation

◆ $_allowUsedFlat

$_allowUsedFlat = true
protected

Definition at line 40 of file Compare.php.

◆ $_catalogProductVisibility

$_catalogProductVisibility
protected

Definition at line 75 of file Compare.php.

◆ $_catalogSession

$_catalogSession
protected

Definition at line 54 of file Compare.php.

◆ $_customerId

$_customerId = null
protected

Definition at line 47 of file Compare.php.

◆ $_customerSession

$_customerSession
protected

Definition at line 61 of file Compare.php.

◆ $_customerVisitor

$_customerVisitor
protected

Definition at line 68 of file Compare.php.

◆ $_formKey

$_formKey
protected

Definition at line 87 of file Compare.php.

◆ $_hasItems

$_hasItems
protected

Definition at line 33 of file Compare.php.

◆ $_itemCollection

$_itemCollection
protected

Definition at line 26 of file Compare.php.

◆ $_itemCollectionFactory

$_itemCollectionFactory
protected

Definition at line 82 of file Compare.php.

◆ $_wishlistHelper

$_wishlistHelper
protected

Definition at line 92 of file Compare.php.

◆ $postHelper

$postHelper
protected

Definition at line 97 of file Compare.php.


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