cancel
Showing results for 
Search instead for 
Did you mean: 

2.2 update category programmatically

2.2 update category programmatically

hello,

I know it's wrong to use object manager directly but in this case I'm forced. I'm successfully adding new categories with this code:

$collection = $objectManager->create('Magento\Catalog\Model\Category');
                $category->setName($name)
                    ->setIsActive($status)
                    ->setUrlKey($slug)
                    ->setParentId($parent_id))
                    ->setPath($collection->load($parent_id)->getPath())
                    ->setStoreId($storeId)
                    ->save();

but when I try to update some data, I don't receive any error but it's not saving:

$collection = $objectManager->create('Magento\Catalog\Model\Category');
$category = $collection->load($id, $storeId);
$category->setName($name)
                    ->setIsActive($status)
                    ->setParentId($collection->load($parent_id)->getPath())
                    ->setPath($rootCat->getPath())
                    ->setStoreId($storeId);
                try {
                    $category->save();
                }
                catch (Exception $e) {
                    echo $e->getMessage();
                }

any help?

thanks

1 REPLY 1

Re: 2.2 update category programmatically

Ok I've realized I have to use the repository to actually save existing data. But this modifies the data in the default store view, what if I want to modify existing data at "All store views" level?

thanks