【发布时间】:2012-03-08 14:28:25
【问题描述】:
我正在寻找一种在 PHP 中以编程方式在 Magento 中设置 CMS 主页的解决方案。我正在寻找的是当您使用手动方法从 System->Configuration->Web->CMS Home Page 的下拉菜单中选择页面标题时得到的。
我已经有方法来确定选择一组页面标题中的任何一个,或者我可以从商店中所有 CMS 页面的 URL 键集中选择。如果我知道页面标题或 URL 键,那么更改当前 CMS 主页所需的 PHP Mage 方法是什么。
我已经想出了创建新 CMS 页面的代码。我只想在我完成后能够选择它并主动将其设为主页。
编辑:2012 年 2 月 18 日
尝试了以下方法但没有成功:
$groups['default']['fields']['cms_home_page']['value'] = 'city-grips-handle-bar-covers';
Mage::getModel('adminhtml/config_data')
->setSection('web')
->setWebsite('Main Website')
->setStore('My Store Name') // Hid Actual Store name here
->setGroups($groups)
->save();
编辑:感谢您的屏幕截图。我没有这些权利。我选择商店时的网址是 .../index.php/admin/system_config/edit/section/general/website/base/store/default/
我认为这意味着 website=base, store=default
(来源:reefworkshop.com)
我把代码改成:
$groups['default']['fields']['cms_home_page']['value'] = 'city-grips-handle-bar-covers';
Mage::getModel('adminhtml/config_data')
->setSection('web')
->setWebsite('base')
->setStore('default')
->setGroups($groups)
->save();
但我仍然没有显示 CMS 主页选择的更改。我刷新前端,它没有切换。我也试过清除缓存但没有运气。有任何想法吗?仅供参考,我正在运行 Magento CE 1.5.1.0
编辑:
这是我运行的文件内容。我把文件放在我的 Magento 安装的根目录下。:
<html>
<head>
</head>
<body>
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app(); // Change default to whatever store you want to run
print('CMS 1<br>');
$groups['default']['fields']['cms_home_page']['value'] = 'city-grips-handle-bar-covers';
print('CMS 2<br>');
Mage::getModel('adminhtml/config_data')
->setSection('web')
->setWebsite('base') // Code is the field name
->setStore('default')
->setGroups($groups)
->save();
print('CMS 3<br>');
?>
</body>
</html>
当我运行它时,它会在浏览器窗口中看到:
CMS 1
CMS 2
CMS 3
编辑: 好的,所以我找到了一个受上次提出的解决方案启发的解决方案(有效):
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app(); // Change default to whatever store you want to run
$Config = new Mage_Core_Model_Config();
$Config ->saveConfig('web/default/cms_home_page', "city-grips-handle-bar-covers");
Mage::app()->cleanCache();
瞧,它有效。配置缓存实际上就是它需要清理的所有内容(我手动确认了这一点)。所以清理所有的缓存有点过头了,但是很好用。
【问题讨论】:
-
你把这段代码放在哪里?你在
exception.log中看到了什么吗? -
异常日志中有一些内容。我不认为它相关:异常'Mage_Core_Exception'与消息'无效块类型:checkout_cart'在/chroot/home/babyhalf/babyhalfoff.com/html/app/Mage.php:550有28层堆栈转储,但我不认为刺激是相关的。它也重复了,我只运行了一次 CMS 页面代码。
标签: php html magento content-management-system