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);
$this->categoryRepositoryInterface->save($category);
// via model (deprecated)
/* Magento\Catalog\Model\Category; */
$category = $this->categoryModel->load($categoryId);
$category->save();
// via factory
/* Magento\Catalog\Model\CategoryFactory; */
$category = $this->categoryFactory->create()->load($categoryId);
$category->save();
I also tried to explicitly set the admin store id when I call the category:
$category = $this->categoryRepositoryInterface->get($categoryId, 0);
$this->categoryRepositoryInterface->save($category);
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->setStoreId($myDefaultStoreId);
$category->setData($defaultAttribute, null);
$this->categoryRepositoryInterface->save($category);
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);
$category->setStoreId($myDefaultStoreId);
$categoryData = $category->getData();
$categoryData["use_default"] = $defaultValues;
$this->categoryRepositoryInterface->save($category);
This also works but it has all the problem of the previous solution so I cannot really use it.