問題描述
我實際上可以通過設置腳本添加一個類別,但由于某種原因,某些字段沒有正確設置.這是我的代碼
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();
運行此腳本后,我創建了一個類別,其中設置了 EAV 表中的所有值.然而,即使我重新索引平面表,平面表也會缺少 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:我已激活 Flat Category,如果我禁用它似乎可以正常工作,但如果我重新索引它仍然無法正常工作.
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 添加類別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!