2

Magento CE 1.9

I'm having some trouble with an exception being thrown and I'm not sure why.

Background

I have some code that is running on Cron which updates the attributes of one or more products if a table in the database says there has been an update.

The problem

The code that is run is going through each website and store of the product setting the attributes to their new values and when it's done it saves the product

$product->save()

The exception is thrown after saving:

PHP Fatal error:  Call to a member function getRootCategoryId() on a non-object in       /home/project/workspace/project/www/ecom/app/code/core/Mage/Catalog/Model/Resource/Url.php on line 1122
PHP Stack trace:
PHP   1. {main}()  /home/project/workspace/project/www/ecom/shell/UpdateScript.php:0
PHP   2. Custom_Module_Model_Observer->importObjects() /home/project/workspace/project/www/ecom/shell/UpdateScript.php:43
PHP   3. Custom_Module_Model_Handler_Product->execute($object = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/local/Custom/Module/Model/Observer.php:26
PHP   4. Custom_Module_Helper_Product->update($productId = *uninitialized*, $attributes = *uninitialized*, $websiteIds = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/local/Custom/Module/Model/Handler/Product.php:46
PHP   5. Mage_Core_Model_Abstract->save() /home/project/workspace/project/www/ecom/app/code/local/Custom/Module/Helper/Product.php:37
PHP   6. Mage_Core_Model_Resource_Abstract->commit() /home/project/workspace/project/www/ecom/app/code/core/Mage/Core/Model/Abstract.php:322
PHP   7. call_user_func:{/home/project/workspace/project/www/ecom/app/code/core/Mage/Core/Model/Resource/Abstract.php:110}(*uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Core/Model/Resource/Abstract.php:110
PHP   8. Mage_Catalog_Model_Product->afterCommitCallback() /home/project/workspace/project/www/ecom/app/code/core/Mage/Core/Model/Resource/Abstract.php:110
PHP   9. Mage_Index_Model_Indexer->processEntityAction($entity = *uninitialized*, $entityType = *uninitialized*, $eventType = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Catalog/Model/Product.php:2087
PHP  10. Mage_Index_Model_Indexer->indexEvent($event = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Indexer.php:317
PHP  11. Mage_Index_Model_Indexer->_runAll($method = *uninitialized*, $args = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Indexer.php:246
PHP  12. call_user_func_array:{/home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Indexer.php:378}(*uninitialized*, *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Indexer.php:378
PHP  13. Mage_Index_Model_Process->safeProcessEvent($event = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Indexer.php:378
PHP  14. Mage_Index_Model_Process->processEvent($event = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Process.php:621
PHP  15. Mage_Index_Model_Indexer_Abstract->processEvent($event = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Process.php:284
PHP  16. Mage_Catalog_Model_Indexer_Url->_processEvent($event = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Index/Model/Indexer/Abstract.php:103
PHP  17. Mage_Catalog_Model_Url->clearStoreInvalidRewrites($storeId = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Catalog/Model/Indexer/Url.php:235
PHP  18. Mage_Catalog_Model_Url->clearStoreInvalidRewrites($storeId = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Catalog/Model/Url.php:591
PHP  19. Mage_Catalog_Model_Resource_Url->clearStoreInvalidRewrites($storeId = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Catalog/Model/Url.php:596
PHP  20. Mage_Catalog_Model_Resource_Url->clearStoreCategoriesInvalidRewrites($storeId = *uninitialized*) /home/project/workspace/project/www/ecom/app/code/core/Mage/Catalog/Model/Resource/Url.php:1194

What i've tried

I've tried to find out what is going wrong.

As seen in the exception things go to shit in core/Mage/Catalog/Model/Resource/Url.php on line 1194. But while debugging I found out that the exception actually happends on line 1122. What seems to happen is that $storeId is 0 (null) which makes $store = $this->getStores($storeId) return an array of stores. This causes the following row to throw an exception because $store is an array and $store->getRootCategoryId() is not valid.

$store          = $this->getStores($storeId);    //$storeId = null. Returns array of stores.
$rootCategoryId = $store->getRootCategoryId();   //Fails because $store is not an object

I'd really appreciate some guidance. Thanks.

1
  • Your Question Solved my Issue – Ketan Borada Dec 7 '16 at 11:13
1

you have to add below line before your code it will set admin store for your code and your code will get store id of admin store and your problem will solve

 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
1
1

For getting Root Category Id, use below code:

Sol 1:

Mage::app()->getStore($storeId)->getRootCategoryId(); //The result was ID: 2

Sol 2:

Mage::app()->getStore()->getRootCategoryId(); //The result was ID: 2

Sol 3:

$store = Mage::getModel('core/store')->load(Mage_Core_Model_App::DISTRO_STORE_ID);
$categoryId = $store->getRootCategoryId();// The result was again ID: 2

The method that only worked and returned ID: 1 is given below and was taken from here

public function getRootCategoryId()
{
    $categories = Mage::getModel('catalog/category')->getCollection();
    $categIds = $categories->getAllIds();
    asort($categIds);
    foreach ($categIds as $k => $catId)
    {
        $category = Mage::getModel('catalog/category')->load($catId);
        if ($category->name)
        {
            return $catId;
        }
    }
}

Hope this helps!

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.