我实际上可以通过设置脚本添加一个类别,因为某些原因,某些字段没有正确设置.这是我的代码
$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();
Run Code Online (Sandbox Code Playgroud)
运行此脚本后,我创建了一个类别,其中包含我在EAVs表中设置的所有值.但是,即使我重新索引平面表,Flat表也会缺少displayMode,landingPage,pageLayout,customLayoutUpdate.
奇怪的是,如果我进入管理员,我可以看到所有这些字段正确设置,但如果我进入我的前端,大多数这些字段都会被忽略.我将不得不去管理员,取消设置这些值并为每个值重置它们以使其正常工作.
另外假设我使用setEnabled(1),我的类别将在管理员中"启用"但不会显示在前端.
PS:我有Flat Category激活,如果我禁用它似乎工作正常,但如果我重新索引它仍然无法正常工作.
zza*_*rbi 10
我终于找到了它,我不知道为什么但是这些字段没有正确显示,因为它们是为默认存储(storeId = 1)插入的,因为我的脚本在更新脚本中运行.您需要使用storeId 0.
有了这些信息,你会认为解决方案是这样的:
$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();
Run Code Online (Sandbox Code Playgroud)
但是这段代码也不起作用.确实在查看了Mage :: app()(Mage_Core_Model_App第804行)之后,我注意到一个IF条件,如果你在安装脚本中,它将始终返回默认存储.
诀窍是伪造你不在安装脚本中,我的工作解决方案是:
$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();
Run Code Online (Sandbox Code Playgroud)
通过数据安装脚本更新类别时遇到了同样的问题.接受的答案中提供的解决方案确实可用于更新类别,但可以通过以下方式进行改进:
我最终得到了以下用于更新类别的数据安装脚本(在此示例中,类别按名称加载,之后名称已更新):
<?php
$this->startSetup();
//Switch to admin store (workaround to successfully save a category)
$originalStoreId = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
//update category
$category = Mage::getModel('catalog/category')
->loadByAttribute('name', 'OLD_CATEGORY_NAME');
if ($category) {
$category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('NEW_CATEGORY_NAME')
->save();
}
//Set store to original value
Mage::app()->setCurrentStore($originalStoreId);
$this->endSetup();
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11431 次 |
| 最近记录: |