Change magento package and theme programatically from admin

When we are dealing with magento sometimes we need to change the package and theme of the front end,From front end its easy to do as shown in stackoverflow

Mage::getDesign()->setArea('frontend') //Area (frontend|adminhtml)
                    ->setPackageName('default') //Name of Package
                    ->setTheme($themeName); // Name of theme

But this code doesnt works from admin section, So we need to use the functionality of the magento’s config section
ie the code snippet will be like this

$groups['theme']['fields']['template']['value'] = 'template_name';
$groups['theme']['fields']['skin']['value'] = 'skin_name';
$groups['theme']['fields']['layout']['value'] = 'layout_name';
$groups['theme']['fields']['default']['value'] = 'template_name';
Mage::getModel('adminhtml/config_data')  
      ->setSection('design')  
      ->setWebsite('your_website')  
      ->setGroups($groups)  
      ->save(); 

This code is helpful when we need to create an theme manager for users in admin in which it can be made similiar to the themes section as we seen on wordpress

Hope this helps to change magento package and theme programatically from admin

Add States or Region to a Country in Magento

By default Magento consists of all the countries and doesn’t have the states for all these countries. Unfortunately we didnt have these options in admin to do add states or region. So to add states or region to a country in magento we have to do some tweaks to the database,ie adding the states to the database

We have two tables in database which stores the region details

  1. directory_country_region
  2. directory_country_region_name

When we add record to directory_country_region it will enable the selectbox for the country we have added and we have the options to make the region required too.
Then we have to add the desired regions to the second table directory_country_region_name

Here is the code to add regions for India

We can run this code outside magento installation by using the code given below

<?php
require_once 'app/Mage.php';
Mage::init();
//Add the code to excecute
?>
// set up array containing regions to be added (region_code => region_name)
$new_regions = array(
	'Kerala' => "Kerala",
	'Karnataka' => "Karnataka",
);
 
// specify country code for new regions
$country_code = 'IN';
 
// specify locale
$locale = 'en_US';
 
// create our core_write conection object
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
 
// iterate our new regions
foreach ($new_regions as $region_code => $region_name) {
 
	// insert region 
	$sql = "INSERT INTO `directory_country_region` (`region_id`,`country_id`,`code`,`default_name`) VALUES (NULL,?,?,?)";
	$connection->query($sql,array($country_code,$region_code,$region_name));
 
	// get new region id for next query
	$region_id = $connection->lastInsertId();
 
	// insert region name
	$sql = "INSERT INTO `directory_country_region_name` (`locale`,`region_id`,`name`) VALUES (?,?,?)";
	$connection->query($sql,array($locale,$region_id,$region_name));
}

This code will add the regions we provided in the array to the magento ie we can add states or region to a country in magento with lot of ease

If you are not interested in writing codes there are extensions availbale in magento connect in which one of them is free
Regions Manager

Form validation on change in magento

In magento some times we need to validate some fields like username on its availability.
So in that case we need to run the validation function on an event change of the element
In order to do that we need to add an function for example onblur to the element as shown below

<input class="input-text required-entry" id="firstname" type="text" name="firstname" onblur="validate_field(this)"/>

and add the javascript function inside the script tag like below

function validate_field(id){
  Validation.validate(id);
}

Hope this post helps to add form validation on change in magento

Copy files from one folder to another using php

Inorder to copy files from one folder to another using php we can use the php’s inbuilt function called copy .Now we are creating an function in which we need only to specify the source directory and the destination directory


function copy_files($source,$destination) {
    $dir = opendir($src);
    @mkdir($dst);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($source. '/' . $file) ) {
               recurse_copy($source. '/' . $file,$destination. '/' . $file);
            }
            else {
                copy($source. '/' . $file,$destination. '/' . $file);
            }
        }
    }
}

Hope this code snippet helps somebody to copy files from one folder to another using php