5
5
namespace Meta \Catalog \Setup \Patch \Data ;
6
6
7
7
use Magento \Catalog \Model \Product ;
8
+ use Magento \Eav \Setup \EavSetup ;
8
9
use Magento \Eav \Setup \EavSetupFactory ;
10
+ use Magento \Framework \Exception \LocalizedException ;
9
11
use Magento \Framework \Setup \ModuleDataSetupInterface ;
10
12
use Magento \Framework \Setup \Patch \DataPatchInterface ;
11
13
use Magento \Framework \Setup \Patch \PatchRevertableInterface ;
14
+ use Magento \Framework \Validator \ValidateException ;
12
15
use Meta \Catalog \Setup \MetaCatalogAttributes ;
13
16
14
17
class AddProductAttributes implements DataPatchInterface, PatchRevertableInterface
@@ -68,6 +71,8 @@ public function getAliases(): array
68
71
* Create product attributes
69
72
*
70
73
* @return void
74
+ * @throws LocalizedException
75
+ * @throws ValidateException
71
76
*/
72
77
public function apply (): void
73
78
{
@@ -78,17 +83,18 @@ public function apply(): void
78
83
79
84
$ entityTypeId = $ eavSetup ->getEntityTypeId (Product::ENTITY );
80
85
$ attributeSetId = $ eavSetup ->getDefaultAttributeSetId ($ entityTypeId );
86
+ $ attributeGroupId = $ eavSetup ->getDefaultAttributeGroupId (Product::ENTITY , $ attributeSetId );
81
87
82
88
foreach ($ productAttributes as $ attributeCode => $ attributeData ) {
83
89
84
90
if (!$ eavSetup ->getAttributeId (Product::ENTITY , $ attributeCode )) {
85
91
$ eavSetup ->addAttribute (Product::ENTITY , $ attributeCode , $ attributeData );
86
92
}
87
- // Assign attributes to default attribute set
93
+ // Assign attributes to default attribute set and group
88
94
$ eavSetup ->addAttributeToGroup (
89
95
$ entityTypeId ,
90
96
$ attributeSetId ,
91
- $ attributeData [ ' group ' ] ,
97
+ $ attributeGroupId ,
92
98
$ attributeCode
93
99
);
94
100
}
@@ -108,6 +114,7 @@ public function revert(): void
108
114
109
115
foreach (array_keys ($ productAttributes ) as $ attributeCode ) {
110
116
$ eavSetup ->removeAttribute (Product::ENTITY , $ attributeCode );
117
+ echo 'Removed attribute ' . $ attributeCode . PHP_EOL ;
111
118
}
112
119
//delete the patch entry from patch_list table
113
120
$ this ->moduleDataSetup ->deleteTableRow ('patch_list ' , 'patch_name ' , __CLASS__ );
0 commit comments