cancel
Showing results for 
Search instead for 
Did you mean: 

how to add category using factory method not object manager

how to add category using factory method not object manager

I have created a input type text i want to grab the data from that input and save that as a category name . I have a template simply where i am creating form and input and submit button. and i am taking value of input in block . look at the code please you will understand it more clearly ..

app/code/Mastering/SampleModule/Block/Hello.php

   <?php 

namespace Mastering\SampleModule\Block;
use Magento\Framework\View\Element\Template;
use Mastering\SampleModule\Model\ResourceModel\Item\Collection;
use Mastering\SampleModule\Model\ResourceModel\Item\CollectionFactory;
use Magento\Framework\Catalog\CategoryFactory ;

class Hello extends Template {
    private $cf;
    private $messageManager;
    public function __construct(Template\Context $context , CollectionFactory $collectionfactory , \Magento\Framework\Message\ManagerInterface $messageManager , array $data = [] ){
        $this->cf = $collectionfactory ;
        parent::__construct($context,$data);
        //$this->messageManager=$messageManager;

    }
    public function getItemsResults(){
        return $this->cf->create()->getItems();
    }
    public function getFormData(){
        $post=$this->getRequest()->getPost();
        if(!empty($post)){
            $name=$post['name'];
            //$this->messageManager->addSuccessManager("data saved");
        }
    }
}
2 REPLIES 2

Re: how to add category using factory method not object manager

Hello @samair

 

Please follow as shared code:

 

class MyClass 
{

    protected $storeManager;

    protected $categoryFactory;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory
    ) {
        $this->storeManager = $storeManager;
        $this->categoryFactory = $categoryFactory;
    }

    public function fetchOrCreateProductCategory($categoryName)
    {
        // get the current stores root category
        $parentId = $this->storeManager->getStore()->getRootCategoryId();

        $parentCategory = $this->categoryFactory->create()->load($parentId);

        $category = $this->categoryFactory->create();
        $cate = $category->getCollection()
            ->addAttributeToFilter('name', $categoryName)
            ->getFirstItem();

        if (!$cate->getId()) {
            $category->setPath($parentCategory->getPath())
                ->setParentId($parentId)
                ->setName($categoryName)
                ->setIsActive(true);
            $category->save();
        }

        return $category;
    }
Manish Mittal
https://www.manishmittal.com/

Re: how to add category using factory method not object manager

Hello @samair

 

Try this

 

<?php

protected $_categoryFactory;

protected $_storeManager;

public function __construct(
	\Magento\Store\Model\StoreManagerInterface $storeManager,
	\Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
	$this->_storeManager = $storeManager;
	$this->_categoryFactory = $categoryFactory;
}

.....
.....

$storeId = $this->_storeManager->getStoreId();
$rootCategoryId = $this->_storeManager->getStore()->getRootCategoryId();
$rootCategory = $this->_categoryFactory->create()->load($rootCategoryId);

$category = $this->_categoryFactory->create();
$category->setName('category name');
$category->setIsActive(true);
$category->setUrlKey('category-url-key');
$category->setData('description', 'description');
$category->setParentId($rootCategoryId);
$category->setStoreId($storeId);
$category->setPath($rootCategory->getPath());
$category->save();
If you find my answer useful, Please click Kudos & Accept as Solution.