0

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;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->setStoreId(0)->load(17);
$category->save();
  • you can create custom command for save category – Rk Rathod Jun 12 at 10:15
  • Can you please add some context to your request? – Jonathan Ribas Jun 12 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 at 10:50
0

Vendor/Module/etc/di.xml

<?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">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="quoteGridAssignSalesrep" xsi:type="object">Vendor\Module\Console\Command\CategorySave</item>
            </argument>
        </arguments>
    </type>
</config>

Vendor\Module\Console\Command\CategorySave.php

<?php

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;
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('command_name')
            ->setDescription('The description of you command here!');

        parent::configure();
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        try{
        $this->appState->setAreaCode('global');
    }catch (\Exception $e){
        $e->getMessage();
    }
    try{
    $categoryCollection = $this->categoryFactory->create()->getCollection();
    foreach($categoryCollection as $category)
    {
        $category->save();
        $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
0

I created an extension which creates random categories via console command

This is the fastest method I could find

https://github.com/DominicWatts/Faker/blob/master/Console/Command/Category.php#L61

https://github.com/DominicWatts/Faker/blob/master/Helper/Category.php#L46-L70

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

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.