Magento: set config values of just created website?

Magento: set config values of just created website?

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: set config values of just created website?

Magento: Setting Config Values for Just-Created Websites

When programmatically creating websites or users in Magento, you’ll eventually need to set configuration values for these new entities. However, you might encounter an issue where the configuration values cannot be immediately saved after creating a website.

The Configuration Problem

The following code snippet demonstrates the common approach and the problem:

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

/* Re-init 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();

The issue arises when you try to save the configuration immediately after creating the website. The reinitStores() function is called, which should update the store data, but the configuration values still cannot be saved correctly.

The Solution

To resolve this issue, you need to understand that Magento’s configuration model operates using a cache mechanism for performance optimization. When you create a new website, it isn’t immediately added to the website cache until after a page load or a specific cache flush event occurs.

Here are two possible solutions:

1. Flush the Cache Manually

After creating and saving the website, manually flush the relevant Magento caches. You can do this using the following code segment:

Mage::app()->getCacheInstance()->cleanType(array('config'));

This line of code explicitly cleans the configuration cache type, ensuring that the newly created website is available for setting configuration values.

2. Refresh the Website Object

Instead of calling reinitStores(), which refreshes all store data, you can reload only the specific website object to update its status. This can be achieved as follows:

$website->refresh();

By reloading the freshly created website object, Magento will ensure that it is properly in sync with the current state of the system.

Conclusion

Setting configuration values for just-created websites in Magento requires a proper understanding and handling of the cache mechanism. Whether you flush the cache manually or refresh the specific website object, ensuring the update of the relevant data is crucial to prevent the issue described.

Remember that these solutions should be applied after saving the newly created website but before attempting to set its configuration values. It’s also important to handle exceptions and edge cases appropriately when working with Magento programmatically.

For more detailed information on Magento configuration management and best practices, I recommend visiting Laravel Company.

Admin Configuration Orders Errors

Our network