Magento:如何在升级脚本中获取特定商店的根类别ID?

时间:2017-01-30 19:47:35

标签: php magento magento-1.9

如何在Magento 1.9更新脚本中获取根类别?

Find root category of store提出了一个解决方案,但在运行脚本后收到错误消息:

Mage registry key "controller" already exists

......以及追踪:

#0 /var/www/instances/global/app/Mage.php(223): Mage::throwException('Mage registry k...')
#1 /var/www/instances/global/app/code/core/Mage/Core/Model/App.php(762): Mage::register('controller', Object(Mage_Core_Controller_Varien_Front))
#2 /var/www/instances/global/app/code/core/Mage/Core/Model/App.php(1113): Mage_Core_Model_App->_initFrontController()
#3 /var/www/instances/global/app/code/core/Mage/Core/Controller/Varien/Front.php(344): Mage_Core_Model_App->getFrontController()
#4 /var/www/instances/global/app/code/core/Mage/Core/Controller/Varien/Front.php(161): Mage_Core_Controller_Varien_Front->_checkBaseUrl(Object(Mage_Core_Controller_Request_Http))
#5 /var/www/instances/global/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#6 /var/www/instances/global/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#7 /var/www/instances/global/index.php(118): Mage::run('', 'store')
#8 {main}

我的脚本首先调用Mage :: init,然后尝试读取商店的根类别名称。

Mage::init();
Mage::app()->getStore(3)->getRootCategoryId();

错误信息似乎很常见; Mage registry key "controller" already exists表示Mage :: run可能已运行两次。

因此我假设Mage :: init也不应该被调用两次,并且可能在调用更新脚本后再次调用 - 这可能会导致我的错误消息。所有假设。

我该怎么办?省略Mage :: init()?但是,我没有得到根id。销毁由Mage :: init()创建的对象?怎么样?

3 个答案:

答案 0 :(得分:0)

我们有许多类型可以访问根类别ID。

1- Mage::app()->getStore($storeId)->getRootCategoryId();

2- Mage::app()->getStore()->getRootCategoryId();

3-

 $store = Mage::getModel('core/store')->load(Mage_Core_Model_App::DISTRO_STORE_ID);
        $categoryId = $store->getRootCategoryId();

答案 1 :(得分:0)

您在升级脚本(安装程序)中不需要Mage::init();Mage::run();

省略Mage::init();

后仍可获取root ID

答案 2 :(得分:0)

此代码不优雅,但不需要Mage :: init()调用,因此可以在Magento升级脚本中调用:

$myStoreId = 4;
$myStoresRootCategoryId = null;
foreach (Mage::app()->getStores() as $store) {
    $storeId = $store->getId();
    $rootCategoryId = $store->getGroup()->getRootCategoryId();
    if ($storeId == $myStoreId) {
        $myStoresRootCategoryId = $rootCategoryId;
    }
}