0

I am trying to update the category using following code.

$category = $this->categoryFactory->create()->load($categoryId);

foreach($data as $d) {
    $category->setDataUsingMethod($d['code'], $d['value']);
}

$category->save();

$this->categoryFactory is object of \Magento\Catalog\Model\CategoryFactory

But the category is not getting updated.

I have checked after reindexation as well, but the category is showing the old data and not the updated data.

What is the issue here?

0

You have not instantiated $data there, and its better to use setData rather than setDataUsingMethod method. You can use the following code to get the result,

$category = $this->categoryFactory->create()->load($categoryId);

$data = $category->getData();

foreach($data as $key => $value) {
    $category->setData($key, $value);
}

$category->save();
0

The issue was because of missing store id.
I am able to update the category using following code.

$category = $this->categoryFactory->create()->load($categoryId);

$data = $category->getData();

foreach($data as $key => $value) {
    $category->setData($key, $value);
}

$category->setStoreId($storeId);
$category->save();

I am not sure at this point why it is necessary and if it is like this then why not Magento picking the admin store as the store id by default.

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.