4

I'm programmtically creating websites/users etc ...

Here's the problem: When creating a website, I can't immediatly set the config values afterwards.

Code:

<?php
/* Website information */
$website_data = array(
            'name' => 'Company name',
            'code' => 'website_company_1',
            'sort_order' => '1',
            );

/* Save website */
$website = Mage::getModel('core/website'); 
$website->setData($website_data);
$website->save();

/* Get website code */
$web_code = $website->getCode();

/* R-int stores */
Mage::app()->reinitStores();

/* Config data array example */
$data = array('name' => 'Company 1', 'phone' => '056 22 33 61')

/* Set config values in array */
$groups = array();
 foreach($data as $key => $value){
 $groups['store_information']['fields'][$key]['value'] = $value;
 }


/* Save config values */
Mage::getModel('adminhtml/config_data')
      ->setSection('general')
      ->setWebsite($web_code)
      ->setStore(NULL)
      ->setGroups($groups)
      ->save();


/* Re-init again */
Mage::app()->reinitStores();

However, this doesn't work for some reason, but if I create a website first(with the same code), then execute this config-save function afterwards, it works fine. As if it needs a new page load first before it can set/update config values. I thought the re-init would solve this but it doesn't ...

Thoughts?

3
  • Additional details would help people debug this. Specifically, what's in $website_data and $data, and where are they getting populated from. Aug 30, 2010 at 20:38
  • Added some example code, since the actual code is way to much to put here. both are just associative arrays that work fine a website that was already created, just not on on that was created in the same script ...
    – Rakward
    Aug 31, 2010 at 6:36
  • I suspect there is some indexing going on here. Try turning on SQL logging (ref magentocommerce.com/boards/viewreply/77655) to trap any indexing or other activity after creating the website, then track down the code that is responsible for it, and replicate it in your code. Aug 31, 2010 at 6:41

2 Answers 2

7

You should use install/upgrade scripts for this purpose (these are the scripts inside modules' sql folders). You may even wish to create a setup-specific module with/in which to run these.

Simply declare a setup resource in your module's global/resources node and then create the file(s) you need to accomplish this. Use Mage_Core_Model_Resource_Setup or have your setup class extend from there.

See Mage_Core_Model_Resource_Setup::setConfigData() and Mage_Core_Model_Resource_Setup::deleteConfigData().

Mage_Core_Model_Resource_Setup::addConfigField() could also be used but is not implemented in the core from what I can tell.

<?xml version="1.0" ?>
<!-- module config.xml -->
<config>
    <modules>
        <Your_Module>
            <version>1.0</version>
            <!-- upgrade script #s evaluated with version_compare(), FYI -->
        </Your_Module>
    </modules>
    <global>
        <resources>
            <unique_node>
                <setup>
                    <!-- match node under <modules> -->
                    <module>Your_Module</module>
                    <class>Mage_Core_Model_Resource_Setup</class>
                </setup>
            </unique_node>
        </resources>
    </global>
</config>

Then in your install/upgrade scripts do this:

<?php

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */ //or whatever you configured

$installer->startSetup();

$installer->setConfigData($path, $value, $scope='default', $scopeId=0) //inherit is not implemented

$installer->endSetup();
0
5

You didn't mention which Magento version you are on. I have tested the below on 1.4.1.1 and made some changes so it is a running example.

The main difference is the change from

Mage::app()->reinitStores();

to

Mage::app()->getConfig()->reinit();

which re-loads the config while also reloading the cache.

Complete Example:

<?php

require_once 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
Mage::app();

/* Website information */
$website_data = array(
    'name' => 'Website Name',
    'code' => 'website_company',
    'sort_order' => '2',
    'is_active' => 1,
);

/* Save website */
$website = Mage::getModel('core/website');
$website->setData($website_data);
$website->save()->load();

/* Save store */
$storeGroup = Mage::getModel('core/store_group');
$storeGroup->setData(
        array(
            'root_category_id' => '3',
            'website_id' => $website->getId(),
            'name' => 'Store',
        )
);
$storeGroup->save()->load();

$store = Mage::getModel('core/store');
$store->setData(
        array(
            'website_id' => $website->getId(),
            'name' => $storeGroup->getName(),
            'code' => 'store_' . $website->load()->getId(),
            'group_id' => $storeGroup->getGroupId(),
            'is_active' => 1,
        )
);
$store->save()->load();

/* Re-init */
Mage::app()->getConfig()->reinit();

/* Config data array example */
$data = array('name' => 'Company 1', 'phone' => '056 22 33 61');

/* Set config values in array */
$groups = array();
foreach ($data as $key => $value) {
    $groups['store_information']['fields'][$key]['value'] = $value;
}

/* Save config values */
$data = Mage::getModel('adminhtml/config_data')
                ->setSection('general')
                ->setWebsite($website->getCode())
                ->setGroups($groups)
                ->save();
1
  • can you please tell me how to set secure and unsecure base link url for a store programmatically? Dec 7, 2012 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.