以PHP编程方式设置Magento中的当前主目录CMS / Page [英] Programmatically set the current home CMS/Page in Magento in PHP

查看:327
本文介绍了以PHP编程方式设置Magento中的当前主目录CMS / Page的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决方案在PHP中以编程方式设置CMS主页在Magento。我正在寻找的是当您使用手动方法从系统 - >配置 - > Web-> CMS主页的下拉菜单中选择页面标题。



我已经有方法来确定选择页面标题集中的任何一个,或者我可以从商店中的所有CMS页面的URL URL集中选择。如果我知道页面标题或URL密钥,什么是PHP Mage方法需要更改当前CMS主页。



我已经把代码创建新的CMS页面。



感谢,


$

b $ b

Kyle



编辑:02/18/2012



  $ groups ['default'] ['fields'] ['cms_home_page'] ['value'] ='city-把手 - 把手

Mage :: getModel('adminhtml / config_data')
- > setSection('web')
- > setWebsite('Main Website')
- > setStore('My Store Name')// Hid实际商店名称
- > setGroups($ groups)
- > save

编辑:感谢您的截图。我没有那些权利。我选择商店时的网址是... / index.php / admin / system_config / edit / section / general / website / base / store / default /



假设意味着website = base,store = default





我将代码更改为:

  $ groups [' '] ['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主页选择的更改。我刷新前端,它不切换。我也试过清除缓存没有运气。有任何想法吗?只是FYI我运行Magento CE 1.5.1.0



编辑:



这是我运行的文件内容。我把文件放在我的Magento安装的根目录。

 < html> 
< head>
< / head>
< body>
<?php
require_once('app / Mage.php'); // Path to Magento
umask(0);
Mage :: app(); //将默认值更改为您要运行的任何商店

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')//代码是字段名
- > setStore('default')
- > setGroups($ groups)
- > save

print('CMS 3< br>');
?>
< / body>
< / html>

当我运行时,它是在浏览器窗口中看到:

  CMS 1 
CMS 2
CMS 3

编辑:
OK所以我找到了一个解决方案(工作原理),灵感来自最后提出的解决方案:

  require_once('app / Mage.php'); // Path to Magento 
umask(0);
Mage :: app(); //将默认值更改为要运行的任何存储库

$ Config = new Mage_Core_Model_Config();
$ Config - > saveConfig('web / default / cms_home_page',city-grips-handle-bar-covers);
Mage :: app() - > cleanCache();

配置缓存真的是所有需要清理(我手动确认)。所以清理所有的缓存是有点过分,但是它很好的工作。

解决方案

这将在默认存储上工作,并将CMS主页更改为您放入的任何内容。显然,具有此URL的CMS页面必须存在。如果指定不存在的话,您会在前端收到错误。

  require_once('app / Mage。 php'); // Path to Magento 
umask(0);
Mage :: app(); //将默认值更改为要运行的任何存储库

$ Config = new Mage_Core_Model_Config();
$ Config - > saveConfig('web / default / cms_home_page',< URL-Key-goes-here>);
Mage :: app() - > cleanCache();


I'm looking for a solution in PHP to programmatically set the CMS home page in Magento. What I'm looking for is what you get when you use the manual method of selecting a page title from the dropdown in the System->Configuration->Web->CMS Home Page.

I already have the methods to determine pick any one of the set of Page Titles or I can pick from the Set of URL keys for all the CMS pages in the store. If I know either the Page Title or the URL Key, what is the PHP Mage methods to needed to change the current CMS home page.

I have already figured the code to create a new CMS Page as well. I'd just like to be able to select it and make it actively the home page when I'm done.

Thanks,

Kyle

Edit: 02/18/2012

tried the following without success:

    $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();

EDIT: Thanks for the screenshot. I didn't have those right. My url when selecting the store is .../index.php/admin/system_config/edit/section/general/website/base/store/default/

Which I assume means website=base, store=default

I changed the code to:

    $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();

But I'm still not showing the CMS Home page selected change. I refresh the front end and it doesn't switch. I've also tried clearing cache with no luck. Any ideas? just FYI I'm running Magento CE 1.5.1.0

EDIT:

Here's the file contents that I run. I put the file at the root of my Magento Install.:

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

When I run it is see in the browser window:

CMS 1
CMS 2
CMS 3

EDIT: OK So I found a solution (that works) that was inspired by the last proposed solution:

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();

Voila it works. The configuration cache is really all that it needed to be cleaned (I confirmed this manually). So cleaning all the cache is a little overkill but oh well it works.

解决方案

Here it is. This works on the default store and will change the CMS Home Page to whatever you put in . Obviously a CMS page with this URL must exist. You'll get an error on the front end if you specify one that does not exist.

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', "<URL-Key-goes-here>");
Mage::app()->cleanCache();

这篇关于以PHP编程方式设置Magento中的当前主目录CMS / Page的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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