/
InstallFreightAttributes.php
277 lines (261 loc) · 10.5 KB
/
InstallFreightAttributes.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
<?php
/*
* ShipperHQ
*
* @category ShipperHQ
* @package ShipperHQ_Shipper
* @copyright Copyright (c) 2022 Zowta LTD and Zowta LLC (http://www.ShipperHQ.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author ShipperHQ Team sales@shipperhq.com
*/
declare(strict_types=1);
namespace ShipperHQ\Shipper\Setup\Patch\Data;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
class InstallFreightAttributes implements DataPatchInterface, PatchRevertableInterface
{
/**
* @var ModuleDataSetupInterface $moduleDataSetup
*/
private $moduleDataSetup;
/**
* Category setup factory
* @var CategorySetupFactory
*/
private $categorySetupFactory;
/**
* @var AttributeCollectionFactory
*/
private $attributeCollectionFactory;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CategorySetupFactory $categorySetupFactory,
AttributeCollectionFactory $attributeCollectionFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->categorySetupFactory = $categorySetupFactory;
$this->attributeCollectionFactory = $attributeCollectionFactory
?: ObjectManager::getInstance()->get(AttributeCollectionFactory::class);
}
/**
* @inheritdoc
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getVersion()
{
return '1.0.12';
}
/**
* Do Upgrade
* @return void
*/
public function apply()
{
/** @var \Magento\Catalog\Setup\CategorySetup $catalogSetup */
$catalogSetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]);
/* ------ freight_class -------- */
$catalogSetup->addAttribute(Product::ENTITY, 'freight_class', [
'type' => 'text',
'source' => 'ShipperHQ\Shipper\Model\Product\Attribute\Source\FreightClass',
'input' => 'select',
'label' => 'Freight Class',
'global' => false,
'visible' => true,
'required' => false,
'visible_on_front' => false,
'is_html_allowed_on_front' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'is_configurable' => false,
'unique' => false,
'user_defined' => true,
'used_in_product_listing' => false
]);
/* ------ shipperhq_nmfc_class -------- */
$catalogSetup->addAttribute(Product::ENTITY, 'shipperhq_nmfc_class', [
'type' => 'text',
'input' => 'text',
'label' => 'NMFC',
'global' => false,
'visible' => true,
'required' => false,
'visible_on_front' => false,
'is_html_allowed_on_front' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'is_configurable' => false,
'unique' => false,
'user_defined' => true,
'used_in_product_listing' => false
]);
/* ------ must_ship_freight -------- */
$catalogSetup->addAttribute(Product::ENTITY, 'must_ship_freight', [
'type' => 'int',
'input' => 'boolean',
'label' => 'Must Ship Freight',
'global' => false,
'visible' => true,
'required' => false,
'visible_on_front' => false,
'is_html_allowed_on_front' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'is_configurable' => false,
'unique' => false,
'user_defined' => true,
'used_in_product_listing' => false,
'note' => 'Can be overridden at Carrier level within ShipperHQ'
]);
/* ------ shipperhq_nmfc_sub -------- */
$catalogSetup->addAttribute(Product::ENTITY, 'shipperhq_nmfc_sub', [
'type' => 'text',
'input' => 'text',
'label' => 'NMFC Sub',
'global' => false,
'visible' => true,
'required' => false,
'visible_on_front' => false,
'is_html_allowed_on_front' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'is_configurable' => false,
'unique' => false,
'user_defined' => true,
'used_in_product_listing' => false,
'note' => 'Only required to support ABF Freight'
]);
$entityTypeId = $catalogSetup->getEntityTypeId(Product::ENTITY);
$attributeSetArr = $catalogSetup->getAllAttributeSetIds($entityTypeId);
$freightAttributeCodes = [
'freight_class' => '1',
'must_ship_freight' => '10'
];
foreach ($attributeSetArr as $attributeSetId) {
//SHQ16-2123 handle migrated instances from M1 to M2
$migrateGroupId = $catalogSetup->getAttributeGroup($entityTypeId, $attributeSetId, 'migration-freight-shipping');
$existingFreightAttributeIds = [];
if ($migrateGroupId) {
$existingFreightAttributeIds = $this->getNonShqAttributeIds(
$catalogSetup,
'migration-freight-shipping',
$attributeSetId
);
$catalogSetup->removeAttributeGroup($entityTypeId, $attributeSetId, 'migration-freight-shipping');
}
$attributeGroupId = $catalogSetup->getAttributeGroup(
$entityTypeId,
$attributeSetId,
'Freight Shipping'
);
if (!$attributeGroupId) {
$catalogSetup->addAttributeGroup($entityTypeId, $attributeSetId, 'Freight Shipping', '101');
}
$attributeGroupId = $catalogSetup->getAttributeGroupId(
$entityTypeId,
$attributeSetId,
'Freight Shipping'
);
$ourFreightAttributeIds = [];
foreach ($freightAttributeCodes as $code => $sort) {
$attributeId = $catalogSetup->getAttributeId($entityTypeId, $code);
$ourFreightAttributeIds[] = $attributeId;
$catalogSetup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
$attributeId,
$sort
);
}
// SHQ18-2825 Add any attributes that were in migration-freight-shipping that were not our attributes back
if (count($existingFreightAttributeIds)) {
$attributeIdsToAdd = array_diff($existingFreightAttributeIds, $ourFreightAttributeIds);
foreach ($attributeIdsToAdd as $attributeId) {
$catalogSetup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
$attributeId,
10
);
}
}
}
}
/**
* SHQ18-2825 Gets all attribute IDs for a given attribute group
*
* @param $catalogSetup
* @param $attributeGroupName
* @param $attributeSetId
*
* @return array
*/
private function getNonShqAttributeIds($catalogSetup, $attributeGroupName, $attributeSetId)
{
$entityTypeId = $catalogSetup->getEntityTypeId(Product::ENTITY);
$attributeGroupId = $catalogSetup->getAttributeGroupId(
$entityTypeId,
$attributeSetId,
$attributeGroupName
);
$collection = $this->attributeCollectionFactory->create();
$collection->setAttributeGroupFilter($attributeGroupId);
$allAttributeIds = [];
foreach ($collection->getItems() as $attribute) {
$allAttributeIds[] = $attribute->getAttributeId();
}
return $allAttributeIds;
}
/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}
public function revert()
{
/** @var \Magento\Catalog\Setup\CategorySetup $catalogSetup */
$catalogSetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]);
$attributeCodes = [
'freight_class',
'shipperhq_nmfc_class',
'must_ship_freight',
'shipperhq_nmfc_sub'
];
foreach ($attributeCodes as $attributeCode) {
$catalogSetup->removeAttribute(Product::ENTITY, $attributeCode);
}
$entityTypeId = $catalogSetup->getEntityTypeId(Product::ENTITY);
$attributeSets = $catalogSetup->getAllAttributeSetIds($entityTypeId);
foreach ($attributeSets as $attributeSet) {
$attributeGroupId = $catalogSetup->getAttributeGroupId(
$entityTypeId,
$attributeSet,
'Freight Shipping'
);
if ($attributeGroupId) {
$catalogSetup->removeAttributeGroup($entityTypeId, $attributeSet, $attributeGroupId);
}
}
}
}