-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathInstallData.php
More file actions
289 lines (267 loc) · 9.75 KB
/
InstallData.php
File metadata and controls
289 lines (267 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*/
/**
* @category Magenerds
* @package Magenerds_BasePrice
* @subpackage Setup
* @copyright Copyright (c) 2017 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Florian Sydekum <f.sydekum@techdivision.com>
* @author Jürgen "Atlan" Schuch <juergen@atmage.de>
*/
namespace Magenerds\BasePrice\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;
/**
* Class InstallData
* @package Magenerds\BasePrice\Setup
*/
class InstallData implements InstallDataInterface
{
/**
* @var \Magento\Eav\Setup\EavSetupFactory
*/
protected $_eavSetupFactory;
/**
* @var \Magento\Catalog\Api\ProductAttributeOptionManagementInterface
*/
protected $_productAttributeOptionManagementInterface;
/**
* @var \Magento\Config\Model\ResourceModel\Config
*/
protected $_configResource;
/**
* @var \Magento\Eav\Model\Config
*/
protected $_eavConfig;
/**
* Constructor
*
* @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
* @param \Magento\Catalog\Api\ProductAttributeOptionManagementInterface $productAttributeOptionManagementInterface
* @param \Magento\Config\Model\ResourceModel\Config $configResource
* @param \Magento\Eav\Model\Config $eavConfig
*/
public function __construct(
\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
\Magento\Catalog\Api\ProductAttributeOptionManagementInterface $productAttributeOptionManagementInterface,
\Magento\Config\Model\ResourceModel\Config $configResource,
\Magento\Eav\Model\Config $eavConfig
){
$this->_eavSetupFactory = $eavSetupFactory;
$this->_productAttributeOptionManagementInterface = $productAttributeOptionManagementInterface;
$this->_configResource = $configResource;
$this->_eavConfig = $eavConfig;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var $eavSetup EavSetup */
$eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'baseprice_product_amount',
[
'type' => 'decimal',
'label' => 'Product Amount',
'input' => 'text',
'required' => false,
'sort_order' => 1,
'visible' => true,
'note' => 'Leave empty to disable baseprice for this product',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'group' => 'Base Price',
'used_in_product_listing' => true,
'visible_on_front' => false
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'baseprice_product_unit',
[
'type' => 'varchar',
'label' => 'Product unit',
'input' => 'select',
'required' => false,
'sort_order' => 2,
'visible' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'group' => 'Base Price',
'used_in_product_listing' => true,
'visible_on_front' => false
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'baseprice_reference_amount',
[
'type' => 'decimal',
'label' => 'Reference Amount',
'input' => 'text',
'required' => false,
'sort_order' => 3,
'visible' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'group' => 'Base Price',
'used_in_product_listing' => true,
'visible_on_front' => false
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'baseprice_reference_unit',
[
'type' => 'varchar',
'label' => 'Reference unit',
'input' => 'select',
'required' => false,
'sort_order' => 4,
'visible' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'group' => 'Base Price',
'used_in_product_listing' => true,
'visible_on_front' => false
]
);
foreach (['baseprice_product_unit', 'baseprice_reference_unit'] as $attributeCode) {
$attributeId = $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, 'attribute_id');
$eavSetup->addAttributeOption([
'attribute_id' => $attributeId,
'values' => ['kg', 'g', 'mg', 'l', 'ml', 'm', 'cm', 'mm']
]);
}
// clean cache so that newly created attributes will be loaded from database
$this->_eavConfig->clear();
$this->_setSystemConfiguration();
}
/**
* Sets up the conversations for default units
*/
protected function _setSystemConfiguration()
{
// holds the conversion rates
$dataTemplate = [
'kg' => [
[
'reference_unit' => 'g',
'conversion_rate' => '0.001'
],
[
'reference_unit' => 'mg',
'conversion_rate' => '0.000001'
]
],
'g' => [
[
'reference_unit' => 'kg',
'conversion_rate' => '1000'
],
[
'reference_unit' => 'mg',
'conversion_rate' => '0.000001'
]
],
'mg' => [
[
'reference_unit' => 'kg',
'conversion_rate' => '1000000'
],
[
'reference_unit' => 'g',
'conversion_rate' => '1000'
]
],
'l' => [
[
'reference_unit' => 'ml',
'conversion_rate' => '0.001'
]
],
'ml' => [
[
'reference_unit' => 'l',
'conversion_rate' => '1000'
]
],
'm' => [
[
'reference_unit' => 'cm',
'conversion_rate' => '0.001'
],
[
'reference_unit' => 'mm',
'conversion_rate' => '0.000001'
]
],
'cm' => [
[
'reference_unit' => 'm',
'conversion_rate' => '1000'
],
[
'reference_unit' => 'mm',
'conversion_rate' => '0.001'
]
],
'mm' => [
[
'reference_unit' => 'm',
'conversion_rate' => '1000000'
],
[
'reference_unit' => 'cm',
'conversion_rate' => '1000'
]
],
];
// get all attribute options for product unit
$productUnitOptions = [];
foreach ($this->_productAttributeOptionManagementInterface->getItems('baseprice_product_unit') as $option) {
$productUnitOptions[$option->getLabel()] = $option->getValue();
}
// get all attribute options for reference unit
$referenceUnitOptions = [];
foreach ($this->_productAttributeOptionManagementInterface->getItems('baseprice_reference_unit') as $option) {
$referenceUnitOptions[$option->getLabel()] = $option->getValue();
}
// iterate over attribute options in order to replace labels with option ids
$data = [];
foreach ($dataTemplate as $unit => $unitData) {
foreach ($unitData as $key => $unitDataEntry) {
$data[] = [
'product_unit' => $productUnitOptions[$unit],
'reference_unit' => $referenceUnitOptions[$unitDataEntry['reference_unit']],
'conversion_rate' => $unitDataEntry['conversion_rate']
];
}
}
$objectManager = ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$version = $productMetadata->getVersion();
if($version < 2.2) {
$serializedData = serialize($data);
} else {
$serializedData = $objectManager->get(Json::class)->serialize($data);
}
// save system configuration
$this->_configResource->saveConfig(
\Magenerds\BasePrice\Helper\Data::CONVERSION_CONFIG_PATH,
$serializedData,
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
0
);
}
}