Category Archives: Magento

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

Magento – Set and Get Session Variable

Magento – Set and Get Session Variable

You can set magento session variable as follow :

Magento Set Session

Mage::getSingleton('core/session')->setYourVariableName('data');

You can get magento session data as follow:

Magento get Session

Mage::getSingleton('core/session')->getYourVariableName();

You can unset magento session variable as :

Magento Unset Session

Mage::getSingleton('core/session')->unsYourVariableName();

Magento collection delete all

Magento collection delete all. | Truncate magento table | Delete All Items from magento collection

You Can delete magento collection items as below.

         $collection    = Mage::getModel('yourcompany_yourmodule/yourmodel')->getCollection();    
         foreach( $collection as $item ){
                      $item->delete();
                }



Note : It will delete all the data from the collection.