2

I am using the following code to delete a category from my Magento installation programmatically:

        $hostId = $model->getId()->getHost();
        $category = \Mage::getModel('catalog/category')
            ->loadByAttribute('jtl_erp_id', $hostId);

        if ($category) {
            \Mage::register('isSecureArea', true);
            $category->delete();
            \Mage::unregister('isSecureArea');
        }

This works fine unless the flat category index is enabled. If this is the case, the following exception is being thrown:

Fatal error:  Call to undefined method Mage_Catalog_Model_Resource_Category_Flat::isForbiddenToDelete() in /path/to/magento/app/code/core/Mage/Catalog/Model/Category.php on line 755

Any ideas? I am using Magento 1.9.0.1

1

You are not supposed to delete categories outside the Admin scope.
You can just simulate the admin environment and it should work

$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation(Mage_Core_Model_App::ADMIN_STORE_ID);

//YOUR CODE GOES HERE

// end admin store emulation
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
| improve this answer | |
0

I faced the same issue when flat_categories were enabled.

You can load the normal category model instad of the flat category and delete it to get around this issue

Mage::register('isSecureArea', true);
$category = Mage::getModel('catalog/category', array('disable_flat' => true))->load($categoryId);
$categoy->delete();

Referance: disable flat category and load

| improve this answer | |

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.