7
7
8
8
namespace Magento \SwatchesGraphQl \Model \Resolver \Product \Options \DataProvider ;
9
9
10
- use Magento \Catalog \Api \Data \ProductInterface ;
11
- use Magento \Catalog \Model \Product \Image \UrlBuilder ;
12
- use Magento \Framework \Exception \LocalizedException ;
13
- use Magento \Framework \Exception \NoSuchEntityException ;
14
- use Magento \Framework \Exception \RuntimeException ;
15
- use Magento \Framework \GraphQl \Query \EnumLookup ;
16
10
use Magento \Swatches \Helper \Data as SwatchData ;
17
11
use Magento \Swatches \Helper \Media as SwatchesMedia ;
18
12
use Magento \Swatches \Model \Swatch ;
19
13
20
14
/**
21
- * Swatch data provider
15
+ * Data provider for options swatches.
22
16
*/
23
17
class SwatchDataProvider
24
18
{
@@ -32,219 +26,41 @@ class SwatchDataProvider
32
26
*/
33
27
private $ swatchMediaHelper ;
34
28
35
- /**
36
- * @var UrlBuilder
37
- */
38
- private $ imageUrlBuilder ;
39
-
40
- /**
41
- * @var EnumLookup
42
- */
43
- private $ enumLookup ;
44
-
45
29
/**
46
30
* SwatchDataProvider constructor.
47
31
*
48
32
* @param SwatchData $swatchHelper
49
33
* @param SwatchesMedia $swatchMediaHelper
50
- * @param UrlBuilder $imageUrlBuilder
51
- * @param EnumLookup $enumLookup
52
34
*/
53
35
public function __construct (
54
36
SwatchData $ swatchHelper ,
55
- SwatchesMedia $ swatchMediaHelper ,
56
- UrlBuilder $ imageUrlBuilder ,
57
- EnumLookup $ enumLookup
37
+ SwatchesMedia $ swatchMediaHelper
58
38
) {
59
39
$ this ->swatchHelper = $ swatchHelper ;
60
40
$ this ->swatchMediaHelper = $ swatchMediaHelper ;
61
- $ this ->imageUrlBuilder = $ imageUrlBuilder ;
62
- $ this ->enumLookup = $ enumLookup ;
63
41
}
64
42
65
43
/**
66
- * Get swatch data
44
+ * Returns swatch data by option ID.
67
45
*
68
46
* @param string $optionId
69
- * @param ProductInterface $product
70
- *
71
- * @return array
72
- *
73
- * @throws LocalizedException
74
- * @throws NoSuchEntityException
75
- * @throws \LogicException
47
+ * @return array|null
76
48
*/
77
- public function getData (string $ optionId, ProductInterface $ product ): array
49
+ public function getData (string $ optionId ): ? array
78
50
{
79
51
$ swatches = $ this ->swatchHelper ->getSwatchesByOptionsId ([$ optionId ]);
80
- if (!isset ($ swatches [$ optionId ], $ swatches [ $ optionId ] ['type ' ], $ swatches [$ optionId ]['value ' ])) {
52
+ if (!isset ($ swatches [$ optionId ]['type ' ], $ swatches [$ optionId ]['value ' ])) {
81
53
return null ;
82
54
}
83
-
84
55
$ type = (int )$ swatches [$ optionId ]['type ' ];
85
56
$ value = $ swatches [$ optionId ]['value ' ];
86
- $ thumbnail = null ;
87
-
88
- // change value & thumbnail if type is 'visual'
57
+ $ data = ['value ' => $ value , 'type ' => $ type ];
89
58
if ($ type === Swatch::SWATCH_TYPE_VISUAL_IMAGE ) {
90
- $ thumbnail = $ this ->swatchMediaHelper ->getSwatchAttributeImage (Swatch::SWATCH_THUMBNAIL_NAME , $ value );
91
- $ value = $ this ->swatchMediaHelper ->getSwatchAttributeImage (Swatch::SWATCH_IMAGE_NAME , $ value );
59
+ $ data ['thumbnail ' ] = $ this ->swatchMediaHelper ->getSwatchAttributeImage (
60
+ Swatch::SWATCH_THUMBNAIL_NAME ,
61
+ $ value
62
+ );
92
63
}
93
-
94
- $ attributeData = $ this ->getSwatchAttributeDataByOptionId ($ product , $ optionId );
95
- // check if swatch value should be getting from related product image
96
- if (!$ this ->isUseProductImageForSwatch ($ attributeData )) {
97
- return $ this ->getResultArray ($ value , $ type , $ thumbnail );
98
- }
99
-
100
- // get product with existing image
101
- $ variationProduct = $ this ->getVariationProduct ($ attributeData , $ optionId , $ product );
102
- if (null === $ variationProduct ) {
103
- return $ this ->getResultArray ($ value , $ type , $ thumbnail );
104
- }
105
-
106
- // set 'visual' type, because the product image is using as swatch value
107
- $ type = Swatch::SWATCH_TYPE_VISUAL_IMAGE ;
108
-
109
- // get image from child product
110
- $ productImage = $ this ->getSwatchProductImage ($ variationProduct , Swatch::SWATCH_IMAGE_NAME );
111
- if (null !== $ productImage ) {
112
- $ value = $ productImage ;
113
- }
114
-
115
- // get thumbnail from child product
116
- $ productThumbnail = $ this ->getSwatchProductImage ($ variationProduct , Swatch::SWATCH_THUMBNAIL_NAME );
117
- if (null !== $ productThumbnail ) {
118
- $ thumbnail = $ productThumbnail ;
119
- }
120
-
121
- return $ this ->getResultArray ($ value , $ type , $ thumbnail );
122
- }
123
-
124
- /**
125
- * Get result array
126
- *
127
- * @param string $value
128
- * @param int $type
129
- * @param null|string $thumbnail
130
- *
131
- * @return array
132
- *
133
- * @throws RuntimeException
134
- */
135
- private function getResultArray (string $ value , int $ type , ?string $ thumbnail )
136
- {
137
- return [
138
- 'value ' => $ value ,
139
- 'type ' => $ this ->enumLookup ->getEnumValueFromField ('SwatchTypeEnum ' , (string )$ type ),
140
- 'thumbnail ' => $ thumbnail
141
- ];
142
- }
143
-
144
- /**
145
- * Is swatch images should be getting from related simple products
146
- *
147
- * @param array $attributeData
148
- *
149
- * @return bool
150
- */
151
- private function isUseProductImageForSwatch (array $ attributeData ) : bool
152
- {
153
- return isset ($ attributeData ['use_product_image_for_swatch ' ]) && $ attributeData ['use_product_image_for_swatch ' ];
154
- }
155
-
156
- /**
157
- * Get simple product with first variation swatch image or image
158
- *
159
- * @param array $attributeData
160
- * @param string $optionId
161
- * @param ProductInterface $product
162
- *
163
- * @return ProductInterface|null
164
- */
165
- private function getVariationProduct (array $ attributeData , string $ optionId , ProductInterface $ product ) : ?ProductInterface
166
- {
167
- $ attributeCode = $ attributeData ['attribute_code ' ];
168
- $ requiredAttributes = [
169
- $ attributeCode => $ optionId
170
- ];
171
-
172
- $ variationProduct = $ this ->swatchHelper ->loadFirstVariationWithSwatchImage ($ product , $ requiredAttributes );
173
- if ($ variationProduct instanceof ProductInterface) {
174
- return $ variationProduct ;
175
- }
176
-
177
- $ variationProduct = $ this ->swatchHelper ->loadFirstVariationWithImage ($ product , $ requiredAttributes );
178
- if ($ variationProduct instanceof ProductInterface) {
179
- return $ variationProduct ;
180
- }
181
-
182
- return null ;
183
- }
184
-
185
- /**
186
- * Get swatch product image
187
- *
188
- * @param ProductInterface $product
189
- * @param string $imageType
190
- *
191
- * @return string|null
192
- */
193
- private function getSwatchProductImage (ProductInterface $ product , $ imageType ) : ?string
194
- {
195
- if ($ this ->isProductHasImage ($ product , Swatch::SWATCH_IMAGE_NAME )) {
196
- $ swatchImageId = $ imageType ;
197
- $ imageAttributes = ['type ' => Swatch::SWATCH_IMAGE_NAME ];
198
- } elseif ($ this ->isProductHasImage ($ product , 'image ' )) {
199
- $ swatchImageId = $ imageType == Swatch::SWATCH_IMAGE_NAME ? 'swatch_image_base ' : 'swatch_thumb_base ' ;
200
- $ imageAttributes = ['type ' => 'image ' ];
201
- }
202
-
203
- if (empty ($ swatchImageId ) || empty ($ imageAttributes ['type ' ])) {
204
- return null ;
205
- }
206
-
207
- return $ this ->imageUrlBuilder ->getUrl ($ product ->getData ($ imageAttributes ['type ' ]), $ swatchImageId );
208
- }
209
-
210
- /**
211
- * Is product has image
212
- *
213
- * @param ProductInterface $product
214
- * @param string $imageType
215
- *
216
- * @return bool
217
- */
218
- private function isProductHasImage (ProductInterface $ product , string $ imageType ) : bool
219
- {
220
- return $ product ->getData ($ imageType ) !== null && $ product ->getData ($ imageType ) != SwatchData::EMPTY_IMAGE_VALUE ;
221
- }
222
-
223
- /**
224
- * Get swatch attribute data by option id
225
- *
226
- * @param ProductInterface $product
227
- * @param string $optionId
228
- *
229
- * @return array
230
- *
231
- * @throws LocalizedException
232
- * @throws \LogicException
233
- * @throws NoSuchEntityException
234
- */
235
- private function getSwatchAttributeDataByOptionId (ProductInterface $ product , string $ optionId ) : array
236
- {
237
- $ attributesData = $ this ->swatchHelper ->getSwatchAttributesAsArray ($ product );
238
- foreach ($ attributesData as $ attributeData ) {
239
- if (!isset ($ attributeData ['options ' ]) || !is_array ($ attributeData ['options ' ])) {
240
- continue ;
241
- }
242
-
243
- if (array_key_exists ($ optionId , $ attributeData ['options ' ])) {
244
- return $ attributeData ;
245
- }
246
- }
247
-
248
- throw new LocalizedException (__ (sprintf ('Cannot find the attribute with option id "%1". ' , $ optionId )));
64
+ return $ data ;
249
65
}
250
66
}
0 commit comments