3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Swatches \Helper ;
7
8
8
9
use Magento \Catalog \Api \Data \ProductInterface as Product ;
19
20
use Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory as SwatchCollectionFactory ;
20
21
use Magento \Swatches \Model \Swatch ;
21
22
use Magento \Swatches \Model \SwatchAttributesProvider ;
23
+ use Magento \Swatches \Model \SwatchAttributeType ;
22
24
23
25
/**
24
26
* Class Helper Data
@@ -94,6 +96,11 @@ class Data
94
96
*/
95
97
private $ serializer ;
96
98
99
+ /**
100
+ * @var SwatchAttributeType
101
+ */
102
+ private $ swatchTypeChecker ;
103
+
97
104
/**
98
105
* @param CollectionFactory $productCollectionFactory
99
106
* @param ProductRepositoryInterface $productRepository
@@ -102,6 +109,7 @@ class Data
102
109
* @param Image $imageHelper
103
110
* @param Json|null $serializer
104
111
* @param SwatchAttributesProvider $swatchAttributesProvider
112
+ * @param SwatchAttributeType|null $swatchTypeChecker
105
113
*/
106
114
public function __construct (
107
115
CollectionFactory $ productCollectionFactory ,
@@ -110,16 +118,19 @@ public function __construct(
110
118
SwatchCollectionFactory $ swatchCollectionFactory ,
111
119
Image $ imageHelper ,
112
120
Json $ serializer = null ,
113
- SwatchAttributesProvider $ swatchAttributesProvider = null
121
+ SwatchAttributesProvider $ swatchAttributesProvider = null ,
122
+ SwatchAttributeType $ swatchTypeChecker = null
114
123
) {
115
- $ this ->productCollectionFactory = $ productCollectionFactory ;
124
+ $ this ->productCollectionFactory = $ productCollectionFactory ;
116
125
$ this ->productRepository = $ productRepository ;
117
126
$ this ->storeManager = $ storeManager ;
118
127
$ this ->swatchCollectionFactory = $ swatchCollectionFactory ;
119
128
$ this ->imageHelper = $ imageHelper ;
120
129
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->create (Json::class);
121
130
$ this ->swatchAttributesProvider = $ swatchAttributesProvider
122
131
?: ObjectManager::getInstance ()->get (SwatchAttributesProvider::class);
132
+ $ this ->swatchTypeChecker = $ swatchTypeChecker
133
+ ?: ObjectManager::getInstance ()->create (SwatchAttributeType::class);
123
134
}
124
135
125
136
/**
@@ -129,7 +140,7 @@ public function __construct(
129
140
public function assembleAdditionalDataEavAttribute (Attribute $ attribute )
130
141
{
131
142
$ initialAdditionalData = [];
132
- $ additionalData = (string ) $ attribute ->getData ('additional_data ' );
143
+ $ additionalData = (string )$ attribute ->getData ('additional_data ' );
133
144
if (!empty ($ additionalData )) {
134
145
$ additionalData = $ this ->serializer ->unserialize ($ additionalData );
135
146
if (is_array ($ additionalData )) {
@@ -149,26 +160,6 @@ public function assembleAdditionalDataEavAttribute(Attribute $attribute)
149
160
return $ this ;
150
161
}
151
162
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
-
172
163
/**
173
164
* @param string $attributeCode swatch_image|image
174
165
* @param ModelProduct $configurableProduct
@@ -221,7 +212,7 @@ public function loadFirstVariationWithImage(Product $configurableProduct, array
221
212
*/
222
213
public function loadVariationByFallback (Product $ parentProduct , array $ attributes )
223
214
{
224
- if (! $ this ->isProductHasSwatch ($ parentProduct )) {
215
+ if (!$ this ->isProductHasSwatch ($ parentProduct )) {
225
216
return false ;
226
217
}
227
218
@@ -443,6 +434,7 @@ public function getSwatchesByOptionsId(array $optionIds)
443
434
$ swatchCollection ->addFilterByOptionsIds ($ swatchOptionIds );
444
435
445
436
$ swatches = [];
437
+ $ fallbackValues = [];
446
438
$ currentStoreId = $ this ->storeManager ->getStore ()->getId ();
447
439
foreach ($ swatchCollection as $ item ) {
448
440
if ($ item ['type ' ] != Swatch::SWATCH_TYPE_TEXTUAL ) {
@@ -497,10 +489,14 @@ private function addFallbackOptions(array $fallbackValues, array $swatches)
497
489
{
498
490
$ currentStoreId = $ this ->storeManager ->getStore ()->getId ();
499
491
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
+ ) {
501
495
$ swatches [$ optionId ] = $ optionsArray [$ currentStoreId ];
502
496
} 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
+ }
504
500
}
505
501
}
506
502
@@ -527,8 +523,7 @@ public function isProductHasSwatch(Product $product)
527
523
*/
528
524
public function isSwatchAttribute (Attribute $ attribute )
529
525
{
530
- $ result = $ this ->isVisualSwatch ($ attribute ) || $ this ->isTextSwatch ($ attribute );
531
- return $ result ;
526
+ return $ this ->swatchTypeChecker ->isSwatchAttribute ($ attribute );
532
527
}
533
528
534
529
/**
@@ -539,10 +534,7 @@ public function isSwatchAttribute(Attribute $attribute)
539
534
*/
540
535
public function isVisualSwatch (Attribute $ attribute )
541
536
{
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 );
546
538
}
547
539
548
540
/**
@@ -553,10 +545,7 @@ public function isVisualSwatch(Attribute $attribute)
553
545
*/
554
546
public function isTextSwatch (Attribute $ attribute )
555
547
{
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 );
560
549
}
561
550
562
551
/**
0 commit comments