Automatically change store based on user location magento

Magento has the inbuilt facility to set up multistore functionality in the admin side.It can be managed from the front end by a store switcher or language switcher.But what if we needed to be automatically change store based on user location, Its simple though we need an extension for this to be done and some code to be run when the magento get loads with the help of the magento observers.

So first of all we need the extension named GeoIP which is free now and we can get that from Magento Connect Once the extension is installed activate them

Then we need to add the event observer in the xml file as shown below


 <events>
       <controller_action_predispatch> <!-- identifier of the event we want to catch -->
        <observers>
          <controller_action_predispatch_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>storeswitcher/observer</class> <!-- observers class alias -->
            <method>getLocationInfoByIp</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </controller_action_predispatch_handler>
        </observers>
      </controller_action_predispatch>
    </events>

And in the observer we need to set the store based on the location


class Naviz_StoreSwitcher_Model_Observer
{

	public function getLocationInfoByIp(Varien_Event_Observer $observer) {
			$geoIP = Mage::getSingleton('geoip/country');
        	$cnCode =  $geoIP->getCountry();
			switch ($cnCode) {
				case "US": {
					  Mage::app()->setCurrentStore('default');
					  break;
				}
				case "IN": {
					Mage::app()->setCurrentStore('lca');
					break;
				}
				case "CA": {
					Mage::app()->setCurrentStore('lca');
					break;
				}
				default: {
					Mage::app()->setCurrentStore('default');
					break;
				}

		}
 	}

}

Here lca,default are the two different store in which lca is for canada and default for all other countries

Download Source Code

Naviz_StoreSwitcher-v1

Map integer to text string in mysql

We can use the functions CASE .. WHEN ... to map integer to text string in mysql

The example query is as shown below

SELECT *, CASE type WHEN 1 THEN 'Enabled' WHEN 2 THEN 'Disabled' END as status
FROM users

You can also use the ELSE clause to specify a default,
e.g.

CASE type WHEN 1 THEN 'Enabled' WHEN 2 THEN 'Disabled' ELSE 'Pending' END.

Delete all categories in Magento

If you have been working on a demo store – or yours is still in the early stages of construction, you will probably find it necessary to remove all the categories to start fresh at some point

So here is the code to delete all categories in magento, Run these queries in database

TRUNCATE TABLE `catalog_category_entity`;
    TRUNCATE TABLE `catalog_category_entity_datetime`;
    TRUNCATE TABLE `catalog_category_entity_decimal`;
    TRUNCATE TABLE `catalog_category_entity_int`;
    TRUNCATE TABLE `catalog_category_entity_text`;
    TRUNCATE TABLE `catalog_category_entity_varchar`;
    TRUNCATE TABLE `catalog_category_product`;
    TRUNCATE TABLE `catalog_category_product_index`;
 
    insert  into `catalog_category_entity`(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`position`,`level`,`children_count`) values (1,3,0,0,'0000-00-00 00:00:00','2009-02-20 00:25:34','1',1,0,1),(2,3,3,0,'2009-02-20 00:25:34','2009-02-20 00:25:34','1/2',1,1,0);
    insert  into `catalog_category_entity_int`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,32,0,2,1),(2,3,32,1,2,1);
    insert  into `catalog_category_entity_varchar`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,31,0,1,'Root Catalog'),(2,3,33,0,1,'root-catalog'),(3,3,31,0,2,'Default Category'),(4,3,39,0,2,'PRODUCTS'),(5,3,33,0,2,'default-category');

Detailed documentation can be found at Magento Wiki

Find and Replace with MySQL

— 1st example
UPDATE files SET filepath = REPLACE(filepath,’path/to/search’,’path/to/replace’);

— 2nd example
UPDATE customers SET address = REPLACE(address,’_CODE_’,postcode);