Data.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Helper;
  7. use Magento\Framework\App\ActionInterface;
  8. use Magento\Wishlist\Controller\WishlistProviderInterface;
  9. /**
  10. * Wishlist Data Helper
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  19. {
  20. /**
  21. * Config key 'Display Wishlist Summary'
  22. */
  23. const XML_PATH_WISHLIST_LINK_USE_QTY = 'wishlist/wishlist_link/use_qty';
  24. /**
  25. * Config key 'Display Out of Stock Products'
  26. */
  27. const XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock';
  28. /**
  29. * Currently logged in customer
  30. *
  31. * @var \Magento\Customer\Api\Data\CustomerInterface
  32. */
  33. protected $_currentCustomer;
  34. /**
  35. * Customer Wishlist instance
  36. *
  37. * @var \Magento\Wishlist\Model\Wishlist
  38. */
  39. protected $_wishlist;
  40. /**
  41. * Wishlist Product Items Collection
  42. *
  43. * @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
  44. */
  45. protected $_productCollection;
  46. /**
  47. * Wishlist Items Collection
  48. *
  49. * @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
  50. */
  51. protected $_wishlistItemCollection;
  52. /**
  53. * Core registry
  54. *
  55. * @var \Magento\Framework\Registry
  56. */
  57. protected $_coreRegistry;
  58. /**
  59. * @var \Magento\Customer\Model\Session
  60. */
  61. protected $_customerSession;
  62. /**
  63. * @var \Magento\Wishlist\Model\WishlistFactory
  64. */
  65. protected $_wishlistFactory;
  66. /**
  67. * @var \Magento\Store\Model\StoreManagerInterface
  68. */
  69. protected $_storeManager;
  70. /**
  71. * @var \Magento\Framework\Data\Helper\PostHelper
  72. */
  73. protected $_postDataHelper;
  74. /**
  75. * @var \Magento\Customer\Helper\View
  76. */
  77. protected $_customerViewHelper;
  78. /**
  79. * @var \Magento\Wishlist\Controller\WishlistProviderInterface
  80. */
  81. protected $wishlistProvider;
  82. /**
  83. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  84. */
  85. protected $productRepository;
  86. /**
  87. * @param \Magento\Framework\App\Helper\Context $context
  88. * @param \Magento\Framework\Registry $coreRegistry
  89. * @param \Magento\Customer\Model\Session $customerSession
  90. * @param \Magento\Wishlist\Model\WishlistFactory $wishlistFactory
  91. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  92. * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
  93. * @param \Magento\Customer\Helper\View $customerViewHelper
  94. * @param WishlistProviderInterface $wishlistProvider
  95. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  96. */
  97. public function __construct(
  98. \Magento\Framework\App\Helper\Context $context,
  99. \Magento\Framework\Registry $coreRegistry,
  100. \Magento\Customer\Model\Session $customerSession,
  101. \Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
  102. \Magento\Store\Model\StoreManagerInterface $storeManager,
  103. \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
  104. \Magento\Customer\Helper\View $customerViewHelper,
  105. WishlistProviderInterface $wishlistProvider,
  106. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  107. ) {
  108. $this->_coreRegistry = $coreRegistry;
  109. $this->_customerSession = $customerSession;
  110. $this->_wishlistFactory = $wishlistFactory;
  111. $this->_storeManager = $storeManager;
  112. $this->_postDataHelper = $postDataHelper;
  113. $this->_customerViewHelper = $customerViewHelper;
  114. $this->wishlistProvider = $wishlistProvider;
  115. $this->productRepository = $productRepository;
  116. parent::__construct($context);
  117. }
  118. /**
  119. * Retrieve customer login status
  120. *
  121. * @return bool
  122. */
  123. protected function _isCustomerLogIn()
  124. {
  125. return $this->_customerSession->isLoggedIn();
  126. }
  127. /**
  128. * Retrieve logged in customer
  129. *
  130. * @return \Magento\Customer\Api\Data\CustomerInterface
  131. */
  132. protected function _getCurrentCustomer()
  133. {
  134. return $this->getCustomer();
  135. }
  136. /**
  137. * Set current customer
  138. *
  139. * @param \Magento\Customer\Api\Data\CustomerInterface $customer
  140. * @return void
  141. */
  142. public function setCustomer(\Magento\Customer\Api\Data\CustomerInterface $customer)
  143. {
  144. $this->_currentCustomer = $customer;
  145. }
  146. /**
  147. * Retrieve current customer
  148. *
  149. * @return \Magento\Customer\Api\Data\CustomerInterface|null
  150. */
  151. public function getCustomer()
  152. {
  153. if (!$this->_currentCustomer && $this->_customerSession->isLoggedIn()) {
  154. $this->_currentCustomer = $this->_customerSession->getCustomerDataObject();
  155. }
  156. return $this->_currentCustomer;
  157. }
  158. /**
  159. * Retrieve wishlist by logged in customer
  160. *
  161. * @return \Magento\Wishlist\Model\Wishlist
  162. */
  163. public function getWishlist()
  164. {
  165. if ($this->_wishlist === null) {
  166. if ($this->_coreRegistry->registry('shared_wishlist')) {
  167. $this->_wishlist = $this->_coreRegistry->registry('shared_wishlist');
  168. } else {
  169. $this->_wishlist = $this->wishlistProvider->getWishlist();
  170. }
  171. }
  172. return $this->_wishlist;
  173. }
  174. /**
  175. * Retrieve wishlist item count (include config settings)
  176. *
  177. * Used in top link menu only
  178. *
  179. * @return int
  180. */
  181. public function getItemCount()
  182. {
  183. $storedDisplayType = $this->_customerSession->getWishlistDisplayType();
  184. $currentDisplayType = $this->scopeConfig->getValue(
  185. self::XML_PATH_WISHLIST_LINK_USE_QTY,
  186. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  187. );
  188. $storedDisplayOutOfStockProducts = $this->_customerSession->getDisplayOutOfStockProducts();
  189. $currentDisplayOutOfStockProducts = $this->scopeConfig->getValue(
  190. self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK,
  191. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  192. );
  193. if (!$this->_customerSession->hasWishlistItemCount() ||
  194. $currentDisplayType != $storedDisplayType ||
  195. $this->_customerSession->hasDisplayOutOfStockProducts() ||
  196. $currentDisplayOutOfStockProducts != $storedDisplayOutOfStockProducts
  197. ) {
  198. $this->calculate();
  199. }
  200. return $this->_customerSession->getWishlistItemCount();
  201. }
  202. /**
  203. * Create wishlist item collection
  204. *
  205. * @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
  206. */
  207. protected function _createWishlistItemCollection()
  208. {
  209. return $this->getWishlist()->getItemCollection();
  210. }
  211. /**
  212. * Retrieve wishlist items collection
  213. *
  214. * @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
  215. */
  216. public function getWishlistItemCollection()
  217. {
  218. if ($this->_wishlistItemCollection === null) {
  219. $this->_wishlistItemCollection = $this->_createWishlistItemCollection();
  220. }
  221. return $this->_wishlistItemCollection;
  222. }
  223. /**
  224. * Retrieve Item Store for URL
  225. *
  226. * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  227. * @return \Magento\Store\Model\Store
  228. */
  229. protected function _getUrlStore($item)
  230. {
  231. $storeId = null;
  232. $product = null;
  233. if ($item instanceof \Magento\Wishlist\Model\Item) {
  234. $product = $item->getProduct();
  235. } elseif ($item instanceof \Magento\Catalog\Model\Product) {
  236. $product = $item;
  237. }
  238. if ($product) {
  239. if ($product->isVisibleInSiteVisibility()) {
  240. $storeId = $product->getStoreId();
  241. } else {
  242. if ($product->hasUrlDataObject()) {
  243. $storeId = $product->getUrlDataObject()->getStoreId();
  244. }
  245. }
  246. }
  247. return $this->_storeManager->getStore($storeId);
  248. }
  249. /**
  250. * Retrieve params for removing item from wishlist
  251. *
  252. * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  253. * @param bool $addReferer
  254. * @return string
  255. */
  256. public function getRemoveParams($item, $addReferer = false)
  257. {
  258. $url = $this->_getUrl('wishlist/index/remove');
  259. $params = ['item' => $item->getWishlistItemId()];
  260. $params[ActionInterface::PARAM_NAME_URL_ENCODED] = '';
  261. if ($addReferer) {
  262. $params = $this->addRefererToParams($params);
  263. }
  264. return $this->_postDataHelper->getPostData($url, $params);
  265. }
  266. /**
  267. * Retrieve URL for configuring item from wishlist
  268. *
  269. * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  270. * @return string
  271. */
  272. public function getConfigureUrl($item)
  273. {
  274. return $this->_getUrl(
  275. 'wishlist/index/configure',
  276. [
  277. 'id' => $item->getWishlistItemId(),
  278. 'product_id' => $item->getProductId()
  279. ]
  280. );
  281. }
  282. /**
  283. * Retrieve params for adding product to wishlist
  284. *
  285. * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  286. * @param array $params
  287. * @return string
  288. */
  289. public function getAddParams($item, array $params = [])
  290. {
  291. $productId = null;
  292. if ($item instanceof \Magento\Catalog\Model\Product) {
  293. $productId = $item->getEntityId();
  294. }
  295. if ($item instanceof \Magento\Wishlist\Model\Item) {
  296. $productId = $item->getProductId();
  297. }
  298. $url = $this->_getUrlStore($item)->getUrl('wishlist/index/add');
  299. if ($productId) {
  300. $params['product'] = $productId;
  301. }
  302. return $this->_postDataHelper->getPostData($url, $params);
  303. }
  304. /**
  305. * Retrieve params for adding product to wishlist
  306. *
  307. * @param int $itemId
  308. *
  309. * @return string
  310. */
  311. public function getMoveFromCartParams($itemId)
  312. {
  313. $url = $this->_getUrl('wishlist/index/fromcart');
  314. $params = ['item' => $itemId];
  315. return $this->_postDataHelper->getPostData($url, $params);
  316. }
  317. /**
  318. * Retrieve params for updating product in wishlist
  319. *
  320. * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  321. *
  322. * @return string|false
  323. */
  324. public function getUpdateParams($item)
  325. {
  326. $itemId = null;
  327. if ($item instanceof \Magento\Catalog\Model\Product) {
  328. $itemId = $item->getWishlistItemId();
  329. $productId = $item->getId();
  330. }
  331. if ($item instanceof \Magento\Wishlist\Model\Item) {
  332. $itemId = $item->getId();
  333. $productId = $item->getProduct()->getId();
  334. }
  335. $url = $this->_getUrl('wishlist/index/updateItemOptions');
  336. if ($itemId) {
  337. $params = ['id' => $itemId, 'product' => $productId, 'qty' => $item->getQty()];
  338. return $this->_postDataHelper->getPostData($url, $params);
  339. }
  340. return false;
  341. }
  342. /**
  343. * Retrieve params for adding item to shopping cart
  344. *
  345. * @param string|\Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  346. * @return string
  347. */
  348. public function getAddToCartUrl($item)
  349. {
  350. return $this->_getUrlStore($item)->getUrl('wishlist/index/cart', $this->_getCartUrlParameters($item));
  351. }
  352. /**
  353. * Retrieve URL for adding item to shopping cart
  354. *
  355. * @param string|\Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  356. * @param bool $addReferer
  357. * @return string
  358. */
  359. public function getAddToCartParams($item, $addReferer = false)
  360. {
  361. $params = $this->_getCartUrlParameters($item);
  362. $params[ActionInterface::PARAM_NAME_URL_ENCODED] = '';
  363. if ($addReferer) {
  364. $params = $this->addRefererToParams($params);
  365. }
  366. return $this->_postDataHelper->getPostData(
  367. $this->_getUrlStore($item)->getUrl('wishlist/index/cart'),
  368. $params
  369. );
  370. }
  371. /**
  372. * Add UENC referer to params
  373. *
  374. * @param array $params
  375. * @return array
  376. */
  377. public function addRefererToParams(array $params)
  378. {
  379. $params[ActionInterface::PARAM_NAME_URL_ENCODED] =
  380. $this->urlEncoder->encode($this->_getRequest()->getServer('HTTP_REFERER'));
  381. return $params;
  382. }
  383. /**
  384. * Retrieve URL for adding item to shopping cart from shared wishlist
  385. *
  386. * @param string|\Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  387. * @return string
  388. */
  389. public function getSharedAddToCartUrl($item)
  390. {
  391. return $this->_postDataHelper->getPostData(
  392. $this->_getUrlStore($item)->getUrl('wishlist/shared/cart'),
  393. $this->_getCartUrlParameters($item)
  394. );
  395. }
  396. /**
  397. * Retrieve URL for adding All items to shopping cart from shared wishlist
  398. *
  399. * @return string
  400. */
  401. public function getSharedAddAllToCartUrl()
  402. {
  403. return $this->_postDataHelper->getPostData(
  404. $this->_storeManager->getStore()->getUrl('*/*/allcart', ['_current' => true])
  405. );
  406. }
  407. /**
  408. * Get cart URL parameters
  409. *
  410. * @param string|\Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  411. * @return array
  412. */
  413. protected function _getCartUrlParameters($item)
  414. {
  415. $params = [
  416. 'item' => is_string($item) ? $item : $item->getWishlistItemId(),
  417. ];
  418. if ($item instanceof \Magento\Wishlist\Model\Item) {
  419. $params['qty'] = $item->getQty();
  420. }
  421. return $params;
  422. }
  423. /**
  424. * Retrieve customer wishlist url
  425. *
  426. * @param int $wishlistId
  427. * @return string
  428. */
  429. public function getListUrl($wishlistId = null)
  430. {
  431. $params = [];
  432. if ($wishlistId) {
  433. $params['wishlist_id'] = $wishlistId;
  434. }
  435. return $this->_getUrl('wishlist', $params);
  436. }
  437. /**
  438. * Check is allow wishlist module
  439. *
  440. * @return bool
  441. */
  442. public function isAllow()
  443. {
  444. if ($this->_moduleManager->isOutputEnabled($this->_getModuleName()) && $this->scopeConfig->getValue(
  445. 'wishlist/general/active',
  446. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  447. )
  448. ) {
  449. return true;
  450. }
  451. return false;
  452. }
  453. /**
  454. * Check is allow wishlist action in shopping cart
  455. *
  456. * @return bool
  457. */
  458. public function isAllowInCart()
  459. {
  460. return $this->isAllow() && $this->getCustomer();
  461. }
  462. /**
  463. * Retrieve customer name
  464. *
  465. * @return string|null
  466. */
  467. public function getCustomerName()
  468. {
  469. return $this->getCustomer()
  470. ? $this->_customerViewHelper->getCustomerName($this->getCustomer())
  471. : null;
  472. }
  473. /**
  474. * Retrieve RSS URL
  475. *
  476. * @param int|string|null $wishlistId
  477. * @return string
  478. */
  479. public function getRssUrl($wishlistId = null)
  480. {
  481. $customer = $this->_getCurrentCustomer();
  482. if ($customer) {
  483. $key = $customer->getId() . ',' . $customer->getEmail();
  484. $params = ['data' => $this->urlEncoder->encode($key), '_secure' => false];
  485. }
  486. if ($wishlistId) {
  487. $params['wishlist_id'] = $wishlistId;
  488. }
  489. return $this->_getUrl('wishlist/index/rss', $params);
  490. }
  491. /**
  492. * Retrieve default empty comment message
  493. *
  494. * @return \Magento\Framework\Phrase
  495. */
  496. public function defaultCommentString()
  497. {
  498. return __('Comment');
  499. }
  500. /**
  501. * Retrieve default empty comment message
  502. *
  503. * @return \Magento\Framework\Phrase
  504. */
  505. public function getDefaultWishlistName()
  506. {
  507. return __('Wish List');
  508. }
  509. /**
  510. * Calculate count of wishlist items and put value to customer session.
  511. * Method called after wishlist modifications and trigger 'wishlist_items_renewed' event.
  512. * Depends from configuration.
  513. *
  514. * @return $this
  515. */
  516. public function calculate()
  517. {
  518. $count = 0;
  519. if ($this->getCustomer()) {
  520. $collection = $this->getWishlistItemCollection()->setInStockFilter(true);
  521. if ($this->scopeConfig->getValue(
  522. self::XML_PATH_WISHLIST_LINK_USE_QTY,
  523. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  524. )
  525. ) {
  526. $count = $collection->getItemsQty();
  527. } else {
  528. $count = $collection->count();
  529. }
  530. $this->_customerSession->setWishlistDisplayType(
  531. $this->scopeConfig->getValue(
  532. self::XML_PATH_WISHLIST_LINK_USE_QTY,
  533. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  534. )
  535. );
  536. $this->_customerSession->setDisplayOutOfStockProducts(
  537. $this->scopeConfig->getValue(
  538. self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK,
  539. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  540. )
  541. );
  542. }
  543. $this->_customerSession->setWishlistItemCount($count);
  544. $this->_eventManager->dispatch('wishlist_items_renewed');
  545. return $this;
  546. }
  547. /**
  548. * Should display item quantities in my wishlist link
  549. *
  550. * @return bool
  551. */
  552. public function isDisplayQty()
  553. {
  554. return $this->scopeConfig->getValue(
  555. self::XML_PATH_WISHLIST_LINK_USE_QTY,
  556. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  557. );
  558. }
  559. /**
  560. * Retrieve URL to item Product
  561. *
  562. * @param \Magento\Wishlist\Model\Item|\Magento\Catalog\Model\Product $item
  563. * @param array $additional
  564. * @return string
  565. */
  566. public function getProductUrl($item, $additional = [])
  567. {
  568. if ($item instanceof \Magento\Catalog\Model\Product) {
  569. $product = $item;
  570. } else {
  571. $product = $item->getProduct();
  572. }
  573. $buyRequest = $item->getBuyRequest();
  574. if (is_object($buyRequest)) {
  575. $config = $buyRequest->getSuperProductConfig();
  576. if ($config && !empty($config['product_id'])) {
  577. $product = $this->productRepository->getById(
  578. $config['product_id'],
  579. false,
  580. $this->_storeManager->getStore()->getStoreId()
  581. );
  582. }
  583. }
  584. return $product->getUrlModel()->getUrl($product, $additional);
  585. }
  586. }