·

photo credit: Daniel Kulinski via photopin cc

Change Magento configuration on the fly

Sometimes you have a situation where you need to change the configuration of Magento on the fly. Just for your single request, without saving it. This can be easily done with the Magento configuration Model:

If you want to change a configuration value on the fly on store-level, you can do this as follows:

Why would you want to do this?

There are numerous reasons why you would want to change configuration on the fly. For example: recently, I had the situation where I was creating unit tests to test some specific functionality of a custom Magento module we’re creating for one of our clients.

For this test, we had to do some major queries on our database, and I had to check if the queries were returning the correct results. Since a unit test is designed to run periodically I couldn’t test against live data, so I had to create some dummy tables with test data from which I could predict the expected results.

In the SQL-queries I used the Magento-way to fetch the table name:

In the configuration of my module I had the tablename declared like so:

So this would render the above SQL-query to this:

Now in my unit test I added the following line:

So this would render the earlier SQL-query for my unit tests to this:

And that’s it! With this simple line I could test the code against a test-table without having to worry about the actual development or live data.

You can read a more detailed article about above method of unit testing in Magento in this article.

Visitors give this article an average rating of 5.0 out of 5.

How would you rate this article?

Leave a Reply