Programmatically set the current home CMS/Page in

2019-09-05 00:12发布

问题:

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.

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


(source: reefworkshop.com)

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.

回答1:

Try in this way:

        $groups['default']['fields']['cms_home_page']['value'] = '<your_cms_page_url_key>';

        Mage::getModel('adminhtml/config_data')
            ->setSection('web')
            ->setWebsite('<your_website_code>')
            ->setStore('<your_store_view_code>')
            ->setGroups($groups)
            ->save();

To quickly find out website code and store view code - go to System->Configuration, select needed store view from dropdown at the left and look at page url:

EDIT:

Another approach, works only in install script:

/** @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

/** @var $store Mage_Core_Model_Store */
$store = Mage::getModel('core/store');
$store->load('<needed_store_code_>');

$installer->setConfigData('web/default/cms_home_page','<your_cms_page_url_key>','stores',$store->getId())



回答2:

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