Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Data Class Reference

Public Member Functions

 __construct (CollectionFactory $productCollectionFactory, ProductRepositoryInterface $productRepository, StoreManagerInterface $storeManager, SwatchCollectionFactory $swatchCollectionFactory, UrlBuilder $urlBuilder, Json $serializer=null, SwatchAttributesProvider $swatchAttributesProvider=null, SwatchAttributeType $swatchTypeChecker=null)
 
 assembleAdditionalDataEavAttribute (Attribute $attribute)
 
 loadFirstVariationWithSwatchImage (Product $configurableProduct, array $requiredAttributes)
 
 loadFirstVariationWithImage (Product $configurableProduct, array $requiredAttributes)
 
 loadVariationByFallback (Product $parentProduct, array $attributes)
 
 getProductMediaGallery (ModelProduct $product)
 
 getSwatchAttributesAsArray (Product $product)
 
 isProductHasSwatch (Product $product)
 
 isSwatchAttribute (Attribute $attribute)
 
 isVisualSwatch (Attribute $attribute)
 
 isTextSwatch (Attribute $attribute)
 

Data Fields

const EMPTY_IMAGE_VALUE = 'no_selection'
 
const DEFAULT_STORE_ID = 0
 

Protected Member Functions

 getMetadataPool ()
 

Protected Attributes

 $productCollectionFactory
 
 $productRepository
 
 $storeManager
 
 $swatchCollectionFactory
 
 $eavAttributeAdditionalDataKeys
 

Detailed Description

Class Helper Data

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 30 of file Data.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( CollectionFactory  $productCollectionFactory,
ProductRepositoryInterface  $productRepository,
StoreManagerInterface  $storeManager,
SwatchCollectionFactory  $swatchCollectionFactory,
UrlBuilder  $urlBuilder,
Json  $serializer = null,
SwatchAttributesProvider  $swatchAttributesProvider = null,
SwatchAttributeType  $swatchTypeChecker = null 
)
Parameters
CollectionFactory$productCollectionFactory
ProductRepositoryInterface$productRepository
StoreManagerInterface$storeManager
SwatchCollectionFactory$swatchCollectionFactory
UrlBuilder$urlBuilder
Json | null$serializer
SwatchAttributesProvider$swatchAttributesProvider
SwatchAttributeType | null$swatchTypeChecker

Definition at line 112 of file Data.php.

121  {
122  $this->productCollectionFactory = $productCollectionFactory;
123  $this->productRepository = $productRepository;
124  $this->storeManager = $storeManager;
125  $this->swatchCollectionFactory = $swatchCollectionFactory;
126  $this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
127  $this->swatchAttributesProvider = $swatchAttributesProvider
128  ?: ObjectManager::getInstance()->get(SwatchAttributesProvider::class);
129  $this->swatchTypeChecker = $swatchTypeChecker
130  ?: ObjectManager::getInstance()->create(SwatchAttributeType::class);
131  $this->imageUrlBuilder = $urlBuilder;
132  }

Member Function Documentation

◆ assembleAdditionalDataEavAttribute()

assembleAdditionalDataEavAttribute ( Attribute  $attribute)
Parameters
Attribute$attribute
Returns
$this

Definition at line 138 of file Data.php.

139  {
140  $initialAdditionalData = [];
141  $additionalData = (string)$attribute->getData('additional_data');
142  if (!empty($additionalData)) {
143  $additionalData = $this->serializer->unserialize($additionalData);
144  if (is_array($additionalData)) {
145  $initialAdditionalData = $additionalData;
146  }
147  }
148 
149  $dataToAdd = [];
150  foreach ($this->eavAttributeAdditionalDataKeys as $key) {
151  $dataValue = $attribute->getData($key);
152  if (null !== $dataValue) {
153  $dataToAdd[$key] = $dataValue;
154  }
155  }
156  $additionalData = array_merge($initialAdditionalData, $dataToAdd);
157  $attribute->setData('additional_data', $this->serializer->serialize($additionalData));
158  return $this;
159  }

◆ getMetadataPool()

getMetadataPool ( )
protected

Get product metadata pool.

Returns
\Magento\Framework\EntityManager\MetadataPool @deprecared

Definition at line 549 of file Data.php.

550  {
551  if (!$this->metadataPool) {
553  ->get(\Magento\Framework\EntityManager\MetadataPool::class);
554  }
555  return $this->metadataPool;
556  }

◆ getProductMediaGallery()

getProductMediaGallery ( ModelProduct  $product)

Method getting full media gallery for current Product Array structure: [ ['image'] => 'http://url/pub/media/catalog/product/2/0/blabla.jpg', ['mediaGallery'] => [ galleryImageId1 => simpleProductImage1.jpg, galleryImageId2 => simpleProductImage2.jpg, ..., ] ]

Parameters
ModelProduct$product
Returns
array

Definition at line 313 of file Data.php.

314  {
315  $baseImage = null;
316  $gallery = [];
317 
318  $mediaGallery = $product->getMediaGalleryEntries();
319  foreach ($mediaGallery as $mediaEntry) {
320  if ($mediaEntry->isDisabled()) {
321  continue;
322  }
323 
324  if (in_array('image', $mediaEntry->getTypes(), true) || !$baseImage) {
325  $baseImage = $mediaEntry->getFile();
326  }
327 
328  $gallery[$mediaEntry->getId()] = $this->getAllSizeImages($mediaEntry->getFile());
329  }
330 
331  if (!$baseImage) {
332  return [];
333  }
334 
335  $resultGallery = $this->getAllSizeImages($baseImage);
336  $resultGallery['gallery'] = $gallery;
337 
338  return $resultGallery;
339  }

◆ getSwatchAttributesAsArray()

getSwatchAttributesAsArray ( Product  $product)

Retrieve all visible Swatch attributes for current product.

Parameters
Product$product
Returns
array

Definition at line 394 of file Data.php.

395  {
396  $result = [];
397  $swatchAttributes = $this->getSwatchAttributes($product);
398  foreach ($swatchAttributes as $swatchAttribute) {
399  $swatchAttribute->setStoreId($this->storeManager->getStore()->getId());
400  $attributeData = $swatchAttribute->getData();
401  foreach ($swatchAttribute->getSource()->getAllOptions(false) as $option) {
402  $attributeData['options'][$option['value']] = $option['label'];
403  }
404  $result[$attributeData['attribute_id']] = $attributeData;
405  }
406 
407  return $result;
408  }

◆ isProductHasSwatch()

isProductHasSwatch ( Product  $product)

Check if the Product has Swatch attributes

Parameters
Product$product
Returns
bool

Definition at line 505 of file Data.php.

506  {
507  return !empty($this->getSwatchAttributes($product));
508  }

◆ isSwatchAttribute()

isSwatchAttribute ( Attribute  $attribute)

Check if an attribute is Swatch

Parameters
Attribute$attribute
Returns
bool

Definition at line 516 of file Data.php.

517  {
518  return $this->swatchTypeChecker->isSwatchAttribute($attribute);
519  }

◆ isTextSwatch()

isTextSwatch ( Attribute  $attribute)

Is attribute Textual Swatch

Parameters
Attribute$attribute
Returns
bool

Definition at line 538 of file Data.php.

539  {
540  return $this->swatchTypeChecker->isTextSwatch($attribute);
541  }

◆ isVisualSwatch()

isVisualSwatch ( Attribute  $attribute)

Is attribute Visual Swatch

Parameters
Attribute$attribute
Returns
bool

Definition at line 527 of file Data.php.

528  {
529  return $this->swatchTypeChecker->isVisualSwatch($attribute);
530  }

◆ loadFirstVariationWithImage()

loadFirstVariationWithImage ( Product  $configurableProduct,
array  $requiredAttributes 
)
Parameters
Product$configurableProduct
array$requiredAttributes
Returns
bool|Product

Definition at line 221 of file Data.php.

222  {
223  return $this->loadFirstVariation('image', $configurableProduct, $requiredAttributes);
224  }

◆ loadFirstVariationWithSwatchImage()

loadFirstVariationWithSwatchImage ( Product  $configurableProduct,
array  $requiredAttributes 
)
Parameters
Product$configurableProduct
array$requiredAttributes
Returns
bool|Product

Definition at line 211 of file Data.php.

212  {
213  return $this->loadFirstVariation('swatch_image', $configurableProduct, $requiredAttributes);
214  }

◆ loadVariationByFallback()

loadVariationByFallback ( Product  $parentProduct,
array  $attributes 
)

Load Variation Product using fallback

Parameters
Product$parentProduct
array$attributes
Returns
bool|Product

Definition at line 233 of file Data.php.

234  {
235  if (!$this->isProductHasSwatch($parentProduct)) {
236  return false;
237  }
238 
239  $productCollection = $this->productCollectionFactory->create();
240 
241  $productLinkedFiled = $this->getMetadataPool()
242  ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
243  ->getLinkField();
244  $parentId = $parentProduct->getData($productLinkedFiled);
245 
246  $this->addFilterByParent($productCollection, $parentId);
247 
248  $configurableAttributes = $this->getAttributesFromConfigurable($parentProduct);
249  $allAttributesArray = [];
250  foreach ($configurableAttributes as $attribute) {
251  if (!empty($attribute['default_value'])) {
252  $allAttributesArray[$attribute['attribute_code']] = $attribute['default_value'];
253  }
254  }
255 
256  $resultAttributesToFilter = array_merge(
257  $attributes,
258  array_diff_key($allAttributesArray, $attributes)
259  );
260 
261  $this->addFilterByAttributes($productCollection, $resultAttributesToFilter);
262 
263  $variationProduct = $productCollection->getFirstItem();
264  if ($variationProduct && $variationProduct->getId()) {
265  return $this->productRepository->getById($variationProduct->getId());
266  }
267 
268  return false;
269  }
$attributes
Definition: matrix.phtml:13
isProductHasSwatch(Product $product)
Definition: Data.php:505

Field Documentation

◆ $eavAttributeAdditionalDataKeys

$eavAttributeAdditionalDataKeys
protected
Initial value:
= [
'update_product_preview_image',
'use_product_image_for_swatch'
]

Definition at line 79 of file Data.php.

◆ $productCollectionFactory

$productCollectionFactory
protected

Definition at line 45 of file Data.php.

◆ $productRepository

$productRepository
protected

Definition at line 50 of file Data.php.

◆ $storeManager

$storeManager
protected

Definition at line 55 of file Data.php.

◆ $swatchCollectionFactory

$swatchCollectionFactory
protected

Definition at line 60 of file Data.php.

◆ DEFAULT_STORE_ID

const DEFAULT_STORE_ID = 0

Default store ID

Definition at line 40 of file Data.php.

◆ EMPTY_IMAGE_VALUE

const EMPTY_IMAGE_VALUE = 'no_selection'

When we init media gallery empty image types contain this value.

Definition at line 35 of file Data.php.


The documentation for this class was generated from the following file: