2

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.

1 Answer 1

4

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.

0

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.