Magento Extensions Rating 2024
EXTENSIONS BY CATEGORY
B2B (Business-To-Business)
Blog
Customer
ERP (Enterprise Resource Planning)
Mega Menu
One Step Checkout
Order
POS (Point Of Sale)
Search
Shopping Cart
Sitemap
SEO
Social
Stock & Inventory Management
EXTENSIONS BY DEVELOPER
aheadWorks
Amasty
Boost My Shop
BSS Commerce
Magestore
MageWorx
Mirasvit
Templates Master
Wyomind
XTENTO
Magento 2 Documentation
Magento 2 Documentation
2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
vendor
magento
magento2-base
dev
tests
integration
testsuite
Magento
Swatches
_files
swatch_attribute.php
Go to the documentation of this file.
1
<?php
6
use
Magento\Eav\Api\Data\AttributeOptionInterface
;
7
9
$objectManager
=
\Magento\TestFramework\Helper\Bootstrap::getObjectManager
();
10
11
$data
= [
12
'is_required'
=> 1,
13
'is_visible_on_front'
=> 1,
14
'is_visible_in_advanced_search'
=> 0,
15
'attribute_code'
=>
'color_swatch'
,
16
'backend_type'
=>
''
,
17
'is_searchable'
=> 0,
18
'is_filterable'
=> 0,
19
'is_filterable_in_search'
=> 0,
20
'frontend_label'
=>
'Attribute '
,
21
'entity_type_id'
=> 4
22
];
23
$optionsPerAttribute
= 3;
24
25
$data
[
'frontend_input'
] =
'swatch_visual'
;
26
$data
[
'swatch_input_type'
] =
'visual'
;
27
$data
[
'swatchvisual'
][
'value'
] = array_reduce(
28
range(1,
$optionsPerAttribute
),
29
function
(
$values
,
$index
) use (
$optionsPerAttribute
) {
30
$values
[
'option_'
.
$index
] =
'#'
31
. str_repeat(
32
dechex(255 *
$index
/
$optionsPerAttribute
),
33
3
34
);
35
return
$values
;
36
},
37
[]
38
);
39
$data
[
'optionvisual'
][
'value'
] = array_reduce(
40
range(1,
$optionsPerAttribute
),
41
function
(
$values
,
$index
) use (
$optionsPerAttribute
) {
42
$values
[
'option_'
.
$index
] = [
'option '
.
$index
];
43
return
$values
;
44
},
45
[]
46
);
47
48
$data
[
'options'
][
'option'
] = array_reduce(
49
range(1,
$optionsPerAttribute
),
50
function
(
$values
,
$index
) use (
$optionsPerAttribute
) {
51
$values
[] = [
52
'label'
=>
'option '
.
$index
,
53
'value'
=>
'option_'
.
$index
,
54
];
55
return
$values
;
56
},
57
[]
58
);
59
60
$options
= [];
61
foreach
(
$data
[
'options'
][
'option'
] as
$optionData
) {
62
$options
[] =
$objectManager
->get(AttributeOptionInterface::class)
63
->setLabel(
$optionData
[
'label'
])
64
->setValue(
$optionData
[
'value'
]);
65
}
66
67
$attribute
=
$objectManager
->create(
68
\
Magento
\Catalog\Api\Data\ProductAttributeInterface::class,
69
[
'data'
=>
$data
]
70
);
71
$attribute
->setOptions(
$options
);
72
$attribute
->save();
$options
$options
Definition:
swatch_attribute.php:60
$optionData
$optionData
Definition:
select_attribute.php:21
$values
$values
Definition:
options.phtml:88
$optionsPerAttribute
$optionsPerAttribute
Definition:
swatch_attribute.php:23
Magento\Eav\Api\Data\AttributeOptionInterface
Definition:
AttributeOptionInterface.php:15
$objectManager
$objectManager
Definition:
swatch_attribute.php:9
Magento
$data
$data
Definition:
swatch_attribute.php:11
$index
$index
Definition:
list.phtml:44
$attribute
foreach($data['options']['option'] as $optionData) $attribute
Definition:
swatch_attribute.php:67
Magento\TestFramework\Helper\Bootstrap\getObjectManager
static getObjectManager()
Definition:
Bootstrap.php:125