How to Programmatically Export CSV File Category Name with Parent Category Name using Root Script in Magento 2? - magento source24


How to Programmatically Export CSV File Category Name with Parent Category Name using Root Script in Magento 2?


How to Programmatically Export CSV File Category Name with Parent Category Name using Root Script in Magento 2? - magento source24


In Magento 2, you can programmatically export CSV files with category names and their parent category names using a custom root script. To achieve this, you will need to create a PHP script and execute it via the command line or through a browser. Here's a step-by-step guide on how to do it:


Step 1: Prepare the root script file

Create a new PHP file in your Magento 2 root directory. For example, you can name it export_categories.php.


Step 2: Initialize Magento

Include the Magento bootstrap file to initialize the Magento environment. This will allow you to use Magento functions and classes in your script. Add the following code at the beginning of the export_categories.php file:


<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';


$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();


Step 3: Load Category Data

Load the category collection from the database, and then loop through the categories to retrieve the category names and their parent category names. Add the following code after the previous step:


<?php

// ...

// Previous code here


// Load the category model and collection

$categoryModel = $objectManager->create(\Magento\Catalog\Model\Category::class);

$categoryCollection = $categoryModel->getCollection();


// Create an array to store the category data

$categoryData = [];


foreach ($categoryCollection as $category) {

    // Get the category name and parent category name

    $categoryName = $category->getName();

    $parentCategoryName = '';

    

    if ($category->getParentId()) {

        $parentCategory = $categoryModel->load($category->getParentId());

        $parentCategoryName = $parentCategory->getName();

    }


    // Add the category data to the array

    $categoryData[] = [

        'category_name' => $categoryName,

        'parent_category_name' => $parentCategoryName,

    ];

}


Step 4: Export Data to CSV

Now, we will export the category data to a CSV file. Add the following code after the previous step: 


<?php

// ...

// Previous code here

// Export the data to CSV

$csvFile = 'category_export.csv';

$csvFilePath = BP . '/' . $csvFile;

$fp = fopen($csvFilePath, 'w');

fputcsv($fp, ['Category Name', 'Parent Category Name']);

foreach ($categoryData as $data) {

    fputcsv($fp, $data);

}

fclose($fp);

echo "CSV file generated: {$csvFile}\n";



Step 5: Run the Script

Save the file and then run the script via the command line or through a browser. Ensure you have sufficient permissions to write files in the Magento root directory. When the script finishes running, it will generate a category_export.csv file containing the category names and their parent category names.


Note: Always exercise caution when executing custom scripts on your Magento installation. Make sure to back up your database before running any custom scripts to avoid data loss. Additionally, running scripts on a production environment may have significant consequences, so it's recommended to test the script thoroughly on a development or staging environment before using it on a live site.



Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post