I have a magento 2.3 with 4 websites, each website has one store view.

When I save a category via backend everything works as expected, but when I save a category via code, the default store view gets the "use default value" checkbox unchecked. This happens ONLY on the default store view (the other store views keep their checkboxes checked).

I've looked directly in the database and here all my rows have store_id = 0, meaning that there are no store-specific rows.

This is the code I've tried so far:

// via interface
/* Magento\Catalog\Api\CategoryRepositoryInterface; */
$category = $this->categoryRepositoryInterface->get($categoryId);

// via model (deprecated)
/* Magento\Catalog\Model\Category; */
$category = $this->categoryModel->load($categoryId);

// via factory
/* Magento\Catalog\Model\CategoryFactory; */
$category = $this->categoryFactory->create()->load($categoryId);

I also tried to explicitly set the admin store id when I call the category:

$category = $this->categoryRepositoryInterface->get($categoryId, 0);

but still the "use default value" option gets unchecked on the default store only.

How can I save the categories so that the checkboxes stays checked even in the default store view? Thanks

Edit to add:

I also tried this code:

$category = $this->categoryRepositoryInterface->get($categoryId, $myDefaultStoreId);
$category->setData($defaultAttribute, null);

This works: if I save via code and then check via backend, I can see that the "use default value" checkbox for that particular attribute has been kept. Unfortunately I don't know which attributes have the checkbox option so I cannot use this method. More over, this method requires to save the category twice (once for all the store views and a second time for the default store view) so it's not a good solution.

I also tried to manually set the checkboxes:

$category = $this->categoryRepositoryInterface->get($categoryId, $myDefaultStoreId);
$categoryData = $category->getData();
$categoryData["use_default"] = $defaultValues;

This also works but it has all the problem of the previous solution so I cannot really use it.

  • you're lucky you only get it on code - every time I change things on some categories I end up having to go in the store view and tick all the "default" boxes and save it again. It's ridiculous.
    – iphigenie
    Commented Mar 27, 2023 at 8:10
  • unfortunately it happens in backend too, and in the 2.4 versions as well.. everytime we save our products, all the default checkboxes in the store views gets deselected.. very annoying
    – Lou Nik
    Commented Apr 6, 2023 at 8:59

1 Answer 1


Not a solutions, but it's a place to start. I think there is an inconsistency in the category repository between get and save.
I suspect that you load the category with the store view scope 0 (admin) but save it with the store scope 1.

So the get method accepts a parameter with the store id, as your last piece of code.
But the save method completely ignores this.

public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
    $storeId = (int)$this->storeManager->getStore()->getId();

So the store id is take from the context, no matter the data set on your category object.
And it gets worse...In order to save the date, the save method loads again all data in the scope of the store view determined on the first line in the method.

$category = $this->get($category->getId(), $storeId);

I think, in order to make this work you should somehow do your operations inside the admin store view scope so $this->storeManager->getStore()->getId() would return 0.

You can try something like this (I don't guarantee success).

$category = $this->categoryRepositoryInterface->get($categoryId, 0);
//do your changes here
$currentStoreId = $this->storeManager->getStore()->getId(); //get the current scope
$this->storeManager->setCurrentStore(0); //set the scope as admin
$this->storeManager->setCurrentStore($currentStoreId); //set the scope back to what it was

Just make sure you add the storeManager as a dependency to your class.

  • Thank you @Marius, I didn't think about the store manager but unfortunately it doesn't work, the checkboxes are still unckecked only for the default store view. If I explicitly set the store_id to my default store view and then set the values to null, then the save keeps the checkboxes but I don't know beforehand which checkboxes are checked so I cannot really use this method. I also noted that by manually added the "use_default" array to my category data I can keep the checkboxes but, again, I don't know them so I need the default store view to behave like the others
    – Lou Nik
    Commented May 24, 2021 at 16:36
  • I have updated my original posts with the tests I mentioned in my reply
    – Lou Nik
    Commented May 24, 2021 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.