Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InstallDefaultCategories.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Helper\DefaultCategoryFactory;
11 use Magento\Catalog\Setup\CategorySetupFactory;
16 
24 {
28  private $moduleDataSetup;
29 
33  private $categorySetupFactory;
34 
38  private $defaultCategoryFactory;
39 
46  public function __construct(
47  ModuleDataSetupInterface $moduleDataSetup,
48  CategorySetupFactory $categorySetupFactory,
49  \Magento\Catalog\Helper\DefaultCategoryFactory $defaultCategoryFactory
50  ) {
51  $this->moduleDataSetup = $moduleDataSetup;
52  $this->categorySetupFactory = $categorySetupFactory;
53  $this->defaultCategoryFactory = $defaultCategoryFactory;
54  }
55 
62  public function apply()
63  {
65  $categorySetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]);
67  $defaultCategory = $this->defaultCategoryFactory->create();
68  $defaultCategoryId = $defaultCategory->getId();
69 
70  $categorySetup->installEntities();
71  // Create Root Catalog Node
72  $categorySetup->createCategory()
73  ->load($rootCategoryId)
74  ->setId($rootCategoryId)
75  ->setStoreId(0)
76  ->setPath($rootCategoryId)
77  ->setLevel(0)
78  ->setPosition(0)
79  ->setChildrenCount(0)
80  ->setName('Root Catalog')
81  ->setInitialSetupFlag(true)
82  ->save();
83 
84  // Create Default Catalog Node
85  $category = $categorySetup->createCategory();
86  $category->load($defaultCategoryId)
87  ->setId($defaultCategoryId)
88  ->setStoreId(0)
89  ->setPath($rootCategoryId . '/' . $defaultCategoryId)
90  ->setName('Default Category')
91  ->setDisplayMode('PRODUCTS')
92  ->setIsActive(1)
93  ->setLevel(1)
94  ->setInitialSetupFlag(true)
95  ->setAttributeSetId($category->getDefaultAttributeSetId())
96  ->save();
97 
98  $data = [
99  'scope' => 'default',
100  'scope_id' => 0,
102  'value' => $category->getId(),
103  ];
104  $this->moduleDataSetup->getConnection()->insertOnDuplicate(
105  $this->moduleDataSetup->getTable('core_config_data'),
106  $data,
107  ['value']
108  );
109 
110  $categorySetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', 'Design', 6);
111 
112  $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
113  $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
114  $attributeGroupId = $categorySetup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
115 
116  // update General Group
117  $categorySetup->updateAttributeGroup($entityTypeId, $attributeSetId, $attributeGroupId, 'sort_order', '10');
118 
119  $groups = [
120  'display' => ['name' => 'Display Settings', 'code' => 'display-settings', 'sort' => 20, 'id' => null],
121  'design' => ['name' => 'Custom Design', 'code' => 'custom-design', 'sort' => 30, 'id' => null],
122  ];
123 
124  foreach ($groups as $k => $groupProp) {
125  $categorySetup->addAttributeGroup($entityTypeId, $attributeSetId, $groupProp['name'], $groupProp['sort']);
126  $groups[$k]['id'] = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, $groupProp['code']);
127  }
128 
129  // update attributes group and sort
130  $attributes = [
131  'custom_design' => ['group' => 'design', 'sort' => 10],
132  // 'custom_design_apply' => array('group' => 'design', 'sort' => 20),
133  'custom_design_from' => ['group' => 'design', 'sort' => 30],
134  'custom_design_to' => ['group' => 'design', 'sort' => 40],
135  'page_layout' => ['group' => 'design', 'sort' => 50],
136  'custom_layout_update' => ['group' => 'design', 'sort' => 60],
137  'display_mode' => ['group' => 'display', 'sort' => 10],
138  'landing_page' => ['group' => 'display', 'sort' => 20],
139  'is_anchor' => ['group' => 'display', 'sort' => 30],
140  'available_sort_by' => ['group' => 'display', 'sort' => 40],
141  'default_sort_by' => ['group' => 'display', 'sort' => 50],
142  ];
143 
144  foreach ($attributes as $attributeCode => $attributeProp) {
145  $categorySetup->addAttributeToGroup(
148  $groups[$attributeProp['group']]['id'],
150  $attributeProp['sort']
151  );
152  }
153 
157  $data = [
158  ['link_type_id' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_RELATED, 'code' => 'relation'],
159  ['link_type_id' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL, 'code' => 'up_sell'],
160  ['link_type_id' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_CROSSSELL, 'code' => 'cross_sell'],
161  ];
162 
163  foreach ($data as $bind) {
164  $this->moduleDataSetup->getConnection()->insertForce(
165  $this->moduleDataSetup->getTable(
166  'catalog_product_link_type'
167  ),
168  $bind
169  );
170  }
171 
175  $data = [
176  [
177  'link_type_id' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_RELATED,
178  'product_link_attribute_code' => 'position',
179  'data_type' => 'int',
180  ],
181  [
182  'link_type_id' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL,
183  'product_link_attribute_code' => 'position',
184  'data_type' => 'int'
185  ],
186  [
187  'link_type_id' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_CROSSSELL,
188  'product_link_attribute_code' => 'position',
189  'data_type' => 'int'
190  ],
191  ];
192 
193  $this->moduleDataSetup->getConnection()->insertMultiple(
194  $this->moduleDataSetup->getTable('catalog_product_link_attribute'),
195  $data
196  );
197 
202  $describe = $this->moduleDataSetup->getConnection()
203  ->describeTable($this->moduleDataSetup->getTable('catalog_eav_attribute'));
204  foreach ($describe as $columnData) {
205  if ($columnData['COLUMN_NAME'] == 'attribute_id') {
206  continue;
207  }
208  $this->moduleDataSetup->getConnection()->dropColumn(
209  $this->moduleDataSetup->getTable('eav_attribute'),
210  $columnData['COLUMN_NAME']
211  );
212  }
213 
214  $newGeneralTabName = 'Product Details';
215  $newPriceTabName = 'Advanced Pricing';
216  $newImagesTabName = 'Image Management';
217  $newMetaTabName = 'Search Engine Optimization';
218  $autosettingsTabName = 'Autosettings';
219  $tabNames = [
220  'General' => [
221  'attribute_group_name' => $newGeneralTabName,
222  'attribute_group_code' => $categorySetup->convertToAttributeGroupCode($newGeneralTabName),
223  'tab_group_code' => 'basic',
224  'sort_order' => 10,
225  ],
226  'Images' => [
227  'attribute_group_name' => $newImagesTabName,
228  'attribute_group_code' => $categorySetup->convertToAttributeGroupCode($newImagesTabName),
229  'tab_group_code' => 'basic',
230  'sort_order' => 20,
231  ],
232  'Meta Information' => [
233  'attribute_group_name' => $newMetaTabName,
234  'attribute_group_code' => $categorySetup->convertToAttributeGroupCode($newMetaTabName),
235  'tab_group_code' => 'basic',
236  'sort_order' => 30,
237  ],
238  'Prices' => [
239  'attribute_group_name' => $newPriceTabName,
240  'attribute_group_code' => $categorySetup->convertToAttributeGroupCode($newPriceTabName),
241  'tab_group_code' => 'advanced',
242  'sort_order' => 40,
243  ],
244  'Design' => ['attribute_group_code' => 'design', 'tab_group_code' => 'advanced', 'sort_order' => 50],
245  ];
246 
247  $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
248  $attributeSetId = $categorySetup->getAttributeSetId($entityTypeId, 'Default');
249 
250  //Rename attribute tabs
251  foreach ($tabNames as $tabName => $tab) {
252  $groupId = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, $tabName);
253  if ($groupId) {
254  foreach ($tab as $propertyName => $propertyValue) {
255  $categorySetup->updateAttributeGroup(
258  $groupId,
259  $propertyName,
260  $propertyValue
261  );
262  }
263  }
264  }
265 
266  //Add new tab
267  $categorySetup->addAttributeGroup($entityTypeId, $attributeSetId, $autosettingsTabName, 60);
268  $categorySetup->updateAttributeGroup(
271  'Autosettings',
272  'attribute_group_code',
273  'autosettings'
274  );
275  $categorySetup->updateAttributeGroup(
278  'Autosettings',
279  'tab_group_code',
280  'advanced'
281  );
282 
283  //New attributes order and properties
284  $properties = ['is_required', 'default_value', 'frontend_input_renderer'];
285  $attributesOrder = [
286  //Product Details tab
287  'name' => [$newGeneralTabName => 10],
288  'sku' => [$newGeneralTabName => 20],
289  'price' => [$newGeneralTabName => 30],
290  'image' => [$newGeneralTabName => 50],
291  'weight' => [$newGeneralTabName => 70, 'is_required' => 0],
292  'category_ids' => [$newGeneralTabName => 80],
293  'description' => [$newGeneralTabName => 90, 'is_required' => 0],
294  'status' => [
295  $newGeneralTabName => 100,
296  'is_required' => 0,
297  'default_value' => 1,
298  'frontend_input_renderer' => \Magento\Framework\Data\Form\Element\Hidden::class,
299  ],
300  //Autosettings tab
301  'short_description' => [$autosettingsTabName => 0, 'is_required' => 0],
302  'visibility' => [$autosettingsTabName => 20, 'is_required' => 0],
303  'news_from_date' => [$autosettingsTabName => 30],
304  'news_to_date' => [$autosettingsTabName => 40],
305  'country_of_manufacture' => [$autosettingsTabName => 50],
306  ];
307 
308  foreach ($attributesOrder as $key => $value) {
309  $attribute = $categorySetup->getAttribute($entityTypeId, $key);
310  if ($attribute) {
311  foreach ($value as $propertyName => $propertyValue) {
312  if (in_array($propertyName, $properties)) {
313  $categorySetup->updateAttribute(
315  $attribute['attribute_id'],
316  $propertyName,
317  $propertyValue
318  );
319  } else {
320  $categorySetup->addAttributeToGroup(
323  $propertyName,
324  $attribute['attribute_id'],
325  $propertyValue
326  );
327  }
328  }
329  }
330  }
331 
332  foreach (['status', 'visibility'] as $attributeCode) {
333  $categorySetup->updateAttribute(
334  \Magento\Catalog\Model\Product::ENTITY,
336  'is_required_in_admin_store',
337  '1'
338  );
339  }
340  $categorySetup->updateAttribute(
341  \Magento\Catalog\Model\Category::ENTITY,
342  'custom_design_from',
343  'attribute_model',
344  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
345  );
346  $categorySetup->updateAttribute(
347  \Magento\Catalog\Model\Category::ENTITY,
348  'custom_design_from',
349  'frontend_model',
350  \Magento\Eav\Model\Entity\Attribute\Frontend\Datetime::class
351  );
352  }
353 
357  public static function getDependencies()
358  {
359  return [];
360  }
361 
365  public static function getVersion()
366  {
367  return '2.0.0';
368  }
369 
373  public function getAliases()
374  {
375  return [];
376  }
377 }
$attributeGroupId
__construct(ModuleDataSetupInterface $moduleDataSetup, CategorySetupFactory $categorySetupFactory, \Magento\Catalog\Helper\DefaultCategoryFactory $defaultCategoryFactory)
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13
$properties
Definition: categories.php:26