1

How to create categories without using id..programmatically in magento2. Can I store these categories without using id

  ID      ParentID      Name    

 100          0         Mobile                                      

 106          100       samsung 

EDIT

I want to create a category. but it is not running. it is showing page not found error. Any error in this code?

<?php

    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php'; 
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager(); 
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');

    function readCSV($csvFile){
     $file_handle = fopen($csvFile, 'r');
     while (!feof($file_handle) ) {
      $line_of_text[] = fgetcsv($file_handle, 1024);
     }
     fclose($file_handle);
     return $line_of_text;
    }
    // Set path to CSV file
    $csvFile = 'csv/categories.csv';
    $arrResult = readCSV($csvFile);
    echo '<pre>';
    print_r($arrResult);
    echo '</pre>';

    protected function prepareData($row, $id = null)
    {

        // in case you want to user the root category id
        // $rootCat = $this->_objectManager->get('Magento\Catalog\Model\Category');
        // $cat_info = $rootCat->load($rootrowId);
        // $categoryTmp->setPath($rootCat->getPath());

           $data = [
                'data' => [
                    "parent_id" => 2,
                    'name' => $row['category_name'],
                    "is_active" => true,
                    "position" => 10,
                    "include_in_menu" => false,
                ],
                'custom_attributes' => [
                    "display_mode"=> "PRODUCTS",
                    "is_anchor"=> "1"
                ]
            ];

            if($id) {
                $data['data']['id'] = $id;
            }

            return $data;
     }

    protected function createCategory(array $data)
    {
        $category = $this->_objectManager
            ->create('Magento\Catalog\Model\Category', $data)
            ->setCustomAttributes($data['custom_attributes']);

        $repository = $this->_objectManager->get(CategoryRepositoryInterface::class);
        $result = $repository->save($category);

        echo "Created Category " . $data['data']['name'] . "\n";

        return true;
    }

foreach ($arrResult as $row) {
    $data = $this->prepareData($row);
    $this->createCategory($data);
}
0
0

Please check below code to create category programmatically:

First, we will prepare an array of categories which needs to be created.

protected function prepareData($row, $id = null)
{
    // in case you want to user the root category id
    // $rootCat = $this->_objectManager->get('Magento\Catalog\Model\Category');
    // $cat_info = $rootCat->load($rootrowId);
    // $categoryTmp->setPath($rootCat->getPath());

       $data = [
            'data' => [
                "parent_id" => 2,
                'name' => $row['category_name'],
                "is_active" => true,
                "position" => 10,
                "include_in_menu" => false,
            ],
            'custom_attributes' => [
                "display_mode"=> "PRODUCTS",
                "is_anchor"=> "1"
            ]
        ];

        if($id) {
            $data['data']['id'] = $id;
        }

        return $data;
 }

Once the data is ready, let’s create the new category using the returned data above.

protected function createCategory(array $data)
{
    $category = $this->_objectManager
        ->create('Magento\Catalog\Model\Category', $data)
        ->setCustomAttributes($data['custom_attributes']);

    $repository = $this->_objectManager->get(CategoryRepositoryInterface::class);
    $result = $repository->save($category);

    echo "Created Category " . $data['data']['name'] . "\n";

    return true;
}

Finally call both functions like below:

foreach ($array_with_categories as $row) {
    $data = $this->prepareData($row);
    $this->createCategory($data);
}

Please let me know if you find any problem.

1
  • It will show "page not found error" – Mahi M Oct 14 '17 at 5:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.