Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
category_tree.php
Go to the documentation of this file.
1 <?php
12  [
13  'id' => 400,
14  'name' => 'Category 1',
15  'parent_id' => 2,
16  'path' => '1/2/400',
17  'level' => 2,
18  'available_sort_by' => 'name',
19  'default_sort_by' => 'name',
20  'is_active' => true,
21  'position' => 1,
22  ],
23  [
24  'id' => 401,
25  'name' => 'Category 1.1',
26  'parent_id' => 400,
27  'path' => '1/2/400/401',
28  'level' => 3,
29  'available_sort_by' => 'name',
30  'default_sort_by' => 'name',
31  'is_active' => true,
32  'position' => 1
33  ],
34  [
35  'id' => 402,
36  'name' => 'Category 1.1.1',
37  'parent_id' => 401,
38  'path' => '1/2/400/401/402',
39  'level' => 4,
40  'available_sort_by' => 'name',
41  'default_sort_by' => 'name',
42  'is_active' => true,
43  'position' => 1
44  ],
45 ];
46 foreach ($categories as $data) {
48  \Magento\Catalog\Model\Category::class
49  );
50  $model->isObjectNew(true);
51  $model->setId($data['id'])
52  ->setName($data['name'])
53  ->setParentId($data['parent_id'])
54  ->setPath($data['path'])
55  ->setLevel($data['level'])
56  ->setAvailableSortBy($data['available_sort_by'])
57  ->setDefaultSortBy($data['default_sort_by'])
58  ->setIsActive($data['is_active'])
59  ->setPosition($data['position'])
60  ->setStoreId(0)
61  ->save();
62 }
$categories