Tag Archives: Magento tutorials online

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.

Magento load model by field name

Magento load model by field name

You can load model in magento by field name ,column name as below


$model = Mage::getModel('yourcompany_yourmodule/modelname')->load($fieldvalue,'field_name'); 

or


$model = Mage::getModel('yourcompany_yourmodule/modelname');
$model = $model->load($columnvalue,'column_name'); 

Exception ‘Mage_Core_Exception’ with message ‘Invalid callback:

Exception ‘Mage_Core_Exception’ with message ‘Invalid callback:

This Error Occurs when company_module name is invalid and not added properly in model tab in cron.



    
        
            1.0.0
        
    

    
        
            
                Yourcompany_Yourmodule_Model_Observer
            
        
    

    
        
            
                
                    */15 * * * *
                
                
                    Yourmodule/observer::updateAll
                
            
        
    

Change it to the

 
        
            
                
                    */15 * * * *
                
                
                     yourcompany_yourmodule/observer::updateAll
                
            
        
    

Magento cron not working

Magento cron not working

Adding cron job in magento is very simple. You have to add only few lines of code to run and schedule the cron
job.

add the following lines of code in the config.xml


		
			
				
					*/15 * * * *
				
				
					modulename/observer::methodtocall
				
			
		



The Above cron will run after each 15 minutes.

If Cron job is not running please check the module name and cron observer or method you are calling.

Item with the same id already exist magento collection

item with the same id already exist magento collection

If Magento collection has duplicate id it throws the above exception.
To fix this problem make sure your collection do’nt have the duplicate records.
Check that the collection have single column with the name id(ie. in case of fetching data using joins there should be no duplicate column). Take Alias of the column if same in case of joins.

ex.

  
        $collection = Mage::getModel('test/user')->getCollection()->distinct()
            ->addFieldToSelect('name')
            ->addFieldToSelect('id');
        $collection->getSelect()->join( array('uc'=> user_profile), 'uc.user_id = main_table.id', array('uc.id'));

Should be
>addFieldToSelect(‘id’) field should have alias as ‘userId’ otherwise it will through the exception

  
        $collection = Mage::getModel('test/user')->getCollection()->distinct()
            ->addFieldToSelect('name')
            ->addFieldToSelect('id','userId');
        $collection->getSelect()->join( array('uc'=> user_profile), 'uc.user_id = main_table.id', array('uc.id'));

magento admin system configuration 404

magento admin system configuration 404

magento admin configuration 404 error

There may be few reasons which throws the above error .

1. Cache Issue-
-Flush Magento Cache.

-Logout .
-Login Again.

2 . You Have not Defined the acl in config.php file. Make Sure You have added the field in acl.
example :

 
   
    
       
           
                
                   
                       
                         
                              
                              100
                         
                       
                   
                
          
      
    
 

Your Fields should be defined in the acl children tag as :


      
         100

Define the values and again clear cache, logout and login .
It Will Work As expected.

Fatal error: Call to a member function setSaveParametersInSession() on a non-object in \mage\app\code\core\Mage\Adminhtml\Block\Widget\Grid\Container.php on line 66

Fatal error: Call to a member function setSaveParametersInSession() on a non-object in \mage\app\code\core\Mage\Adminhtml\Block\Widget\Grid\Container.php on line 66

This error occurs due to invalid grid path in the block constructor

example :

public function __construct(){
$this->_controller = ‘adminhtml_advertisement’; /**this sould be the path of the admin html files*/
$this->_blockGroup = ‘test_advertisement’; /** this should be proper path**/
parent::__construct();
$this->_headerText = Mage::helper(‘test_advertisement’)->__(‘Advretisement’);
$this->_updateButton(‘add’, ‘label’, Mage::helper(‘test_advertisement’)->__(‘Add Advretisement’));

}

Please dont be confused about $this->_controller this is not controller name this is the path of the grid which will generate Adminhtml_Advertisement_Grid

$this->_blockGroup is block namespce which you specify in config.xml file.
Note : : If the block groups and controller points the correct path to the grid (Grid.php) file there will occur no error so make sure the both of them produces the correct class name of the Grid.php

$this->_controller = ‘adminhtml_advertisement’;
$this->_blockGroup = ‘test_advertisement’;
should produce the following class name :

Test_Advertisement_Block_Adminhtml_Advertisement_Grid

example :

       
            
                Test_Advertisement_Block
            
        

Magento get current date time

Magento get current date time

You Can get Current date and time (according to timezone) in magento as following


Mage::getModel('core/date')->date('Y-m-d H:i:s');

Or You Can use following method

date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));