Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Catalog\Helper\Image as ImageHelper;
14 
21 class Data
22 {
26  protected $imageHelper;
27 
31  private $imageUrlBuilder;
32 
37  public function __construct(ImageHelper $imageHelper, UrlBuilder $urlBuilder = null)
38  {
39  $this->imageHelper = $imageHelper;
40  $this->imageUrlBuilder = $urlBuilder ?? ObjectManager::getInstance()->get(UrlBuilder::class);
41  }
42 
49  public function getGalleryImages(ProductInterface $product)
50  {
51  $images = $product->getMediaGalleryImages();
52  if ($images instanceof \Magento\Framework\Data\Collection) {
54  foreach ($images as $image) {
55  $smallImageUrl = $this->imageUrlBuilder
56  ->getUrl($image->getFile(), 'product_page_image_small');
57  $image->setData('small_image_url', $smallImageUrl);
58 
59  $mediumImageUrl = $this->imageUrlBuilder
60  ->getUrl($image->getFile(), 'product_page_image_medium');
61  $image->setData('medium_image_url', $mediumImageUrl);
62 
63  $largeImageUrl = $this->imageUrlBuilder
64  ->getUrl($image->getFile(), 'product_page_image_large');
65  $image->setData('large_image_url', $largeImageUrl);
66  }
67  }
68 
69  return $images;
70  }
71 
79  public function getOptions($currentProduct, $allowedProducts)
80  {
81  $options = [];
82  $allowAttributes = $this->getAllowAttributes($currentProduct);
83 
84  foreach ($allowedProducts as $product) {
85  $productId = $product->getId();
86  foreach ($allowAttributes as $attribute) {
87  $productAttribute = $attribute->getProductAttribute();
88  $productAttributeId = $productAttribute->getId();
89  $attributeValue = $product->getData($productAttribute->getAttributeCode());
90 
91  $options[$productAttributeId][$attributeValue][] = $productId;
92  $options['index'][$productId][$productAttributeId] = $attributeValue;
93  }
94  }
95  return $options;
96  }
97 
104  public function getAllowAttributes($product)
105  {
106  return $product->getTypeInstance()->getConfigurableAttributes($product);
107  }
108 }
__construct(ImageHelper $imageHelper, UrlBuilder $urlBuilder=null)
Definition: Data.php:37
getOptions($currentProduct, $allowedProducts)
Definition: Data.php:79