11
11
use Magento \Catalog \Api \Data \ProductCustomOptionValuesInterfaceFactory ;
12
12
use Magento \Catalog \Api \Data \ProductInterface ;
13
13
use Magento \Catalog \Model \Product ;
14
+ use Magento \Catalog \Model \Product \Option \Type \Date ;
15
+ use Magento \Catalog \Model \Product \Option \Type \DefaultType ;
16
+ use Magento \Catalog \Model \Product \Option \Type \File ;
17
+ use Magento \Catalog \Model \Product \Option \Type \Select ;
18
+ use Magento \Catalog \Model \Product \Option \Type \Text ;
14
19
use Magento \Catalog \Model \ResourceModel \Product \Option \Value \Collection ;
15
20
use Magento \Catalog \Pricing \Price \BasePrice ;
16
21
use Magento \Framework \EntityManager \MetadataPool ;
@@ -98,6 +103,16 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
98
103
*/
99
104
protected $ validatorPool ;
100
105
106
+ /**
107
+ * @var string[]
108
+ */
109
+ private $ optionGroups ;
110
+
111
+ /**
112
+ * @var string[]
113
+ */
114
+ private $ optionTypesToGroups ;
115
+
101
116
/**
102
117
* @var MetadataPool
103
118
*/
@@ -121,6 +136,8 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
121
136
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
122
137
* @param array $data
123
138
* @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
139
+ * @param array $optionGroups
140
+ * @param array $optionTypesToGroups
124
141
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
125
142
*/
126
143
public function __construct (
@@ -135,14 +152,34 @@ public function __construct(
135
152
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
136
153
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
137
154
array $ data = [],
138
- ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null
155
+ ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null ,
156
+ array $ optionGroups = [],
157
+ array $ optionTypesToGroups = []
139
158
) {
140
159
$ this ->productOptionValue = $ productOptionValue ;
141
160
$ this ->optionTypeFactory = $ optionFactory ;
142
- $ this ->validatorPool = $ validatorPool ;
143
161
$ this ->string = $ string ;
162
+ $ this ->validatorPool = $ validatorPool ;
144
163
$ this ->customOptionValuesFactory = $ customOptionValuesFactory ?:
145
164
\Magento \Framework \App \ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
165
+ $ this ->optionGroups = $ optionGroups ?: [
166
+ self ::OPTION_GROUP_DATE => Date::class,
167
+ self ::OPTION_GROUP_FILE => File::class,
168
+ self ::OPTION_GROUP_SELECT => Select::class,
169
+ self ::OPTION_GROUP_TEXT => Text::class,
170
+ ];
171
+ $ this ->optionTypesToGroups = $ optionTypesToGroups ?: [
172
+ self ::OPTION_TYPE_FIELD => self ::OPTION_GROUP_TEXT ,
173
+ self ::OPTION_TYPE_AREA => self ::OPTION_GROUP_TEXT ,
174
+ self ::OPTION_TYPE_FILE => self ::OPTION_GROUP_FILE ,
175
+ self ::OPTION_TYPE_DROP_DOWN => self ::OPTION_GROUP_SELECT ,
176
+ self ::OPTION_TYPE_RADIO => self ::OPTION_GROUP_SELECT ,
177
+ self ::OPTION_TYPE_CHECKBOX => self ::OPTION_GROUP_SELECT ,
178
+ self ::OPTION_TYPE_MULTIPLE => self ::OPTION_GROUP_SELECT ,
179
+ self ::OPTION_TYPE_DATE => self ::OPTION_GROUP_DATE ,
180
+ self ::OPTION_TYPE_DATE_TIME => self ::OPTION_GROUP_DATE ,
181
+ self ::OPTION_TYPE_TIME => self ::OPTION_GROUP_DATE ,
182
+ ];
146
183
147
184
parent ::__construct (
148
185
$ context ,
@@ -306,44 +343,30 @@ public function setProduct(Product $product = null)
306
343
/**
307
344
* Get group name of option by given option type
308
345
*
309
- * @param string $type
346
+ * @param string|null $type
310
347
* @return string
311
348
*/
312
349
public function getGroupByType ($ type = null )
313
350
{
314
351
if ($ type === null ) {
315
352
$ type = $ this ->getType ();
316
353
}
317
- $ optionGroupsToTypes = [
318
- self ::OPTION_TYPE_FIELD => self ::OPTION_GROUP_TEXT ,
319
- self ::OPTION_TYPE_AREA => self ::OPTION_GROUP_TEXT ,
320
- self ::OPTION_TYPE_FILE => self ::OPTION_GROUP_FILE ,
321
- self ::OPTION_TYPE_DROP_DOWN => self ::OPTION_GROUP_SELECT ,
322
- self ::OPTION_TYPE_RADIO => self ::OPTION_GROUP_SELECT ,
323
- self ::OPTION_TYPE_CHECKBOX => self ::OPTION_GROUP_SELECT ,
324
- self ::OPTION_TYPE_MULTIPLE => self ::OPTION_GROUP_SELECT ,
325
- self ::OPTION_TYPE_DATE => self ::OPTION_GROUP_DATE ,
326
- self ::OPTION_TYPE_DATE_TIME => self ::OPTION_GROUP_DATE ,
327
- self ::OPTION_TYPE_TIME => self ::OPTION_GROUP_DATE ,
328
- ];
329
354
330
- return $ optionGroupsToTypes [$ type ] ?? '' ;
355
+ return $ this -> optionTypesToGroups [$ type ] ?? '' ;
331
356
}
332
357
333
358
/**
334
359
* Group model factory
335
360
*
336
361
* @param string $type Option type
337
- * @return \Magento\Catalog\Model\Product\Option\Type\ DefaultType
362
+ * @return DefaultType
338
363
* @throws LocalizedException
339
364
*/
340
365
public function groupFactory ($ type )
341
366
{
342
367
$ group = $ this ->getGroupByType ($ type );
343
- if (!empty ($ group )) {
344
- return $ this ->optionTypeFactory ->create (
345
- 'Magento\Catalog\Model\Product\Option\Type \\' . $ this ->string ->upperCaseWords ($ group )
346
- );
368
+ if (!empty ($ group ) && isset ($ this ->optionGroups [$ group ])) {
369
+ return $ this ->optionTypeFactory ->create ($ this ->optionGroups [$ group ]);
347
370
}
348
371
throw new LocalizedException (__ ('The option type to get group instance is incorrect. ' ));
349
372
}
0 commit comments