AddToWishlist.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Block;
  7. /**
  8. * Wishlist js plugin initialization block
  9. *
  10. * @api
  11. * @since 100.1.0
  12. */
  13. class AddToWishlist extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Product types
  17. *
  18. * @var array|null
  19. */
  20. private $productTypes;
  21. /**
  22. * @param \Magento\Framework\View\Element\Template\Context $context
  23. * @param array $data
  24. */
  25. public function __construct(
  26. \Magento\Framework\View\Element\Template\Context $context,
  27. array $data = []
  28. ) {
  29. parent::__construct(
  30. $context,
  31. $data
  32. );
  33. }
  34. /**
  35. * Returns wishlist widget options
  36. *
  37. * @return array
  38. * @since 100.1.0
  39. */
  40. public function getWishlistOptions()
  41. {
  42. return ['productType' => $this->getProductTypes()];
  43. }
  44. /**
  45. * Returns an array of product types
  46. *
  47. * @return array|null
  48. * @throws \Magento\Framework\Exception\LocalizedException
  49. */
  50. private function getProductTypes()
  51. {
  52. if ($this->productTypes === null) {
  53. $this->productTypes = [];
  54. $block = $this->getLayout()->getBlock('category.products.list');
  55. if ($block) {
  56. $productCollection = $block->getLoadedProductCollection();
  57. $productTypes = [];
  58. /** @var $product \Magento\Catalog\Model\Product */
  59. foreach ($productCollection as $product) {
  60. $productTypes[] = $this->escapeHtml($product->getTypeId());
  61. }
  62. $this->productTypes = array_unique($productTypes);
  63. }
  64. }
  65. return $this->productTypes;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. * @since 100.1.0
  70. */
  71. protected function _toHtml()
  72. {
  73. if (!$this->getProductTypes()) {
  74. return '';
  75. }
  76. return parent::_toHtml();
  77. }
  78. }