I want to load all collection in Magento 2 and save one by one programmatically. is there any faster way to do so.

I tried below code for one category

use Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->setStoreId(0)->load(17);
  • you can create custom command for save category – Rk Rathod Jun 12 '19 at 10:15
  • Can you please add some context to your request? – Jonathan Ribas Jun 12 '19 at 10:16
  • @JonathanRibas i want to get category collection in a varaible and i want to foreach all this category just save. nothing attribute need to be changed. – Pradeep Sanku Jun 12 '19 at 10:50


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="quoteGridAssignSalesrep" xsi:type="object">Vendor\Module\Console\Command\CategorySave</item>



namespace Vendor\Module\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\State as AppState;

class CategorySave extends Command
    protected $categoryFactory;

    public function __construct(
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        AppState $appState)
        $this->appState = $appState;
        $this->categoryFactory =$categoryFactory;

    protected function configure()
            ->setDescription('The description of you command here!');

    protected function execute(InputInterface $input, OutputInterface $output)
    }catch (\Exception $e){
    $categoryCollection = $this->categoryFactory->create()->getCollection();
    foreach($categoryCollection as $category)
        $output->writeln('Save Category Id' . $category->getId());
    } catch (\Exception $e) {
            $output->writeln('Error In Category Id ' .$category->getId(). $e->getMessage());

After This Run This Command

php bin/magento command_name
| improve this answer | |

I created an extension which creates random categories via console command

This is the fastest method I could find



Doesn't cover loading categories but if you cover loading via a category collection as the other answer suggests this should be enough.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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