Wishlist.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * @author Magento Core Team <core@magentocommerce.com>
  8. */
  9. namespace Magento\Wishlist\Block\Customer;
  10. /**
  11. * Wishlist block customer items.
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Wishlist extends \Magento\Wishlist\Block\AbstractBlock
  17. {
  18. /**
  19. * List of product options rendering configurations by product type
  20. *
  21. * @var array
  22. */
  23. protected $_optionsCfg = [];
  24. /**
  25. * @var \Magento\Catalog\Helper\Product\ConfigurationPool
  26. */
  27. protected $_helperPool;
  28. /**
  29. * @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
  30. * @since 101.1.1
  31. */
  32. protected $_collection;
  33. /**
  34. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  35. */
  36. protected $currentCustomer;
  37. /**
  38. * @var \Magento\Framework\Data\Helper\PostHelper
  39. */
  40. protected $postDataHelper;
  41. /**
  42. * @param \Magento\Catalog\Block\Product\Context $context
  43. * @param \Magento\Framework\App\Http\Context $httpContext
  44. * @param \Magento\Catalog\Helper\Product\ConfigurationPool $helperPool
  45. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  46. * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
  47. * @param array $data
  48. */
  49. public function __construct(
  50. \Magento\Catalog\Block\Product\Context $context,
  51. \Magento\Framework\App\Http\Context $httpContext,
  52. \Magento\Catalog\Helper\Product\ConfigurationPool $helperPool,
  53. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  54. \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
  55. array $data = []
  56. ) {
  57. parent::__construct(
  58. $context,
  59. $httpContext,
  60. $data
  61. );
  62. $this->_helperPool = $helperPool;
  63. $this->currentCustomer = $currentCustomer;
  64. $this->postDataHelper = $postDataHelper;
  65. }
  66. /**
  67. * Add wishlist conditions to collection
  68. *
  69. * @param \Magento\Wishlist\Model\ResourceModel\Item\Collection $collection
  70. * @return $this
  71. */
  72. protected function _prepareCollection($collection)
  73. {
  74. $collection->setInStockFilter(true)->setOrder('added_at', 'ASC');
  75. return $this;
  76. }
  77. /**
  78. * Paginate Wishlist Product Items collection
  79. *
  80. * @return void
  81. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  82. */
  83. private function paginateCollection()
  84. {
  85. $page = $this->getRequest()->getParam("p", 1);
  86. $limit = $this->getRequest()->getParam("limit", 10);
  87. $this->_collection
  88. ->setPageSize($limit)
  89. ->setCurPage($page);
  90. }
  91. /**
  92. * Retrieve Wishlist Product Items collection
  93. *
  94. * @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
  95. * @since 101.1.1
  96. */
  97. public function getWishlistItems()
  98. {
  99. if ($this->_collection === null) {
  100. $this->_collection = $this->_createWishlistItemCollection();
  101. $this->_prepareCollection($this->_collection);
  102. $this->paginateCollection();
  103. }
  104. return $this->_collection;
  105. }
  106. /**
  107. * Preparing global layout
  108. *
  109. * @return $this
  110. */
  111. protected function _prepareLayout()
  112. {
  113. parent::_prepareLayout();
  114. $this->pageConfig->getTitle()->set(__('My Wish List'));
  115. $this->getChildBlock('wishlist_item_pager')
  116. ->setUseContainer(
  117. true
  118. )->setShowAmounts(
  119. true
  120. )->setFrameLength(
  121. $this->_scopeConfig->getValue(
  122. 'design/pagination/pagination_frame',
  123. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  124. )
  125. )->setJump(
  126. $this->_scopeConfig->getValue(
  127. 'design/pagination/pagination_frame_skip',
  128. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  129. )
  130. )->setLimit(
  131. $this->getLimit()
  132. )
  133. ->setCollection($this->getWishlistItems());
  134. return $this;
  135. }
  136. /**
  137. * Retrieve Back URL
  138. *
  139. * @return string
  140. */
  141. public function getBackUrl()
  142. {
  143. return $this->getUrl('customer/account/');
  144. }
  145. /**
  146. * Sets all options render configurations
  147. *
  148. * @param null|array $optionCfg
  149. * @return $this
  150. */
  151. public function setOptionsRenderCfgs($optionCfg)
  152. {
  153. $this->_optionsCfg = $optionCfg;
  154. return $this;
  155. }
  156. /**
  157. * Returns all options render configurations
  158. *
  159. * @return array
  160. */
  161. public function getOptionsRenderCfgs()
  162. {
  163. return $this->_optionsCfg;
  164. }
  165. /**
  166. * Adds config for rendering product type options
  167. *
  168. * @param string $productType
  169. * @param string $helperName
  170. * @param null|string $template
  171. * @return $this
  172. */
  173. public function addOptionsRenderCfg($productType, $helperName, $template = null)
  174. {
  175. $this->_optionsCfg[$productType] = ['helper' => $helperName, 'template' => $template];
  176. return $this;
  177. }
  178. /**
  179. * Returns html for showing item options
  180. *
  181. * @param string $productType
  182. * @return array|null
  183. */
  184. public function getOptionsRenderCfg($productType)
  185. {
  186. if (isset($this->_optionsCfg[$productType])) {
  187. return $this->_optionsCfg[$productType];
  188. } elseif (isset($this->_optionsCfg['default'])) {
  189. return $this->_optionsCfg['default'];
  190. } else {
  191. return null;
  192. }
  193. }
  194. /**
  195. * Returns html for showing item options
  196. *
  197. * @param \Magento\Wishlist\Model\Item $item
  198. * @return string
  199. */
  200. public function getDetailsHtml(\Magento\Wishlist\Model\Item $item)
  201. {
  202. $cfg = $this->getOptionsRenderCfg($item->getProduct()->getTypeId());
  203. if (!$cfg) {
  204. return '';
  205. }
  206. $block = $this->getChildBlock('item_options');
  207. if (!$block) {
  208. return '';
  209. }
  210. if ($cfg['template']) {
  211. $template = $cfg['template'];
  212. } else {
  213. $cfgDefault = $this->getOptionsRenderCfg('default');
  214. if (!$cfgDefault) {
  215. return '';
  216. }
  217. $template = $cfgDefault['template'];
  218. }
  219. $block->setTemplate($template);
  220. $block->setOptionList($this->_helperPool->get($cfg['helper'])->getOptions($item));
  221. return $block->toHtml();
  222. }
  223. /**
  224. * Returns qty to show visually to user
  225. *
  226. * @param \Magento\Wishlist\Model\Item $item
  227. * @return float
  228. */
  229. public function getAddToCartQty(\Magento\Wishlist\Model\Item $item)
  230. {
  231. $qty = $this->getQty($item);
  232. return $qty ? $qty : 1;
  233. }
  234. /**
  235. * Get add all to cart params for POST request
  236. *
  237. * @return string
  238. */
  239. public function getAddAllToCartParams()
  240. {
  241. return $this->postDataHelper->getPostData(
  242. $this->getUrl('wishlist/index/allcart'),
  243. ['wishlist_id' => $this->getWishlistInstance()->getId()]
  244. );
  245. }
  246. /**
  247. * @inheritdoc
  248. */
  249. protected function _toHtml()
  250. {
  251. if ($this->currentCustomer->getCustomerId()) {
  252. return parent::_toHtml();
  253. } else {
  254. return '';
  255. }
  256. }
  257. }