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

How to add a category to Magento via Setup script?(如何通过安装脚本向 Magento 添加类别?)
本文介绍了如何通过安装脚本向 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 添加类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)