-
Notifications
You must be signed in to change notification settings - Fork 38
/
UpgradeData.php
332 lines (302 loc) · 10.7 KB
/
UpgradeData.php
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?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/
*/
namespace Magenerds\BasePrice\Setup;
use Magenerds\BasePrice\Helper\Data;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
use Magento\Catalog\Model\Product;
use Magento\Config\Model\ResourceModel\Config as ResourceConfig;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Eav\Setup\EavSetupFactory;
class UpgradeData implements UpgradeDataInterface
{
const UNIT_ATTRIBUTE_CODES = ['baseprice_product_unit', 'baseprice_reference_unit'];
const OPTIONS_SORT_ORDER = ['kg', 'g', 'mg', 'l', 'ml', 'km', 'm', 'cm', 'mm', 'qm', 'cbm', 'pc'];
/**
* Conversion rates from InstallData.
* Used to add all conversion rates when first installing the module.
* @see \Magenerds\BasePrice\Setup\InstallData::setSystemConfiguration()
*/
const DEFAULT_DATA_TEMPLATE = [
'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.001'
]
],
'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.01'
],
[
'reference_unit' => 'mm',
'conversion_rate' => '0.001'
]
],
'cm' => [
[
'reference_unit' => 'm',
'conversion_rate' => '100'
],
[
'reference_unit' => 'mm',
'conversion_rate' => '0.001'
]
],
'mm' => [
[
'reference_unit' => 'm',
'conversion_rate' => '1000'
],
[
'reference_unit' => 'cm',
'conversion_rate' => '10'
]
],
];
/**
* @var EavSetupFactory
*/
public $eavSetupFactory;
/**
* @var ProductAttributeOptionManagementInterface
*/
private $productAttributeOptionManagementInterface;
/**
* @var ResourceConfig
*/
private $configResource;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var EavConfig
*/
private $eavConfig;
/**
* @var AttributeRepositoryInterface
*/
private $attributeRepository;
public function __construct(
EavSetupFactory $eavSetupFactory,
ProductAttributeOptionManagementInterface $productAttributeOptionManagementInterface,
ResourceConfig $configResource,
SerializerInterface $serializer,
ScopeConfigInterface $scopeConfig,
EavConfig $eavConfig,
AttributeRepositoryInterface $attributeRepository
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->productAttributeOptionManagementInterface = $productAttributeOptionManagementInterface;
$this->configResource = $configResource;
$this->serializer = $serializer;
$this->scopeConfig = $scopeConfig;
$this->eavConfig = $eavConfig;
$this->attributeRepository = $attributeRepository;
}
/**
* Upgrades data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.1.0', '<')) {
$options = ['km', 'qm', 'cbm', 'pc'];
// holds the conversion rates
$dataTemplate = [
'km' => [
[
'reference_unit' => 'm',
'conversion_rate' => '0.001',
],
[
'reference_unit' => 'cm',
'conversion_rate' => '0.00001',
],
[
'reference_unit' => 'mm',
'conversion_rate' => '0.000001',
],
],
'm' => [
[
'reference_unit' => 'km',
'conversion_rate' => '1000',
],
],
'cm' => [
[
'reference_unit' => 'km',
'conversion_rate' => '100000',
],
],
'mm' => [
[
'reference_unit' => 'km',
'conversion_rate' => '1000000',
],
],
];
$this->addOption($eavSetup, $options);
$this->eavConfig->clear();
$this->addToSystemConfiguration($dataTemplate);
$this->sortOptions($setup);
}
$setup->endSetup();
}
/**
* @param $eavSetup
* @param array $options
*/
protected function addOption($eavSetup, array $options)
{
foreach (self::UNIT_ATTRIBUTE_CODES as $attributeCode) {
$attributeId = $eavSetup->getAttribute(Product::ENTITY, $attributeCode, 'attribute_id');
$eavSetup->addAttributeOption([
'attribute_id' => $attributeId,
'values' => $options,
]);
}
}
/**
* Sets conversations for added default units
*
* @param array $dataTemplate
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
protected function addToSystemConfiguration(array $dataTemplate)
{
// 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();
}
//Config value is not available during first installation -> maybe config cache
if (is_null($this->scopeConfig->getValue(Data::CONVERSION_CONFIG_PATH))) {
$data = [];
$dataTemplate = array_merge_recursive(self::DEFAULT_DATA_TEMPLATE, $dataTemplate);
} else {
$data = $this->serializer->unserialize($this->scopeConfig->getValue(Data::CONVERSION_CONFIG_PATH));
}
// iterate over attribute options in order to replace labels with option ids
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'],
];
}
}
//save system configuration
$this->configResource->saveConfig(
Data::CONVERSION_CONFIG_PATH,
$this->serializer->serialize($data),
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
0
);
}
/**
* Sort options of the attribute(s) based on self::OPTIONS_SORT_ORDER
*
* @param ModuleDataSetupInterface $setup
* @param array $attributes
*
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function sortOptions(ModuleDataSetupInterface $setup, array $attributes = [])
{
if (empty($attributes)) {
$attributes = self::UNIT_ATTRIBUTE_CODES;
}
//iterate through given attribute_codes
foreach ($attributes as $attributeCode) {
$attribute = $this->attributeRepository->get(Product::ENTITY, $attributeCode);
//iterate through options
foreach ($attribute->getOptions() as $option) {
//get sort order based on options label
$sortOrder = array_search($option->getLabel(), self::OPTIONS_SORT_ORDER);
$table = $setup->getTable('eav_attribute_option');
$optionId = $attribute->getSource()->getOptionId($option->getValue());
if ($optionId != '' && ! is_null($optionId)) {
$setup->getConnection()->update($table, ['sort_order' => $sortOrder], 'option_id=' . $optionId);
}
}
}
}
}