Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Compare.php
Go to the documentation of this file.
1 <?php
7 
10 
19 class Compare extends \Magento\Framework\Url\Helper\Data
20 {
26  protected $_itemCollection;
27 
33  protected $_hasItems;
34 
40  protected $_allowUsedFlat = true;
41 
47  protected $_customerId = null;
48 
54  protected $_catalogSession;
55 
61  protected $_customerSession;
62 
68  protected $_customerVisitor;
69 
76 
83 
87  protected $_formKey;
88 
92  protected $_wishlistHelper;
93 
97  protected $postHelper;
98 
102  private $_storeManager;
103 
117  public function __construct(
118  \Magento\Framework\App\Helper\Context $context,
120  \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory $itemCollectionFactory,
121  \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
122  \Magento\Customer\Model\Visitor $customerVisitor,
123  \Magento\Customer\Model\Session $customerSession,
124  \Magento\Catalog\Model\Session $catalogSession,
125  \Magento\Framework\Data\Form\FormKey $formKey,
126  \Magento\Wishlist\Helper\Data $wishlistHelper,
127  \Magento\Framework\Data\Helper\PostHelper $postHelper
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  }
140 
146  public function getListUrl()
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  }
160 
167  public function getPostDataParams($product)
168  {
169  return $this->postHelper->getPostData($this->getAddUrl(), ['product' => $product->getId()]);
170  }
171 
177  public function getAddUrl()
178  {
179  return $this->_getUrl('catalog/product_compare/add');
180  }
181 
189  {
190  $beforeCompareUrl = $this->_catalogSession->getBeforeCompareUrl();
191 
192  $encodedUrl = [
194  ];
195 
196  return $this->_wishlistHelper->getAddParams($product, $encodedUrl);
197  }
198 
205  public function getAddToCartUrl($product)
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  }
216 
222  public function getRemoveUrl()
223  {
224  return $this->_getUrl('catalog/product_compare/remove');
225  }
226 
233  public function getPostDataRemove($product)
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  }
243 
249  public function getClearListUrl()
250  {
251  return $this->_getUrl('catalog/product_compare/clear');
252  }
253 
259  public function getPostDataClearList()
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  }
268 
274  public function getItemCollection()
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  }
303 
310  public function calculate($logout = false)
311  {
313  $collection = $this->_itemCollectionFactory->create()
314  ->useProductItem(true);
315  if (!$logout && $this->_customerSession->isLoggedIn()) {
316  $collection->setCustomerId($this->_customerSession->getCustomerId());
317  } elseif ($this->_customerId) {
318  $collection->setCustomerId($this->_customerId);
319  } else {
320  $collection->setVisitorId($this->_customerVisitor->getId());
321  }
322 
323  /* Price data is added to consider item stock status using price index */
324  $collection->addPriceData()
325  ->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
326 
327  $count = $collection->getSize();
328  $this->_catalogSession->setCatalogCompareItemsCount($count);
329 
330  return $this;
331  }
332 
338  public function getItemCount()
339  {
340  if (!$this->_catalogSession->hasCatalogCompareItemsCount()) {
341  $this->calculate();
342  }
343 
344  return $this->_catalogSession->getCatalogCompareItemsCount();
345  }
346 
352  public function hasItems()
353  {
354  return $this->getItemCount() > 0;
355  }
356 
363  public function setAllowUsedFlat($flag)
364  {
365  $this->_allowUsedFlat = (bool)$flag;
366  return $this;
367  }
368 
375  public function getAllowUsedFlat()
376  {
377  return $this->_allowUsedFlat;
378  }
379 
386  public function setCustomerId($id)
387  {
388  $this->_customerId = $id;
389  return $this;
390  }
391 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$wishlistHelper
Definition: sidebar.phtml:12
$id
Definition: fieldset.phtml:14
$count
Definition: recent.phtml:13
$storeManager
__()
Definition: __.php:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
__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)
Definition: Compare.php:117