Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
7 
10 
21 {
25  const XML_PATH_WISHLIST_LINK_USE_QTY = 'wishlist/wishlist_link/use_qty';
26 
30  const XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock';
31 
37  protected $_currentCustomer;
38 
44  protected $_wishlist;
45 
52 
59 
65  protected $_coreRegistry;
66 
70  protected $_customerSession;
71 
75  protected $_wishlistFactory;
76 
80  protected $_storeManager;
81 
85  protected $_postDataHelper;
86 
91 
95  protected $wishlistProvider;
96 
101 
113  public function __construct(
114  \Magento\Framework\App\Helper\Context $context,
115  \Magento\Framework\Registry $coreRegistry,
116  \Magento\Customer\Model\Session $customerSession,
117  \Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
118  \Magento\Store\Model\StoreManagerInterface $storeManager,
119  \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
120  \Magento\Customer\Helper\View $customerViewHelper,
122  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
123  ) {
124  $this->_coreRegistry = $coreRegistry;
125  $this->_customerSession = $customerSession;
126  $this->_wishlistFactory = $wishlistFactory;
127  $this->_storeManager = $storeManager;
128  $this->_postDataHelper = $postDataHelper;
129  $this->_customerViewHelper = $customerViewHelper;
130  $this->wishlistProvider = $wishlistProvider;
131  $this->productRepository = $productRepository;
132  parent::__construct($context);
133  }
134 
140  protected function _isCustomerLogIn()
141  {
142  return $this->_customerSession->isLoggedIn();
143  }
144 
150  protected function _getCurrentCustomer()
151  {
152  return $this->getCustomer();
153  }
154 
161  public function setCustomer(\Magento\Customer\Api\Data\CustomerInterface $customer)
162  {
163  $this->_currentCustomer = $customer;
164  }
165 
171  public function getCustomer()
172  {
173  if (!$this->_currentCustomer && $this->_customerSession->isLoggedIn()) {
174  $this->_currentCustomer = $this->_customerSession->getCustomerDataObject();
175  }
177  }
178 
184  public function getWishlist()
185  {
186  if ($this->_wishlist === null) {
187  if ($this->_coreRegistry->registry('shared_wishlist')) {
188  $this->_wishlist = $this->_coreRegistry->registry('shared_wishlist');
189  } else {
190  $this->_wishlist = $this->wishlistProvider->getWishlist();
191  }
192  }
193  return $this->_wishlist;
194  }
195 
203  public function getItemCount()
204  {
205  $storedDisplayType = $this->_customerSession->getWishlistDisplayType();
206  $currentDisplayType = $this->scopeConfig->getValue(
207  self::XML_PATH_WISHLIST_LINK_USE_QTY,
208  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
209  );
210 
211  $storedDisplayOutOfStockProducts = $this->_customerSession->getDisplayOutOfStockProducts();
212  $currentDisplayOutOfStockProducts = $this->scopeConfig->getValue(
213  self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK,
214  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
215  );
216  if (!$this->_customerSession->hasWishlistItemCount() ||
217  $currentDisplayType != $storedDisplayType ||
218  $this->_customerSession->hasDisplayOutOfStockProducts() ||
219  $currentDisplayOutOfStockProducts != $storedDisplayOutOfStockProducts
220  ) {
221  $this->calculate();
222  }
223 
224  return $this->_customerSession->getWishlistItemCount();
225  }
226 
232  protected function _createWishlistItemCollection()
233  {
234  return $this->getWishlist()->getItemCollection();
235  }
236 
242  public function getWishlistItemCollection()
243  {
244  if ($this->_wishlistItemCollection === null) {
245  $this->_wishlistItemCollection = $this->_createWishlistItemCollection();
246  }
248  }
249 
256  protected function _getUrlStore($item)
257  {
258  $storeId = null;
259  $product = null;
260  if ($item instanceof \Magento\Wishlist\Model\Item) {
261  $product = $item->getProduct();
262  } elseif ($item instanceof \Magento\Catalog\Model\Product) {
263  $product = $item;
264  }
265  if ($product) {
266  if ($product->isVisibleInSiteVisibility()) {
267  $storeId = $product->getStoreId();
268  } else {
269  if ($product->hasUrlDataObject()) {
270  $storeId = $product->getUrlDataObject()->getStoreId();
271  }
272  }
273  }
274  return $this->_storeManager->getStore($storeId);
275  }
276 
284  public function getRemoveParams($item, $addReferer = false)
285  {
286  $url = $this->_getUrl('wishlist/index/remove');
287  $params = ['item' => $item->getWishlistItemId()];
289 
290  if ($addReferer) {
292  }
293 
294  return $this->_postDataHelper->getPostData($url, $params);
295  }
296 
303  public function getConfigureUrl($item)
304  {
305  return $this->_getUrl(
306  'wishlist/index/configure',
307  [
308  'id' => $item->getWishlistItemId(),
309  'product_id' => $item->getProductId()
310  ]
311  );
312  }
313 
321  public function getAddParams($item, array $params = [])
322  {
323  $productId = null;
324  if ($item instanceof \Magento\Catalog\Model\Product) {
325  $productId = $item->getEntityId();
326  }
327  if ($item instanceof \Magento\Wishlist\Model\Item) {
328  $productId = $item->getProductId();
329  }
330 
331  $url = $this->_getUrlStore($item)->getUrl('wishlist/index/add');
332  if ($productId) {
333  $params['product'] = $productId;
334  }
335 
336  return $this->_postDataHelper->getPostData($url, $params);
337  }
338 
346  public function getMoveFromCartParams($itemId)
347  {
348  $url = $this->_getUrl('wishlist/index/fromcart');
349  $params = ['item' => $itemId];
350  return $this->_postDataHelper->getPostData($url, $params);
351  }
352 
360  public function getUpdateParams($item)
361  {
362  $itemId = null;
363  if ($item instanceof \Magento\Catalog\Model\Product) {
364  $itemId = $item->getWishlistItemId();
365  $productId = $item->getId();
366  }
367  if ($item instanceof \Magento\Wishlist\Model\Item) {
368  $itemId = $item->getId();
369  $productId = $item->getProduct()->getId();
370  }
371 
372  $url = $this->_getUrl('wishlist/index/updateItemOptions');
373  if ($itemId) {
374  $params = ['id' => $itemId, 'product' => $productId, 'qty' => $item->getQty()];
375  return $this->_postDataHelper->getPostData($url, $params);
376  }
377 
378  return false;
379  }
380 
387  public function getAddToCartUrl($item)
388  {
389  return $this->_getUrlStore($item)->getUrl('wishlist/index/cart', $this->_getCartUrlParameters($item));
390  }
391 
399  public function getAddToCartParams($item, $addReferer = false)
400  {
403 
404  if ($addReferer) {
406  }
407 
408  return $this->_postDataHelper->getPostData(
409  $this->_getUrlStore($item)->getUrl('wishlist/index/cart'),
410  $params
411  );
412  }
413 
420  public function addRefererToParams(array $params)
421  {
423  $this->urlEncoder->encode($this->_getRequest()->getServer('HTTP_REFERER'));
424  return $params;
425  }
426 
433  public function getSharedAddToCartUrl($item)
434  {
435  return $this->_postDataHelper->getPostData(
436  $this->_getUrlStore($item)->getUrl('wishlist/shared/cart'),
438  );
439  }
440 
446  public function getSharedAddAllToCartUrl()
447  {
448  return $this->_postDataHelper->getPostData(
449  $this->_storeManager->getStore()->getUrl('*/*/allcart', ['_current' => true])
450  );
451  }
452 
459  protected function _getCartUrlParameters($item)
460  {
461  $params = [
462  'item' => is_string($item) ? $item : $item->getWishlistItemId(),
463  ];
464  if ($item instanceof \Magento\Wishlist\Model\Item) {
465  $params['qty'] = $item->getQty();
466  }
467  return $params;
468  }
469 
476  public function getListUrl($wishlistId = null)
477  {
478  $params = [];
479  if ($wishlistId) {
480  $params['wishlist_id'] = $wishlistId;
481  }
482  return $this->_getUrl('wishlist', $params);
483  }
484 
490  public function isAllow()
491  {
492  if ($this->_moduleManager->isOutputEnabled($this->_getModuleName()) && $this->scopeConfig->getValue(
493  'wishlist/general/active',
494  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
495  )
496  ) {
497  return true;
498  }
499  return false;
500  }
501 
507  public function isAllowInCart()
508  {
509  return $this->isAllow() && $this->getCustomer();
510  }
511 
517  public function getCustomerName()
518  {
519  return $this->getCustomer()
520  ? $this->_customerViewHelper->getCustomerName($this->getCustomer())
521  : null;
522  }
523 
530  public function getRssUrl($wishlistId = null)
531  {
532  $customer = $this->_getCurrentCustomer();
533  if ($customer) {
534  $key = $customer->getId() . ',' . $customer->getEmail();
535  $params = ['data' => $this->urlEncoder->encode($key), '_secure' => false];
536  }
537  if ($wishlistId) {
538  $params['wishlist_id'] = $wishlistId;
539  }
540  return $this->_getUrl('wishlist/index/rss', $params);
541  }
542 
548  public function defaultCommentString()
549  {
550  return __('Comment');
551  }
552 
558  public function getDefaultWishlistName()
559  {
560  return __('Wish List');
561  }
562 
570  public function calculate()
571  {
572  $count = 0;
573  if ($this->getCustomer()) {
574  $collection = $this->getWishlistItemCollection()->setInStockFilter(true);
575  if ($this->scopeConfig->getValue(
576  self::XML_PATH_WISHLIST_LINK_USE_QTY,
577  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
578  )
579  ) {
580  $count = $collection->getItemsQty();
581  } else {
582  $count = $collection->count();
583  }
584  $this->_customerSession->setWishlistDisplayType(
585  $this->scopeConfig->getValue(
586  self::XML_PATH_WISHLIST_LINK_USE_QTY,
587  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
588  )
589  );
590  $this->_customerSession->setDisplayOutOfStockProducts(
591  $this->scopeConfig->getValue(
592  self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK,
593  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
594  )
595  );
596  }
597  $this->_customerSession->setWishlistItemCount($count);
598  $this->_eventManager->dispatch('wishlist_items_renewed');
599  return $this;
600  }
601 
607  public function isDisplayQty()
608  {
609  return $this->scopeConfig->getValue(
610  self::XML_PATH_WISHLIST_LINK_USE_QTY,
611  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
612  );
613  }
614 
622  public function getProductUrl($item, $additional = [])
623  {
624  if ($item instanceof \Magento\Catalog\Model\Product) {
625  $product = $item;
626  } else {
627  $product = $item->getProduct();
628  }
629  $buyRequest = $item->getBuyRequest();
630  if (is_object($buyRequest)) {
631  $config = $buyRequest->getSuperProductConfig();
632  if ($config && !empty($config['product_id'])) {
633  $product = $this->productRepository->getById(
634  $config['product_id'],
635  false,
636  $this->_storeManager->getStore()->getStoreId()
637  );
638  }
639  }
640  return $product->getUrlModel()->getUrl($product, $additional);
641  }
642 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$customer
Definition: customers.php:11
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Customer\Model\Session $customerSession, \Magento\Wishlist\Model\WishlistFactory $wishlistFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, \Magento\Customer\Helper\View $customerViewHelper, WishlistProviderInterface $wishlistProvider, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository)
Definition: Data.php:113
$config
Definition: fraud_order.php:17
$count
Definition: recent.phtml:13
$storeManager
setCustomer(\Magento\Customer\Api\Data\CustomerInterface $customer)
Definition: Data.php:161
__()
Definition: __.php:13
getAddToCartParams($item, $addReferer=false)
Definition: Data.php:399
const XML_PATH_WISHLIST_LINK_USE_QTY
Definition: Data.php:25
const XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK
Definition: Data.php:30
getListUrl($wishlistId=null)
Definition: Data.php:476
getMoveFromCartParams($itemId)
Definition: Data.php:346
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
getProductUrl($item, $additional=[])
Definition: Data.php:622
getAddParams($item, array $params=[])
Definition: Data.php:321
getRemoveParams($item, $addReferer=false)
Definition: Data.php:284
addRefererToParams(array $params)
Definition: Data.php:420
getRssUrl($wishlistId=null)
Definition: Data.php:530
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18