Remove store level value for category attribute
-
13-12-2019 - |
Question
On our shop we have a customer category attribute that has been set against each store. Now we want to remove the store level values and replace it with one global level. Now I know how to do this via the admin but is there a way of sorting this via a setup script.
Solution
I think you can replicate the behavior from the admin category controller, saveAction
.
if ($useDefaults = $this->getRequest()->getPost('use_default')) {
foreach ($useDefaults as $attributeCode) {
$category->setData($attributeCode, false);
}
}
But this requires a load
in a loop. Actually 2 loops.
Something like this (untested code)
$attributeCode = 'attribute_code_here';
$collection = Mage::getModel('catalog/category')->getCollection()->addLevelFilter(2); //exclude the root of all roots and the root catalog.
$stores = Mage::getModel('core/store')->getCollection()->setWithoutDefaultFilter();
foreach ($collection as $category) {
foreach ($stores as $store) {
$_category = Mage::getModel('catalog/category')->setStoreId($store->getId())->load($category->getId());
$_category->setData($attributeCode, false);
$_category->save();
}
}
I know it's not the best solution but since this will run only once it shouldn't be that much trouble.
I still say you should use a query. It's way faster.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange