如何通过安装脚本将类别添加到Magento? [英] How to add a category to Magento via Setup script?
问题描述
我实际上可以通过安装脚本添加类别,由于某种原因,某些字段设置不正确.这是我的代码
I actually can add a category via setup script, the thing is for some reason some of the fields doesn't get set properly. Here's is my code
$this->startSetup();
Mage::register('isSecureArea', 1);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setName('Category Name')
->setUrlKey('category-name')
->setIsActive(0)
->setIncludeInMenu(1)
->setInfinitescroll(1)
->setDisplayMode('PAGE')
->setLandingPage($idToCmsBlock)
->setPageLayout('anotherLayoutThanDefault')
->setCustomUseParentSettings(0)
->setCustomLayoutUpdate('<reference name="head"><action method="addCss"><stylesheet>css/somecss.css</stylesheet></action></reference>')
->save();
$this->endSetup();
运行此脚本后,将创建一个类别,并在EAVs表中设置所有值. 但是,即使重新索引平面表,平面表也将缺少displayMode,landingPage,pageLayout,customLayoutUpdate.
After running this script, I have a category created with all my value set in the EAVs table. However the Flat table will be missing displayMode, landingPage, pageLayout, customLayoutUpdate even if I re-index the flat table.
奇怪的是,如果我进入管理员,则可以看到所有这些字段均已正确设置,但是如果我进入前端,则这些字段中的大多数都将被忽略.我将不得不去管理员,取消设置这些值,然后重新设置它们以使它们每个都正常工作.
The weird thing is that if I go in the admin, I can see all those fields properly set but if I go in my frontend most of those fields are ignored. I will have to go to the admin, unset those value and reset them for each of them to work properly.
也可以说我使用setEnabled(1),我的类别将在管理员中启用",但不会显示在前端.
Also let say I use setEnabled(1), my category will be "enable" in the admin but not show up in the frontend.
PS:我激活了平面类别",如果禁用它,它似乎可以正常工作,但是如果我重新编制索引,它仍然不起作用.
PS: I have Flat Category activated, if I disable it seems to work fine but if I re-index it still not working.
推荐答案
我终于找到了它,我不确定为什么,但是这些字段未正确显示,因为它们是为默认商店(storeId = 1)插入的,因为我的脚本正在更新脚本中运行.您需要使用storeId 0.
I finally found it, I'm not sure why but those fields are not showing up properly because they were inserted for the default store (storeId=1) because my script is running in an update script. You need to use the storeId 0.
有了这些信息,您会认为解决方案将类似于:
With this information you would think that the solution would be something like :
$this->startSetup();
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
但是此代码也不起作用.确实,在查看了Mage :: app()(Mage_Core_Model_App第804行)之后,我注意到了一个IF条件,如果您使用安装脚本,该条件将始终返回默认存储.
But this code doesn't work either. Indeed after looking into Mage::app() (Mage_Core_Model_App Line 804) I noticed a IF condition that would always return the default store if you're in a setup script.
诀窍是假冒您不在安装脚本中,我的有效解决方案是:
The trick is to fake that you're not in a setup script, my working solution is:
$this->startSetup();
Mage::register('isSecureArea', 1);
// Force the store to be admin
Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
这篇关于如何通过安装脚本将类别添加到Magento?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!