Skip to content

Commit d9fc848

Browse files
authored
Merge pull request #2097 from magento-chaika/MAGETWO-85661
Fixed issue: MAGETWO-85661 Can not revert "Swatches" attribute type
2 parents 9d9a6c3 + 6e957af commit d9fc848

File tree

6 files changed

+334
-282
lines changed

6 files changed

+334
-282
lines changed

app/code/Magento/Swatches/Helper/Data.php

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Swatches\Helper;
78

89
use Magento\Catalog\Api\Data\ProductInterface as Product;
@@ -19,6 +20,7 @@
1920
use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory as SwatchCollectionFactory;
2021
use Magento\Swatches\Model\Swatch;
2122
use Magento\Swatches\Model\SwatchAttributesProvider;
23+
use Magento\Swatches\Model\SwatchAttributeType;
2224

2325
/**
2426
* Class Helper Data
@@ -94,6 +96,11 @@ class Data
9496
*/
9597
private $serializer;
9698

99+
/**
100+
* @var SwatchAttributeType
101+
*/
102+
private $swatchTypeChecker;
103+
97104
/**
98105
* @param CollectionFactory $productCollectionFactory
99106
* @param ProductRepositoryInterface $productRepository
@@ -102,6 +109,7 @@ class Data
102109
* @param Image $imageHelper
103110
* @param Json|null $serializer
104111
* @param SwatchAttributesProvider $swatchAttributesProvider
112+
* @param SwatchAttributeType|null $swatchTypeChecker
105113
*/
106114
public function __construct(
107115
CollectionFactory $productCollectionFactory,
@@ -110,16 +118,19 @@ public function __construct(
110118
SwatchCollectionFactory $swatchCollectionFactory,
111119
Image $imageHelper,
112120
Json $serializer = null,
113-
SwatchAttributesProvider $swatchAttributesProvider = null
121+
SwatchAttributesProvider $swatchAttributesProvider = null,
122+
SwatchAttributeType $swatchTypeChecker = null
114123
) {
115-
$this->productCollectionFactory = $productCollectionFactory;
124+
$this->productCollectionFactory = $productCollectionFactory;
116125
$this->productRepository = $productRepository;
117126
$this->storeManager = $storeManager;
118127
$this->swatchCollectionFactory = $swatchCollectionFactory;
119128
$this->imageHelper = $imageHelper;
120129
$this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
121130
$this->swatchAttributesProvider = $swatchAttributesProvider
122131
?: ObjectManager::getInstance()->get(SwatchAttributesProvider::class);
132+
$this->swatchTypeChecker = $swatchTypeChecker
133+
?: ObjectManager::getInstance()->create(SwatchAttributeType::class);
123134
}
124135

125136
/**
@@ -129,7 +140,7 @@ public function __construct(
129140
public function assembleAdditionalDataEavAttribute(Attribute $attribute)
130141
{
131142
$initialAdditionalData = [];
132-
$additionalData = (string) $attribute->getData('additional_data');
143+
$additionalData = (string)$attribute->getData('additional_data');
133144
if (!empty($additionalData)) {
134145
$additionalData = $this->serializer->unserialize($additionalData);
135146
if (is_array($additionalData)) {
@@ -149,26 +160,6 @@ public function assembleAdditionalDataEavAttribute(Attribute $attribute)
149160
return $this;
150161
}
151162

152-
/**
153-
* @param Attribute $attribute
154-
* @return $this
155-
*/
156-
private function populateAdditionalDataEavAttribute(Attribute $attribute)
157-
{
158-
$serializedAdditionalData = $attribute->getData('additional_data');
159-
if ($serializedAdditionalData) {
160-
$additionalData = $this->serializer->unserialize($serializedAdditionalData);
161-
if (isset($additionalData) && is_array($additionalData)) {
162-
foreach ($this->eavAttributeAdditionalDataKeys as $key) {
163-
if (isset($additionalData[$key])) {
164-
$attribute->setData($key, $additionalData[$key]);
165-
}
166-
}
167-
}
168-
}
169-
return $this;
170-
}
171-
172163
/**
173164
* @param string $attributeCode swatch_image|image
174165
* @param ModelProduct $configurableProduct
@@ -221,7 +212,7 @@ public function loadFirstVariationWithImage(Product $configurableProduct, array
221212
*/
222213
public function loadVariationByFallback(Product $parentProduct, array $attributes)
223214
{
224-
if (! $this->isProductHasSwatch($parentProduct)) {
215+
if (!$this->isProductHasSwatch($parentProduct)) {
225216
return false;
226217
}
227218

@@ -443,6 +434,7 @@ public function getSwatchesByOptionsId(array $optionIds)
443434
$swatchCollection->addFilterByOptionsIds($swatchOptionIds);
444435

445436
$swatches = [];
437+
$fallbackValues = [];
446438
$currentStoreId = $this->storeManager->getStore()->getId();
447439
foreach ($swatchCollection as $item) {
448440
if ($item['type'] != Swatch::SWATCH_TYPE_TEXTUAL) {
@@ -497,10 +489,14 @@ private function addFallbackOptions(array $fallbackValues, array $swatches)
497489
{
498490
$currentStoreId = $this->storeManager->getStore()->getId();
499491
foreach ($fallbackValues as $optionId => $optionsArray) {
500-
if (isset($optionsArray[$currentStoreId])) {
492+
if (isset($optionsArray[$currentStoreId], $swatches[$optionId]['type'])
493+
&& $swatches[$optionId]['type'] === $optionsArray[$currentStoreId]['type']
494+
) {
501495
$swatches[$optionId] = $optionsArray[$currentStoreId];
502496
} else {
503-
$swatches[$optionId] = $optionsArray[self::DEFAULT_STORE_ID];
497+
if (isset($optionsArray[self::DEFAULT_STORE_ID])) {
498+
$swatches[$optionId] = $optionsArray[self::DEFAULT_STORE_ID];
499+
}
504500
}
505501
}
506502

@@ -527,8 +523,7 @@ public function isProductHasSwatch(Product $product)
527523
*/
528524
public function isSwatchAttribute(Attribute $attribute)
529525
{
530-
$result = $this->isVisualSwatch($attribute) || $this->isTextSwatch($attribute);
531-
return $result;
526+
return $this->swatchTypeChecker->isSwatchAttribute($attribute);
532527
}
533528

534529
/**
@@ -539,10 +534,7 @@ public function isSwatchAttribute(Attribute $attribute)
539534
*/
540535
public function isVisualSwatch(Attribute $attribute)
541536
{
542-
if (!$attribute->hasData(Swatch::SWATCH_INPUT_TYPE_KEY)) {
543-
$this->populateAdditionalDataEavAttribute($attribute);
544-
}
545-
return $attribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) == Swatch::SWATCH_INPUT_TYPE_VISUAL;
537+
return $this->swatchTypeChecker->isVisualSwatch($attribute);
546538
}
547539

548540
/**
@@ -553,10 +545,7 @@ public function isVisualSwatch(Attribute $attribute)
553545
*/
554546
public function isTextSwatch(Attribute $attribute)
555547
{
556-
if (!$attribute->hasData(Swatch::SWATCH_INPUT_TYPE_KEY)) {
557-
$this->populateAdditionalDataEavAttribute($attribute);
558-
}
559-
return $attribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) == Swatch::SWATCH_INPUT_TYPE_TEXT;
548+
return $this->swatchTypeChecker->isTextSwatch($attribute);
560549
}
561550

562551
/**
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Swatches\Model;
8+
9+
use Magento\Eav\Api\Data\AttributeInterface;
10+
use Magento\Framework\Serialize\Serializer\Json;
11+
12+
class SwatchAttributeType
13+
{
14+
15+
/**
16+
* @var Json
17+
*/
18+
private $serializer;
19+
20+
/**
21+
* Data key which should populated to Attribute entity from "additional_data" field
22+
*
23+
* @var array
24+
*/
25+
private $eavAttributeAdditionalDataKeys = [
26+
Swatch::SWATCH_INPUT_TYPE_KEY,
27+
'update_product_preview_image',
28+
'use_product_image_for_swatch'
29+
];
30+
31+
/**
32+
* SwatchAttributeType constructor.
33+
* @param Json $serializer
34+
*/
35+
public function __construct(Json $serializer)
36+
{
37+
$this->serializer = $serializer;
38+
}
39+
40+
/**
41+
* @param AttributeInterface $productAttribute
42+
* @return bool
43+
*/
44+
public function isTextSwatch(AttributeInterface $productAttribute)
45+
{
46+
$this->populateAdditionalDataEavAttribute($productAttribute);
47+
return $productAttribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) === Swatch::SWATCH_INPUT_TYPE_TEXT;
48+
}
49+
50+
/**
51+
* @param AttributeInterface $productAttribute
52+
* @return bool
53+
*/
54+
public function isVisualSwatch(AttributeInterface $productAttribute)
55+
{
56+
$this->populateAdditionalDataEavAttribute($productAttribute);
57+
return $productAttribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) === Swatch::SWATCH_INPUT_TYPE_VISUAL;
58+
}
59+
60+
/**
61+
* @param AttributeInterface $productAttribute
62+
* @return bool
63+
*/
64+
public function isSwatchAttribute(AttributeInterface $productAttribute)
65+
{
66+
return ($this->isTextSwatch($productAttribute) || $this->isVisualSwatch($productAttribute));
67+
}
68+
69+
/**
70+
* @param AttributeInterface $attribute
71+
* @return void
72+
*/
73+
private function populateAdditionalDataEavAttribute(AttributeInterface $attribute)
74+
{
75+
if (!$attribute->hasData(Swatch::SWATCH_INPUT_TYPE_KEY)) {
76+
$serializedAdditionalData = $attribute->getData('additional_data');
77+
if ($serializedAdditionalData) {
78+
$additionalData = $this->serializer->unserialize($serializedAdditionalData);
79+
if ($additionalData !== null && is_array($additionalData)) {
80+
foreach ($this->eavAttributeAdditionalDataKeys as $key) {
81+
if (isset($additionalData[$key])) {
82+
$attribute->setData($key, $additionalData[$key]);
83+
}
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}

app/code/Magento/Swatches/Model/SwatchAttributesProvider.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Swatches\Model;
78

89
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
910
use Magento\Catalog\Model\Product;
1011
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
12+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
13+
use Magento\Framework\App\ObjectManager;
1114

1215
/**
1316
* Provide list of swatch attributes for product.
@@ -30,16 +33,25 @@ class SwatchAttributesProvider
3033
*/
3134
private $attributesPerProduct;
3235

36+
/**
37+
* @var SwatchAttributeType
38+
*/
39+
private $swatchTypeChecker;
40+
3341
/**
3442
* @param Configurable $typeConfigurable
3543
* @param SwatchAttributeCodes $swatchAttributeCodes
44+
* @param SwatchAttributeType|null $swatchTypeChecker
3645
*/
3746
public function __construct(
3847
Configurable $typeConfigurable,
39-
SwatchAttributeCodes $swatchAttributeCodes
48+
SwatchAttributeCodes $swatchAttributeCodes,
49+
SwatchAttributeType $swatchTypeChecker = null
4050
) {
4151
$this->typeConfigurable = $typeConfigurable;
4252
$this->swatchAttributeCodes = $swatchAttributeCodes;
53+
$this->swatchTypeChecker = $swatchTypeChecker
54+
?: ObjectManager::getInstance()->create(SwatchAttributeType::class);
4355
}
4456

4557
/**
@@ -61,8 +73,13 @@ public function provide(Product $product)
6173
$swatchAttributes = [];
6274
foreach ($configurableAttributes as $configurableAttribute) {
6375
if (array_key_exists($configurableAttribute->getAttributeId(), $swatchAttributeCodeMap)) {
64-
$swatchAttributes[$configurableAttribute->getAttributeId()]
65-
= $configurableAttribute->getProductAttribute();
76+
/** @var AbstractAttribute $productAttribute */
77+
$productAttribute = $configurableAttribute->getProductAttribute();
78+
if ($productAttribute !== null
79+
&& $this->swatchTypeChecker->isSwatchAttribute($productAttribute)
80+
) {
81+
$swatchAttributes[$configurableAttribute->getAttributeId()] = $productAttribute;
82+
}
6683
}
6784
}
6885
$this->attributesPerProduct[$product->getId()] = $swatchAttributes;

0 commit comments

Comments
 (0)