如何通过安装脚本向 Magento 添加类别? [英] How to add a category to Magento via Setup script?

查看:22
本文介绍了如何通过安装脚本向 Magento 添加类别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 添加类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆