Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Configurable.php
Go to the documentation of this file.
1 <?php
7 
19 use Magento\Swatches\Helper\Data as SwatchData;
24 
33  \Magento\Framework\DataObject\IdentityInterface
34 {
38  const SWATCH_RENDERER_TEMPLATE = 'Magento_Swatches::product/view/renderer.phtml';
39 
43  const CONFIGURABLE_RENDERER_TEMPLATE = 'Magento_ConfigurableProduct::product/view/type/options/configurable.phtml';
44 
48  const MEDIA_CALLBACK_ACTION = 'swatches/ajax/media';
49 
53  const SWATCH_IMAGE_NAME = 'swatchImage';
54 
58  const SWATCH_THUMBNAIL_NAME = 'swatchThumb';
59 
63  protected $product;
64 
68  protected $swatchHelper;
69 
73  protected $swatchMediaHelper;
74 
83 
87  private $swatchAttributesProvider;
88 
92  private $imageUrlBuilder;
93 
110  public function __construct(
111  Context $context,
114  Data $helper,
119  SwatchData $swatchHelper,
121  array $data = [],
122  SwatchAttributesProvider $swatchAttributesProvider = null,
123  UrlBuilder $imageUrlBuilder = null
124  ) {
125  $this->swatchHelper = $swatchHelper;
126  $this->swatchMediaHelper = $swatchMediaHelper;
127  $this->swatchAttributesProvider = $swatchAttributesProvider
128  ?: ObjectManager::getInstance()->get(SwatchAttributesProvider::class);
129  $this->imageUrlBuilder = $imageUrlBuilder ?? ObjectManager::getInstance()->get(UrlBuilder::class);
130  parent::__construct(
131  $context,
132  $arrayUtils,
133  $jsonEncoder,
134  $helper,
139  $data
140  );
141  }
142 
149  public function getCacheKey()
150  {
151  return parent::getCacheKey() . '-' . $this->getProduct()->getId();
152  }
153 
160  protected function getCacheLifetime()
161  {
162  return parent::hasCacheLifetime() ? parent::getCacheLifetime() : 3600;
163  }
164 
170  public function getJsonSwatchConfig()
171  {
173  $allOptionIds = $this->getConfigurableOptionsIds($attributesData);
174  $swatchesData = $this->swatchHelper->getSwatchesByOptionsId($allOptionIds);
175 
176  $config = [];
177  foreach ($attributesData as $attributeId => $attributeDataArray) {
178  if (isset($attributeDataArray['options'])) {
179  $config[$attributeId] = $this->addSwatchDataForAttribute(
180  $attributeDataArray['options'],
181  $swatchesData,
182  $attributeDataArray
183  );
184  }
185  }
186 
187  return $this->jsonEncoder->encode($config);
188  }
189 
196  public function getNumberSwatchesPerProduct()
197  {
198  return $this->_scopeConfig->getValue(
199  'catalog/frontend/swatches_per_product',
201  );
202  }
203 
210  public function setProduct(Product $product)
211  {
212  $this->product = $product;
213  return $this;
214  }
215 
221  public function getProduct()
222  {
223  if (!$this->product) {
224  $this->product = parent::getProduct();
225  }
226 
227  return $this->product;
228  }
229 
233  protected function getSwatchAttributesData()
234  {
235  return $this->swatchHelper->getSwatchAttributesAsArray($this->getProduct());
236  }
237 
244  protected function initIsProductHasSwatchAttribute()
245  {
246  $this->isProductHasSwatchAttribute = $this->swatchHelper->isProductHasSwatch($this->getProduct());
247  }
248 
255  protected function isProductHasSwatchAttribute()
256  {
257  $swatchAttributes = $this->swatchAttributesProvider->provide($this->getProduct());
258  return count($swatchAttributes) > 0;
259  }
260 
269  protected function addSwatchDataForAttribute(
270  array $options,
271  array $swatchesCollectionArray,
272  array $attributeDataArray
273  ) {
274  $result = [];
275  foreach ($options as $optionId => $label) {
276  if (isset($swatchesCollectionArray[$optionId])) {
277  $result[$optionId] = $this->extractNecessarySwatchData($swatchesCollectionArray[$optionId]);
278  $result[$optionId] = $this->addAdditionalMediaData($result[$optionId], $optionId, $attributeDataArray);
279  $result[$optionId]['label'] = $label;
280  }
281  }
282 
283  return $result;
284  }
285 
294  protected function addAdditionalMediaData(array $swatch, $optionId, array $attributeDataArray)
295  {
296  if (isset($attributeDataArray['use_product_image_for_swatch'])
297  && $attributeDataArray['use_product_image_for_swatch']
298  ) {
299  $variationMedia = $this->getVariationMedia($attributeDataArray['attribute_code'], $optionId);
300  if (! empty($variationMedia)) {
301  $swatch['type'] = Swatch::SWATCH_TYPE_VISUAL_IMAGE;
302  $swatch = array_merge($swatch, $variationMedia);
303  }
304  }
305  return $swatch;
306  }
307 
314  protected function extractNecessarySwatchData(array $swatchDataArray)
315  {
316  $result['type'] = $swatchDataArray['type'];
317 
318  if ($result['type'] == Swatch::SWATCH_TYPE_VISUAL_IMAGE && !empty($swatchDataArray['value'])) {
319  $result['value'] = $this->swatchMediaHelper->getSwatchAttributeImage(
321  $swatchDataArray['value']
322  );
323  $result['thumb'] = $this->swatchMediaHelper->getSwatchAttributeImage(
325  $swatchDataArray['value']
326  );
327  } else {
328  $result['value'] = $swatchDataArray['value'];
329  }
330 
331  return $result;
332  }
333 
342  {
343  $variationProduct = $this->swatchHelper->loadFirstVariationWithSwatchImage(
344  $this->getProduct(),
346  );
347 
348  if (!$variationProduct) {
349  $variationProduct = $this->swatchHelper->loadFirstVariationWithImage(
350  $this->getProduct(),
352  );
353  }
354 
355  $variationMediaArray = [];
356  if ($variationProduct) {
357  $variationMediaArray = [
358  'value' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_IMAGE_NAME),
359  'thumb' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_THUMBNAIL_NAME),
360  ];
361  }
362 
363  return $variationMediaArray;
364  }
365 
371  protected function getSwatchProductImage(Product $childProduct, $imageType)
372  {
373  if ($this->isProductHasImage($childProduct, Swatch::SWATCH_IMAGE_NAME)) {
374  $swatchImageId = $imageType;
375  $imageAttributes = ['type' => Swatch::SWATCH_IMAGE_NAME];
376  } elseif ($this->isProductHasImage($childProduct, 'image')) {
377  $swatchImageId = $imageType == Swatch::SWATCH_IMAGE_NAME ? 'swatch_image_base' : 'swatch_thumb_base';
378  $imageAttributes = ['type' => 'image'];
379  }
380 
381  if (!empty($swatchImageId) && !empty($imageAttributes['type'])) {
382  return $this->imageUrlBuilder->getUrl($childProduct->getData($imageAttributes['type']), $swatchImageId);
383  }
384  }
385 
391  protected function isProductHasImage(Product $product, $imageType)
392  {
393  return $product->getData($imageType) !== null && $product->getData($imageType) != SwatchData::EMPTY_IMAGE_VALUE;
394  }
395 
401  protected function getConfigurableOptionsIds(array $attributeData)
402  {
403  $ids = [];
404  foreach ($this->getAllowProducts() as $product) {
406  foreach ($this->helper->getAllowAttributes($this->getProduct()) as $attribute) {
407  $productAttribute = $attribute->getProductAttribute();
408  $productAttributeId = $productAttribute->getId();
409  if (isset($attributeData[$productAttributeId])) {
410  $ids[$product->getData($productAttribute->getAttributeCode())] = 1;
411  }
412  }
413  }
414  return array_keys($ids);
415  }
416 
423  public function toHtml()
424  {
425  $this->setTemplate(
426  $this->getRendererTemplate()
427  );
428 
429  return parent::toHtml();
430  }
431 
437  protected function _toHtml()
438  {
439  return $this->getHtmlOutput();
440  }
441 
449  protected function getRendererTemplate()
450  {
451  return $this->isProductHasSwatchAttribute() ?
453  }
454 
459  protected function getHtmlOutput()
460  {
461  return parent::_toHtml();
462  }
463 
467  public function getMediaCallback()
468  {
469  return $this->getUrl(self::MEDIA_CALLBACK_ACTION, ['_secure' => $this->getRequest()->isSecure()]);
470  }
471 
478  public function getIdentities()
479  {
480  if ($this->product instanceof \Magento\Framework\DataObject\IdentityInterface) {
481  return $this->product->getIdentities();
482  } else {
483  return [];
484  }
485  }
486 
492  public function getJsonSwatchSizeConfig()
493  {
494  $imageConfig = $this->swatchMediaHelper->getImageConfig();
495  $sizeConfig = [];
496 
497  $sizeConfig[self::SWATCH_IMAGE_NAME]['width'] = $imageConfig[Swatch::SWATCH_IMAGE_NAME]['width'];
498  $sizeConfig[self::SWATCH_IMAGE_NAME]['height'] = $imageConfig[Swatch::SWATCH_IMAGE_NAME]['height'];
499  $sizeConfig[self::SWATCH_THUMBNAIL_NAME]['height'] = $imageConfig[Swatch::SWATCH_THUMBNAIL_NAME]['height'];
500  $sizeConfig[self::SWATCH_THUMBNAIL_NAME]['width'] = $imageConfig[Swatch::SWATCH_THUMBNAIL_NAME]['width'];
501 
502  return $this->jsonEncoder->encode($sizeConfig);
503  }
504 }
getSwatchProductImage(Product $childProduct, $imageType)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
$label
Definition: details.phtml:21
$attributesData
__construct(Context $context, ArrayUtils $arrayUtils, EncoderInterface $jsonEncoder, Data $helper, CatalogProduct $catalogProduct, CurrentCustomer $currentCustomer, PriceCurrencyInterface $priceCurrency, ConfigurableAttributeData $configurableAttributeData, SwatchData $swatchHelper, Media $swatchMediaHelper, array $data=[], SwatchAttributesProvider $swatchAttributesProvider=null, UrlBuilder $imageUrlBuilder=null)
$attributeCode
Definition: extend.phtml:12
addAdditionalMediaData(array $swatch, $optionId, array $attributeDataArray)
addSwatchDataForAttribute(array $options, array $swatchesCollectionArray, array $attributeDataArray)