Tag Archives: Magento tutorials online

Magento custom admin module 404 after security patch installation


Magento custom admin module 404 after security patch installation :


This error occurs when you install security patch. Because security path overrides the method is_allowed which return false.

Add the following method in your controller to override the default method to fix the 404 error.

protected function _isAllowed()
    {
        return true;
    }

Which will solve your problem.

Magento set default value for config defined value


Magento set default value for config defined value


Add the following code in your config.php file under the tag.


    
                        
            My Default Value         
              
    


Example :

system.xml file



    
        
            
            309
        
    
        

        
                
                352
                1
                1
                1
                Your_tab
                
                
					
					
                    12
                    1
                    1
                    1
                    
                        
                            
                            text
                            1
                            1
                            1
                            1
			    Your settings.
                        
                    

                
                
            
         

  
		

For Setting The default value of the field : demo_field you have to add following code in config.xml



	
		
			0.1.0
		
	
        
			
				YourCompany_Your_module_Helper
			
        
      
       
        
            
                
                    
                        
                            
                                
                                 
                                        
                                        101
                                  
                                
                            
                        
                    
                 
            
        
     
     
    
    
                        
            1         
              
     



Which will look with default value 1 as :

magento set default value for config defined value

magento set default value for config defined value

Magento admin yes no in system.xml

You can add yes/ no dropdown in magento admin by adding simply the following code in system.xml file:

       
                
                            
                            select
                            adminhtml/system_config_source_yesno
                            1
                            1
                            1
                            1
			     Your comment.
                 
         

Which will generate the select box like this –

Magento admin select yes/ no dropdown

Magento admin select yes/ no dropdown

Magento redirect from phtml or model or helper or block or observer


Magento redirect from phtml or model or helper or block or observer : Mage::app()->getResponse()->setRedirect() is used for redirecting from phtml in magento.


Note : $this->_redirect(”); is used in controller , it will not work in phtml.

Magento redirect from phtml or model or helper or block or observer Example

Example :


Mage::app()->getResponse()->setRedirect('checkout/onepage');

or


Mage::app()->getResponse()->setRedirect('checkout/onepage');
exit;

Magento set Order() not working

Magento set Order() not working

An alternate way to sort magento collection is

$collection = Mage::getModel('test/test')
->getCollection()
->getSelect()
->order('id DESC');

This will sort the magento collection in descending order by id.

Magento add order by to collection

Magento add order by to collection : You can simply add the order in magento collection
by adding the –>setOrder(field_name, ASC_DESC); as below

$collection = Mage::getModel('test/test')
->getCollection()
->setOrder('id', 'DESC');

The above example will sort the collection in descending order by id.

Magento set order for grid collection

Magento set order for grid collection :

You simply add order in magento admin grid collection.
Go to your grid.php file and change order in construct.

setId('YourGridId');
        $this->setDefaultSort('id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }

}
?>

You can add sorting as per your need by Changing this –
$this->setDefaultSort(‘id’);
$this->setDefaultDir(‘DESC’);

How to connect to another database in magento

How to connect to another database in magento :

For Connecting to another database rather than using the current database you need to define new resource in config.php and tell magento to use this resource.

Config.php using External Database



    
        
            0.1.1
        
    
    
        
            
                
                    anotherexternal_database
                
            
            
                
                    anotherexternal_database
                
            
            
                
                    core_setup
                
            
            
                
                    
                    
                    
                    
                    mysql4
                    pdo_mysql
                    1
                
            
        
    


Now you can access Database as below :

Access Database

getConnection('anotherexternaldb_read');
    $data   = $connection->query("SELECT * FROM customers");
 
	


?>

Magento get current category id

Magento get current category id
You can get current categeory id as below :

Magento get current category id

$layer = Mage::getSingleton('catalog/layer');
$Category = $layer->getCurrentCategory();
$currentCatId= $Category ->getId(); //will give current categeory id