Skip to content

Commit ac665b1

Browse files
authored
issue-resolution/ category attribute fixes (#191)
* fixed data patch to create category and product attributes * updated the name of attribute displayed in category form * added new line at end of file * fixed static test issue
1 parent 47c7254 commit ac665b1

File tree

4 files changed

+132
-19
lines changed

4 files changed

+132
-19
lines changed

app/code/Meta/Catalog/Setup/MetaCatalogAttributes.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MetaCatalogAttributes
1414
*
1515
* @return array[]
1616
*/
17-
public function execute(): array
17+
public function getProductAttributes(): array
1818
{
1919
return [
2020
'google_product_category' => [
@@ -33,17 +33,6 @@ public function execute(): array
3333
'is_html_allowed_on_front' => false,
3434
'visible_on_front' => false
3535
],
36-
'sync_to_facebook_catalog' => [
37-
'type' => 'int',
38-
'label' => 'Sync to Facebook Catalog',
39-
'input' => 'boolean',
40-
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
41-
'visible' => true,
42-
'default' => "1",
43-
'required' => false,
44-
'global' => ScopedAttributeInterface::SCOPE_STORE,
45-
'group' => 'Display Settings'
46-
],
4736
'send_to_facebook' => [
4837
'group' => 'General',
4938
'type' => 'int',
@@ -63,4 +52,26 @@ public function execute(): array
6352
]
6453
];
6554
}
55+
56+
/**
57+
* Returns array of category attributes to be created
58+
*
59+
* @return array[]
60+
*/
61+
public function getCategoryAttributes(): array
62+
{
63+
return [
64+
'sync_to_facebook_catalog' => [
65+
'type' => 'int',
66+
'label' => 'Sync to Facebook Catalog',
67+
'input' => 'boolean',
68+
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
69+
'visible' => true,
70+
'default' => "1",
71+
'required' => false,
72+
'global' => ScopedAttributeInterface::SCOPE_STORE,
73+
'group' => 'Display Settings'
74+
]
75+
];
76+
}
6677
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meta\Catalog\Setup\Patch\Data;
6+
7+
use Magento\Catalog\Model\Category;
8+
use Magento\Framework\Setup\Patch\DataPatchInterface;
9+
use Magento\Eav\Setup\EavSetupFactory;
10+
use Magento\Framework\Setup\ModuleDataSetupInterface;
11+
use Meta\Catalog\Setup\MetaCatalogAttributes;
12+
13+
class AddCategoryAttributes implements DataPatchInterface
14+
{
15+
/**
16+
* @var ModuleDataSetupInterface
17+
*/
18+
private $moduleDataSetup;
19+
20+
/**
21+
* @var EavSetupFactory
22+
*/
23+
private $eavSetupFactory;
24+
25+
/**
26+
* @var MetaCatalogAttributes
27+
*/
28+
private $metaCatalogAttributes;
29+
30+
/**
31+
* @param ModuleDataSetupInterface $moduleDataSetup
32+
* @param EavSetupFactory $eavSetupFactory
33+
* @param MetaCatalogAttributes $metaCatalogAttributes
34+
*/
35+
public function __construct(
36+
ModuleDataSetupInterface $moduleDataSetup,
37+
EavSetupFactory $eavSetupFactory,
38+
MetaCatalogAttributes $metaCatalogAttributes
39+
) {
40+
$this->moduleDataSetup = $moduleDataSetup;
41+
$this->eavSetupFactory = $eavSetupFactory;
42+
$this->metaCatalogAttributes = $metaCatalogAttributes;
43+
}
44+
45+
/**
46+
* Get dependencies for the data patch
47+
*
48+
* @return array
49+
*/
50+
public static function getDependencies(): array
51+
{
52+
return [];
53+
}
54+
55+
/**
56+
* Get alias for the data patch
57+
*
58+
* @return array
59+
*/
60+
public function getAliases(): array
61+
{
62+
return [];
63+
}
64+
65+
/**
66+
* Create category attributes
67+
*
68+
* @return void
69+
*/
70+
public function apply(): void
71+
{
72+
$categoryAttributes = $this->metaCatalogAttributes->getCategoryAttributes();
73+
74+
/** @var EavSetup $eavSetup */
75+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
76+
77+
foreach ($categoryAttributes as $attributeCode => $attributeData) {
78+
if (!$eavSetup->getAttributeId(Category::ENTITY, $attributeCode)) {
79+
$eavSetup->addAttribute(Category::ENTITY, $attributeCode, $attributeData);
80+
}
81+
}
82+
}
83+
84+
/**
85+
* Revert the created product attributes
86+
*
87+
* @return void
88+
*/
89+
public function revert(): void
90+
{
91+
$categoryAttributes = $this->metaCatalogAttributes->getCategoryAttributes();
92+
$this->moduleDataSetup->getConnection()->startSetup();
93+
/** @var EavSetup $eavSetup */
94+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
95+
96+
foreach (array_keys($categoryAttributes) as $attributeCode) {
97+
$eavSetup->removeAttribute(Category::ENTITY, $attributeCode);
98+
}
99+
100+
$this->moduleDataSetup->getConnection()->endSetup();
101+
}
102+
}

app/code/Meta/Catalog/Setup/Patch/Data/AddProductAttributes.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public function getAliases(): array
7070
*/
7171
public function apply(): void
7272
{
73-
$catalogAttributes = $this->metaCatalogAttributes->execute();
73+
$productAttributes = $this->metaCatalogAttributes->getProductAttributes();
7474

7575
/** @var EavSetup $eavSetup */
7676
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
7777

7878
$entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);
7979
$attributeSetId = $eavSetup->getDefaultAttributeSetId($entityTypeId);
8080

81-
foreach ($catalogAttributes as $attributeCode => $attributeData) {
81+
foreach ($productAttributes as $attributeCode => $attributeData) {
8282

8383
if (!$eavSetup->getAttributeId(Product::ENTITY, $attributeCode)) {
8484
$eavSetup->addAttribute(Product::ENTITY, $attributeCode, $attributeData);
@@ -100,12 +100,12 @@ public function apply(): void
100100
*/
101101
public function revert(): void
102102
{
103-
$catalogAttributes = $this->metaCatalogAttributes->execute();
103+
$productAttributes = $this->metaCatalogAttributes->getProductAttributes();
104104
$this->moduleDataSetup->getConnection()->startSetup();
105105
/** @var EavSetup $eavSetup */
106106
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
107107

108-
foreach (array_keys($catalogAttributes) as $attributeCode) {
108+
foreach (array_keys($productAttributes) as $attributeCode) {
109109
$eavSetup->removeAttribute(Product::ENTITY, $attributeCode);
110110
}
111111

app/code/Meta/Catalog/view/adminhtml/ui_component/category_form.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0"?>
22
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
33
<fieldset name="general">
4-
<field name="sync_to_facebook_catalog" formElement="checkbox" >
4+
<field name="sync_to_facebook_catalog" sortOrder="40" formElement="checkbox">
55
<argument name="data" xsi:type="array">
6-
<item name="config" xsi:type="array" >
6+
<item name="config" xsi:type="array">
77
<item name="default" xsi:type="number">1</item>
88
</item>
99
</argument>
1010
<settings>
11-
<label>Log JS Errors to Session Storage</label>
11+
<label translate="true">Sync to Facebook Catalog</label>
1212
<dataType>boolean</dataType>
1313
</settings>
1414
<formElements>

0 commit comments

Comments
 (0)