How add category programmatically magento 2.3?

by user2710761   Last Updated July 12, 2019 13:09 PM - source

I would like to create categories programmatically in magento 2.3

Can you help me to provide us code?

Thanks in advance!

Tags : magento2


Answers 1


You can use following code to create category programmatically:

$category = $objectManager->create('Magento\Catalog\Model\Category', ['data' =>[
    "id" => 5,
    "parent_id" => 2,
    "name" => "cat2",
    "is_active" => true,
    "position" => 2,
    "include_in_menu" => false,
]);

$category->setCustomAttributes([
    "display_mode"=> "PRODUCTS",
    "is_anchor"=> "1",
    "custom_use_parent_settings"=> "0",
    "custom_apply_to_products"=> "0",
    "url_key"=> "cat2",
    "url_path"=> "cat2",
    "automatic_sorting"=> "0",
    'new_attribute' => 'value' // <-- your attribute
]);

$repository = $objectManager->get(CategoryRepositoryInterface::class);
$repository->save($category);
Faisal Sheikh
Faisal Sheikh
July 12, 2019 12:32 PM

Related Questions