-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagento2_create_category_programmatically.php
62 lines (45 loc) · 1.84 KB
/
magento2_create_category_programmatically.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
<?php
//
// Magento 2 create category programmatically
//
// Source : https://github.com/coresh/magento2scripts/blob/master/magento2_create_category_programmatically.php
// Purpose : testing
// Tested : magento EE 2.1.3
// Usage:
// $ cd magento2 root directory
// $ php /path/to/magento2_create_category_programmatically.php
//
include('./app/bootstrap.php');
use \Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore();
// Get Root Category ID
$rootCategoryId = $store->getRootCategoryId();
echo "rootCategoryId: ". $rootCategoryId . "\n";
// Load root category
$rootCategory = $objectManager->create('\Magento\Catalog\Model\CategoryFactory')->create()->load($rootCategoryId);
$rootCategoryPath = $rootCategory->getPath();
echo "rootCategoryPath: ". $rootCategoryPath . "\n";
$storeId = $store->getStoreId();
$category = $objectManager->create('\Magento\Catalog\Model\CategoryFactory')->create();
$data = [
'store_id' => $storeId,
'path' => $rootCategoryPath,
'parent_id' => $rootCategoryId,
'updated_at' => '2017-01-11 11:17:19',
'position' => '1',
'is_active' => '1',
'is_anchor' => '1',
'name' => 'name' . rand(1111111, 9999999),
'custom_design_from' => '',
'custom_design_to' => '',
'include_in_menu' => '1',
'default_sort_by' => '',
'meta_title' => 'meta_title' . rand(1111111, 9999999),
'url_key' => 'url_key' . rand(1111111, 9999999),
];
$category->addData($data);
$createdCategoryId = $category->save()->getId();
echo 'Category: Created. Category Id: '. $createdCategoryId . "\n";