Category Archives: Magento

Magento upload multiple image files


Magento upload multiple image files : You can upload multiple images in magento as below.

Magento upload multiple image files

Suppose you have following image file upload option in your magento form.

 Image 1
 Image 2
 Image 3
 Image 4
 Image 5

Now add the following code in your form Action ie. In your Controller method :

function uploadAdvertisementImage(){
foreach($_FILES['images']['name']  as $key =>$image){
		if (!empty($image)) {
			try {
				//$uploader = new Varien_File_Uploader('image_url');
                                $uploader = new Varien_File_Uploader(array(
                                    'name' => $_FILES['image_url']['name'][$key],
                                    'type' => $_FILES['image_url']['type'][$key],
                                    'tmp_name' => $_FILES['image_url']['tmp_name'][$key],
                                    'error' => $_FILES['image_url']['error'][$key],
                                    'size' => $_FILES['image_url']['size'][$key]
                                     ));

				$uploader->setAllowedExtensions(array('jpg', 'jpeg', 'png'));
				$uploader->setAllowRenameFiles(false);
				$uploader->setFilesDispersion(false);
				$path = Mage::getBaseDir('media') . DS . 'advertisement';
				$fileName = time()."_".substr( base_convert( time(), 10, 30 ).md5( microtime() ), 0, 10 ).'.'.$uploader->getFileExtension();
				
				$uploader->save($path, $fileName);                              
                                $rand = rand(0,20000);
                                $pathUrl = Mage::getUrl('media/images/');
                                $fileNames[$key] = trim($fileName);
			} catch (Exception $e) { 
                         Mage::log('Error in upload');                        
                        
                         
                        }
		}
            } 
}

Magento get current category name


Magento get current category name: You can get the category id and category name on product detail page as below


Magento get current category name Example

Use the following code to get current category detail.


getCurrentCategory()->getId(); 
  $catName = Mage::getModel('catalog/layer')->getCurrentCategory()->getName(); 

?>

Which will give the category name and id.

Magento count cart items


Magento count cart items : You Can count carts item from the current quote.


Magento count cart items Syntax


 $count = Mage::helper('checkout/cart')->getCart()->getItemsCount();

Which will the total no of items in the cart.

If Want to process all items

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

Magento left join collection Example


Magento left join collection Example : Magento left join collection query is explained below.


Magento left join collection Example

Following syntax is used for left joining two tables in magento :

//load collection 
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->joinLeft(Mage::getConfig()->getTablePrefix().'table_to_be_joined','main_table.table_field ='.Mage::getConfig()->getTablePrefix().'table_to_be_joined.table_field',array('field_name'));

Magento left join two custom tables Example

$collection = Mage::getModel('sales/order')->getCollection();
$collection->getSelect()->joinLeft( array('order_item'=> 'sales_flat_order_item'), 'order_item.order_id = main_table.entity_id', array('order_item.sku'));

Magento 2 Left Join Example

Magento join select multiple columns


Magento join select multiple columns : You can select multiple columns as below.


Example of magento join select multiple columns

You can select multiple columns in magento joins as below:

//load collection 
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join('table2'=>'table2_to_be_joined','main_table.table_field ='table2.table2_field',array('table2.field_name1','table2.field_name2','main_table.field_name1','main_table.field_name2','main_table.field_name3'));

You can use table alias to select columns as above.

Magento join multiple tables


Magento join mulitple tables : You can join multiple tables in magento as below.


Magento join multiple tables Syntax

Following syntax is used for joining multiple tables in magento :

//load collection 
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table2_to_be_joined','main_table.table_field ='.Mage::getConfig()->getTablePrefix().'table2_to_be_joined.table2_field',array('field_name'));
//join with 3rd table
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table3_to_be_joined','main_table.table_field ='.Mage::getConfig()->getTablePrefix().'table3_to_be_joined.table3_field',array('field_name'));

Magento join two custom tables


Magento join two custom tables : You can join two custom tables in magento as below.


Magento join two custom tables Syntax

Following syntax is used for joining two tables in magento :

//load collection 
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_to_be_joined','main_table.table_field ='.Mage::getConfig()->getTablePrefix().'table_to_be_joined.table_field',array('field_name'));

Magento join two custom tables Example

$collection = Mage::getModel('sales/order')->getCollection();
$collection->getSelect()->join( array('order_item'=> 'sales_flat_order_item'), 'order_item.order_id = main_table.entity_id', array('order_item.sku'));

Magento join query in collection


Magento join query in collection : A simple join in magento is given as below:


Magento join query in collection Syntax

$collection = Mage::getModel('Module_name/Model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=> 'table_name'), 'condition', array('columns_name1'));

Magento join query in collection Example

$collection = Mage::getModel('sales/order')->getCollection();
$collection->getSelect()->join( array('order_item'=> 'sales_flat_order_item'), 'order_item.order_id = main_table.entity_id', array('order_item.sku'));

Magento 2 Join Collection Example

Magento call observer after customer login


Magento call observer after customer login : You Can call observer after customer login as below


Magento call observer after customer login

Add the following lines of code in your config.xml with details of your module.

 

            
              
              singleton
              Namespace_ModuleName_Model_Observer
              Name_of_method