This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
UpgradeData.php
281 lines (257 loc) · 10.6 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
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
namespace Facebook\BusinessExtension\Setup;
use Exception;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Facebook\BusinessExtension\Logger\Logger;
use Facebook\BusinessExtension\Model\Config\ProductAttributes;
use Facebook\BusinessExtension\Helper\FBEHelper;
use Magento\Framework\Setup\UpgradeDataInterface;
use \Magento\Catalog\Model\Product;
class UpgradeData implements UpgradeDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Category setup factory
*
* @var CategorySetupFactory
*/
private $categorySetupFactory;
/**
* Attribute set factory
*
* @var SetFactory
*/
private $attributeSetFactory;
/**
* contains fb attribute config
*
* @var ProductAttributes
*/
private $attributeConfig;
/**
* @var FBEHelper
*/
private $helper;
/**
* @var Logger
*/
private $logger;
/**
* Constructor
*
* @param EavSetupFactory $eavSetupFactory
* @param CategorySetupFactory $categorySetupFactory
* @param SetFactory $attributeSetFactory
* @param ProductAttributes $attributeConfig
* @param FBEHelper $helper
* @param Logger $logger
*/
public function __construct(
EavSetupFactory $eavSetupFactory,
CategorySetupFactory $categorySetupFactory,
SetFactory $attributeSetFactory,
ProductAttributes $attributeConfig,
FBEHelper $helper,
Logger $logger
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->categorySetupFactory = $categorySetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->attributeConfig = $attributeConfig;
$this->helper = $helper;
$this->logger = $logger;
}
/**
* Retrieve the min Attribute Group Sort order, and plus one, we want to put fb attribute group the second place.
* method stolen from Magento\Eav\Setup\EavSetup::getAttributeGroupSortOrder()
*
* @param EavSetup $eavSetup
* @param int|string $entityTypeId
* @param int|string $setId
* @return int
* @throws LocalizedException
*/
private function getMinAttributeGroupSortOrder(EavSetup $eavSetup, $entityTypeId, $setId)
{
$bind = ['attribute_set_id' => $eavSetup->getAttributeSetId($entityTypeId, $setId)];
$select = $eavSetup->getSetup()->getConnection()->select()->from(
$eavSetup->getSetup()->getTable('eav_attribute_group'),
'MIN(sort_order)'
)->where(
'attribute_set_id = :attribute_set_id'
);
$sortOrder = $eavSetup->getSetup()->getConnection()->fetchOne($select, $bind) + 1;
return $sortOrder;
}
/**
* {@inheritdoc}
* @throws LocalizedException|\Zend_Validate_Exception
*/
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$this->helper->log("getVersion" . $context->getVersion());
// introducing google product category in 1.2.2
if (version_compare($context->getVersion(), '1.2.2') < 0) {
$attrCode = 'google_product_category';
if (!$eavSetup->getAttributeId(Product::ENTITY, $attrCode)) {
try {
$eavSetup->addAttribute(Product::ENTITY, $attrCode, [
'group' => 'General',
'type' => 'varchar',
'label' => 'Google Product Category',
'input' => 'select',
'source' => 'Facebook\BusinessExtension\Model\Config\Source\Product\GoogleProductCategory',
'required' => false,
'sort_order' => 10,
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'visible' => true,
'is_html_allowed_on_front' => false,
'visible_on_front' => false
]);
} catch (Exception $e) {
$this->logger->critical($e);
}
}
}
if (version_compare($context->getVersion(), '1.2.0') < 0) {
$attributeConfig = $this->attributeConfig->getAttributesConfig();
foreach ($attributeConfig as $attrCode => $config) {
// verify if already installed before
if (!$eavSetup->getAttributeId(Product::ENTITY, $attrCode)) {
//Create the attribute
$this->helper->log($attrCode . " not exist before, process it");
// attribute does not exist
// add a new attribute
// and assign it to the "FacebookAttributeSet" attribute set
$eavSetup->addAttribute(
Product::ENTITY,
$attrCode,
[
'type' => $config['type'],
'label' => $config['label'],
'input' => $config['input'],
'source' => $config['source'],
'note' => $config['note'],
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'required' => false,
'user_defined' => true,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'visible' => true,
'is_html_allowed_on_front' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'attribute_set' => 'FacebookAttributeSet'
]
);
} else {
$this->helper->log($attrCode . " already installed, skip");
}
}
/**
* Create a custom attribute group in all attribute sets
* And, Add attribute to that attribute group for all attribute sets
*/
$attributeGroupName = $this->attributeConfig->getAttributeGroupName();
// get the catalog_product entity type id/code
$entityTypeId = $categorySetup->getEntityTypeId(Product::ENTITY);
// get the attribute set ids of all the attribute sets present in your Magento store
$attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);
foreach ($attributeSetIds as $attributeSetId) {
$attr_group_sort_order = $this->getMinAttributeGroupSortOrder(
$eavSetup,
$entityTypeId,
$attributeSetId
);
$eavSetup->addAttributeGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupName,
$attr_group_sort_order // sort order
);
foreach ($attributeConfig as $attributeCode => $config) {
// get the newly create attribute group id
$attributeGroupId = $eavSetup->getAttributeGroupId(
$entityTypeId,
$attributeSetId,
$attributeGroupName
);
// add attribute to group
$categorySetup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupName, // attribute group
$attributeCode,
$config['sort_order']
);
}
}
}
// change attribute code facebook_software_system_requirements -> facebook_system_requirements
// due to 30 length limit
if (version_compare($context->getVersion(), '1.2.5') < 0) {
$oldAttrCode = 'facebook_software_system_requirements';
$newAttrCode = 'facebook_system_requirements';
$oldAttrId = $eavSetup->getAttributeId(Product::ENTITY, $oldAttrCode);
if ($oldAttrId) {
$eavSetup->updateAttribute(
\Magento\Catalog\Model\Product::ENTITY,
$oldAttrId,
[
'attribute_code' => $newAttrCode,
]
);
}
}
// user can config if they want to sync a category or not
if (version_compare($context->getVersion(), '1.4.2') < 0) {
$attrCode = "sync_to_facebook_catalog";
$eavSetup->removeAttribute(Product::ENTITY, $attrCode);
if (!$eavSetup->getAttributeId(Product::ENTITY, $attrCode)) {
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
$attrCode,
[
'type' => 'int',
'input' => 'boolean',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'visible' => true,
'default' => "1",
'required' => false,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'Display Settings',
]
);
}
}
$setup->endSetup();
}
}