Data.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Helper;
  7. use Magento\Catalog\Model\Product\Image\UrlBuilder;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Catalog\Helper\Image as ImageHelper;
  10. use Magento\Catalog\Api\Data\ProductInterface;
  11. use Magento\Catalog\Model\Product\Image;
  12. /**
  13. * Class Data
  14. * Helper class for getting options
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Data
  19. {
  20. /**
  21. * @var ImageHelper
  22. */
  23. protected $imageHelper;
  24. /**
  25. * @var UrlBuilder
  26. */
  27. private $imageUrlBuilder;
  28. /**
  29. * @param ImageHelper $imageHelper
  30. * @param UrlBuilder $urlBuilder
  31. */
  32. public function __construct(ImageHelper $imageHelper, UrlBuilder $urlBuilder = null)
  33. {
  34. $this->imageHelper = $imageHelper;
  35. $this->imageUrlBuilder = $urlBuilder ?? ObjectManager::getInstance()->get(UrlBuilder::class);
  36. }
  37. /**
  38. * Retrieve collection of gallery images
  39. *
  40. * @param ProductInterface $product
  41. * @return Image[]|null
  42. */
  43. public function getGalleryImages(ProductInterface $product)
  44. {
  45. $images = $product->getMediaGalleryImages();
  46. if ($images instanceof \Magento\Framework\Data\Collection) {
  47. /** @var $image Image */
  48. foreach ($images as $image) {
  49. $smallImageUrl = $this->imageUrlBuilder
  50. ->getUrl($image->getFile(), 'product_page_image_small');
  51. $image->setData('small_image_url', $smallImageUrl);
  52. $mediumImageUrl = $this->imageUrlBuilder
  53. ->getUrl($image->getFile(), 'product_page_image_medium');
  54. $image->setData('medium_image_url', $mediumImageUrl);
  55. $largeImageUrl = $this->imageUrlBuilder
  56. ->getUrl($image->getFile(), 'product_page_image_large');
  57. $image->setData('large_image_url', $largeImageUrl);
  58. }
  59. }
  60. return $images;
  61. }
  62. /**
  63. * Get Options for Configurable Product Options
  64. *
  65. * @param \Magento\Catalog\Model\Product $currentProduct
  66. * @param array $allowedProducts
  67. * @return array
  68. */
  69. public function getOptions($currentProduct, $allowedProducts)
  70. {
  71. $options = [];
  72. $allowAttributes = $this->getAllowAttributes($currentProduct);
  73. foreach ($allowedProducts as $product) {
  74. $productId = $product->getId();
  75. foreach ($allowAttributes as $attribute) {
  76. $productAttribute = $attribute->getProductAttribute();
  77. $productAttributeId = $productAttribute->getId();
  78. $attributeValue = $product->getData($productAttribute->getAttributeCode());
  79. $options[$productAttributeId][$attributeValue][] = $productId;
  80. $options['index'][$productId][$productAttributeId] = $attributeValue;
  81. }
  82. }
  83. return $options;
  84. }
  85. /**
  86. * Get allowed attributes
  87. *
  88. * @param \Magento\Catalog\Model\Product $product
  89. * @return array
  90. */
  91. public function getAllowAttributes($product)
  92. {
  93. return $product->getTypeInstance()->getConfigurableAttributes($product);
  94. }
  95. }