Adding New Categories and Subcategories from CSV File in Magento
Adding new categories and subcategories is an essential part of managing your Magento store’s product taxonomy. In this post, we’ll guide you through the process of importing these hierarchical categories using a CSV file. We’ll cover concepts like creating root categories, adding subcategories, and relating them to each other.
Understanding Category Hierarchy in Magento
Magento uses a nested directory structure for its category system. Each category can have child categories (subcategories), which can further contain their own children (grandchildren or sub-subcategories). This hierarchical organization helps in grouping related products and enhances the navigation experience for your customers.
The CSV file you provided follows this hierarchy:
root_cat_id;subcat_id;subcat_name;subsubcat_id;subsubcat_name;
1;2;Animal;3;Dog;
In this example, 1 is a root category with two subcategories (with IDs 2 and 3), and one of those subcategories has another child category (ID 3).
Creating the Root Category
First, let’s create a new root category using the code you started with. We’ll use the Mage::getModel('catalog/category') to get an instance of the category model.
$category = Mage::getModel('catalog/category');
$category->setStoreId(0);
$rootCategory['name'] = 'Reserved';
$rootCategory['path'] = "1"; // For root category, set path as 1
$rootCategory['display_mode'] = "PRODUCTS_AND_PAGE";
$rootCategory['is_active'] = 1;
$category->addData($rootCategory);
try {
$category->save();
} catch (Exception $e) {
echo $e->getMessage();
}
This code creates a new category named ‘Reserved’ and sets it as the root category by specifying path as 1. The display_mode is set to show both products and static content, and the is_active flag is enabled.
Getting the Root Category ID
To access the newly created category’s ID, you can use the following code:
$rootCategoryId = $category->getId();
This will give you the unique identifier for your root category.
Adding Subcategories to the Root Category
Now let’s add subcategories to this root category. We’ll create a function that accepts the parent category ID and processes each row of the CSV file:
function importCategories($csvFile, $rootCategoryId)
{
// Read the CSV file line by line
if (($handle = fopen($csvFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
list($rootId, $subcatId, $subcatName, $subSubcatId, $subSubcatName) = $data;
// Skip the header row
if (!empty($rootId)) {
// Create a new category model
$category = Mage::getModel('catalog/category');
// Set the category data based on CSV fields
$category->setStoreId(0);
$category->setName($subcatName);
// If it's a sub-subcategory, set its parent ID; otherwise, use the root category ID
if (!empty($subSubcatId)) {
$parentCategory = Mage::getModel('catalog/category')->load($rootId);
$category->setParentId($parentCategory->getId());
} else {
$category->setParentId($rootCategoryId);
}
// Save the new category
try {
$category->save();
} catch (Exception $e) {
echo "Error adding category: " . $e->getMessage() . "\n";
}
}
}
fclose($handle);
}
}
This function reads each line of the CSV file and creates new categories based on the provided data. It uses the load() method to retrieve the parent category object and sets its ID as the parentId for the child category.
Conclusion
Adding new categories and subcategories from a CSV file allows you to efficiently manage your Magento store’s product taxonomy. By understanding the category hierarchy and using the appropriate Magento models, you can create well-structured and organized product categories that enhance both your administrative experience and your customers’ shopping journey.
Remember that keeping your category structure clean is essential for improving site navigation, SEO performance, and overall user experience. Regularly review and update your categories to ensure they remain relevant and helpful to your audience.
If you’re looking for more advanced Magento development tips and tricks, be sure to check out our other articles and resources at Laravel Company. We provide comprehensive guides on various aspects of Magento development, from theme customization to extension creation.