2

i came accros this script ( link ) that allows you to create multiple subcategories at once by creating a list of the subcategories in a csv file and then the scripts takes the ID of the root category to hold these subcategories. It seems to work fine on magento 1, but not on magento 2

This is the script:

Needed changes to change into below script:

  1. $rootCateId - here you need to set the ID of root category under which you want to add sub categories
  2. $path = here I set this as "1/2/" mostly in magento by default this is same but cross check it once.

<?php

set_time_limit(0);
ini_set('memory_limit', '1024M');
umask(0);
require_once 'app/Mage.php';
Mage::app('admin');
if (($handle = fopen("category.csv", "r")) !== FALSE) {
    $counter = 1;
    while (($data = fgetcsv($handle)) !== FALSE) {
        if ($counter > 1) {
            $cateName = $data[0];
            echo $cateName . "\n";
            $rootCateId = 559; // here we need to assign root cate Id under which you want to create your new category
            if ($cateName) {
                createCategory($cateName, $rootCateId);
            }
        }
        $counter++;
        if ($counter > 2) {
            // if want to break the scrip
            // exit();
        }
    }
}
function createCategory($name, $rootCateId)
{
    $path = "1/2/" . $rootCateId; // here 1/2 is my store root category path you can check this in database table 'catalog_category_entity' table
    $cateName = $name;
    $category = Mage::getModel('catalog/category');
    $category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().
    if ($id) {
        //if update
        //$category->load($id);
    }
    $general['name'] = $cateName;
    $general['path'] = $path; // catalog path
    //$general['description'] = " Category";
    //$general['meta_title'] = "My Category";
    //Page title
    //$general['meta_keywords'] = "";
    //$general['meta_description'] = "";
    //$general['landing_page'] = "";
    //has to be created in advance, here comes id
    $general['display_mode'] = "PRODUCTS"; //static block and the products are shown on the page
    $general['is_active'] = 1;
    $general['is_anchor'] = 0;
    //$general['page_layout'] = 'two_columns_left';
    //$general['url_key'] = "cars"; //url to be used for this category's page by magento.
    //$general['image'] = "cars.jpg";
    $category->addData($general);
    try {
        $category->save();
        echo "Success! new category Id: " . $category->getId() . "\n";
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

I have made the necessery changes but when i run the script through my browser i get an error messege that no such file as mage.php exists. I have very little php knowledge but it would seem the scrip is not compatible with magento 2. Please could someone help me figure out how to get the script working on magento 2.
Thanks in advance.

Browse other questions tagged or ask your own question.