gpt4 book ai didi

php - 如何通过安装脚本向 Magento 添加类别?

转载 作者:可可西里 更新时间:2023-11-01 13:35:49 25 4
gpt4 key购买 nike

我实际上可以通过设置脚本添加一个类别,问题是由于某些原因某些字段没有正确设置。这是我的代码

$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。

奇怪的是,如果我进入管理员,我可以看到所有这些字段都已正确设置,但如果我进入我的前端,大多数这些字段都被忽略了。我将不得不去管理员那里,取消设置这些值并重新设置它们以使它们中的每一个都能正常工作。

另外假设我使用 setEnabled(1),我的类别将在管理员中“启用”但不会显示在前端。

PS:我激活了 Flat Category,如果我禁用它似乎工作正常但如果我重新索引它仍然无法工作。

最佳答案

我终于找到了,但我不确定为什么这些字段没有正确显示,因为它们是为默认商店 (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();

但是这段代码也不起作用。事实上,在查看 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();

关于php - 如何通过安装脚本向 Magento 添加类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com