AbstractBlock.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Block;
  7. use Magento\Catalog\Helper\Image;
  8. use Magento\Catalog\Model\Product\Image\UrlBuilder;
  9. use Magento\Framework\View\ConfigInterface;
  10. use Magento\Framework\App\ObjectManager;
  11. /**
  12. * Wishlist Product Items abstract Block
  13. */
  14. abstract class AbstractBlock extends \Magento\Catalog\Block\Product\AbstractProduct
  15. {
  16. /**
  17. * Wishlist Product Items Collection
  18. *
  19. * @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
  20. */
  21. protected $_collection;
  22. /**
  23. * Store wishlist Model
  24. *
  25. * @var \Magento\Wishlist\Model\Wishlist
  26. */
  27. protected $_wishlist;
  28. /**
  29. * @var \Magento\Framework\App\Http\Context
  30. */
  31. protected $httpContext;
  32. /**
  33. * @var ConfigInterface
  34. */
  35. private $viewConfig;
  36. /**
  37. * @var UrlBuilder
  38. */
  39. private $imageUrlBuilder;
  40. /**
  41. * @param \Magento\Catalog\Block\Product\Context $context
  42. * @param \Magento\Framework\App\Http\Context $httpContext
  43. * @param array $data
  44. * @param ConfigInterface|null $config
  45. * @param UrlBuilder|null $urlBuilder
  46. */
  47. public function __construct(
  48. \Magento\Catalog\Block\Product\Context $context,
  49. \Magento\Framework\App\Http\Context $httpContext,
  50. array $data = [],
  51. ConfigInterface $config = null,
  52. UrlBuilder $urlBuilder = null
  53. ) {
  54. $this->httpContext = $httpContext;
  55. parent::__construct(
  56. $context,
  57. $data
  58. );
  59. $this->viewConfig = $config ?? ObjectManager::getInstance()->get(ConfigInterface::class);
  60. $this->imageUrlBuilder = $urlBuilder ?? ObjectManager::getInstance()->get(UrlBuilder::class);
  61. }
  62. /**
  63. * Retrieve Wishlist Data Helper
  64. *
  65. * @return \Magento\Wishlist\Helper\Data
  66. */
  67. protected function _getHelper()
  68. {
  69. return $this->_wishlistHelper;
  70. }
  71. /**
  72. * Retrieve Wishlist model
  73. *
  74. * @return \Magento\Wishlist\Model\Wishlist
  75. */
  76. protected function _getWishlist()
  77. {
  78. return $this->_getHelper()->getWishlist();
  79. }
  80. /**
  81. * Prepare additional conditions to collection
  82. *
  83. * @param \Magento\Wishlist\Model\ResourceModel\Item\Collection $collection
  84. * @return \Magento\Wishlist\Block\Customer\Wishlist
  85. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  86. */
  87. protected function _prepareCollection($collection)
  88. {
  89. return $this;
  90. }
  91. /**
  92. * Create wishlist item collection
  93. *
  94. * @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
  95. */
  96. protected function _createWishlistItemCollection()
  97. {
  98. return $this->_getWishlist()->getItemCollection();
  99. }
  100. /**
  101. * Retrieve Wishlist Product Items collection
  102. *
  103. * @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
  104. */
  105. public function getWishlistItems()
  106. {
  107. if ($this->_collection === null) {
  108. $this->_collection = $this->_createWishlistItemCollection();
  109. $this->_prepareCollection($this->_collection);
  110. }
  111. return $this->_collection;
  112. }
  113. /**
  114. * Retrieve wishlist instance
  115. *
  116. * @return \Magento\Wishlist\Model\Wishlist
  117. */
  118. public function getWishlistInstance()
  119. {
  120. return $this->_getWishlist();
  121. }
  122. /**
  123. * Retrieve params for Removing item from wishlist
  124. *
  125. * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  126. *
  127. * @return string
  128. */
  129. public function getItemRemoveParams($item)
  130. {
  131. return $this->_getHelper()->getRemoveParams($item);
  132. }
  133. /**
  134. * Retrieve Add Item to shopping cart params for POST request
  135. *
  136. * @param string|\Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  137. * @return string
  138. */
  139. public function getItemAddToCartParams($item)
  140. {
  141. return $this->_getHelper()->getAddToCartParams($item);
  142. }
  143. /**
  144. * Retrieve Add Item to shopping cart URL from shared wishlist
  145. *
  146. * @param string|\Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
  147. * @return string
  148. */
  149. public function getSharedItemAddToCartUrl($item)
  150. {
  151. return $this->_getHelper()->getSharedAddToCartUrl($item);
  152. }
  153. /**
  154. * Retrieve URL for adding All items to shopping cart from shared wishlist
  155. *
  156. * @return string
  157. */
  158. public function getSharedAddAllToCartUrl()
  159. {
  160. return $this->_getHelper()->getSharedAddAllToCartUrl();
  161. }
  162. /**
  163. * Retrieve params for adding Product to wishlist
  164. *
  165. * @param \Magento\Catalog\Model\Product $product
  166. * @return string
  167. */
  168. public function getAddToWishlistParams($product)
  169. {
  170. return $this->_getHelper()->getAddParams($product);
  171. }
  172. /**
  173. * Returns item configure url in wishlist
  174. *
  175. * @param \Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $product
  176. *
  177. * @return string
  178. */
  179. public function getItemConfigureUrl($product)
  180. {
  181. return $this->_getHelper()->getConfigureUrl($product);
  182. }
  183. /**
  184. * Retrieve Escaped Description for Wishlist Item
  185. *
  186. * @param \Magento\Catalog\Model\Product $item
  187. * @return string
  188. */
  189. public function getEscapedDescription($item)
  190. {
  191. if ($item->getDescription()) {
  192. return $this->escapeHtml($item->getDescription());
  193. }
  194. return '&nbsp;';
  195. }
  196. /**
  197. * Check Wishlist item has description
  198. *
  199. * @param \Magento\Catalog\Model\Product $item
  200. * @return bool
  201. */
  202. public function hasDescription($item)
  203. {
  204. return trim($item->getDescription()) != '';
  205. }
  206. /**
  207. * Retrieve formated Date
  208. *
  209. * @param string $date
  210. * @deprecated 101.1.1
  211. * @return string
  212. */
  213. public function getFormatedDate($date)
  214. {
  215. return $this->getFormattedDate($date);
  216. }
  217. /**
  218. * Retrieve formatted Date
  219. *
  220. * @param string $date
  221. * @return string
  222. */
  223. public function getFormattedDate($date)
  224. {
  225. return $this->formatDate($date, \IntlDateFormatter::MEDIUM);
  226. }
  227. /**
  228. * Check is the wishlist has a salable product(s)
  229. *
  230. * @return bool
  231. */
  232. public function isSaleable()
  233. {
  234. foreach ($this->getWishlistItems() as $item) {
  235. if ($item->getProduct()->isSaleable()) {
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. /**
  242. * Retrieve wishlist loaded items count
  243. *
  244. * @return int
  245. */
  246. public function getWishlistItemsCount()
  247. {
  248. return $this->_getWishlist()->getItemsCount();
  249. }
  250. /**
  251. * Retrieve Qty from item
  252. *
  253. * @param \Magento\Wishlist\Model\Item|\Magento\Catalog\Model\Product $item
  254. * @return float
  255. */
  256. public function getQty($item)
  257. {
  258. $qty = $item->getQty() * 1;
  259. if (!$qty) {
  260. $qty = 1;
  261. }
  262. return $qty;
  263. }
  264. /**
  265. * Check is the wishlist has items
  266. *
  267. * @return bool
  268. */
  269. public function hasWishlistItems()
  270. {
  271. return $this->getWishlistItemsCount() > 0;
  272. }
  273. /**
  274. * Retrieve URL to item Product
  275. *
  276. * @param \Magento\Wishlist\Model\Item|\Magento\Catalog\Model\Product $item
  277. * @param array $additional
  278. * @return string
  279. */
  280. public function getProductUrl($item, $additional = [])
  281. {
  282. return $this->_getHelper()->getProductUrl($item, $additional);
  283. }
  284. /**
  285. * Product image url getter
  286. *
  287. * @param \Magento\Catalog\Model\Product $product
  288. * @return string
  289. */
  290. public function getImageUrl($product)
  291. {
  292. $viewImageConfig = $this->viewConfig->getViewConfig()->getMediaAttributes(
  293. 'Magento_Catalog',
  294. Image::MEDIA_TYPE_CONFIG_NODE,
  295. 'wishlist_small_image'
  296. );
  297. return $this->imageUrlBuilder->getUrl(
  298. $product->getData($viewImageConfig['type']),
  299. 'wishlist_small_image'
  300. );
  301. }
  302. /**
  303. * Return HTML block with price
  304. *
  305. * @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
  306. * @param string $priceType
  307. * @param string $renderZone
  308. * @param array $arguments
  309. * @return string|null
  310. */
  311. public function getItemPriceHtml(
  312. \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item,
  313. $priceType = \Magento\Catalog\Pricing\Price\ConfiguredPriceInterface::CONFIGURED_PRICE_CODE,
  314. $renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
  315. array $arguments = []
  316. ) {
  317. /** @var \Magento\Framework\Pricing\Render $priceRender */
  318. $priceRender = $this->getLayout()->getBlock('product.price.render.default');
  319. $priceRender->setItem($item);
  320. $arguments += [
  321. 'zone' => $renderZone,
  322. 'render_block' => $priceRender
  323. ];
  324. return $priceRender ? $priceRender->render($priceType, $item->getProduct(), $arguments) : null;
  325. }
  326. }