Monday 18 August 2014

Magento - Module INSERT,UPDATE, DELETE, SELECT code

Suppose, I have a module named mynews. Here follows the code to select, insert, update, and delete data from the news table.
INSERT DATA
$data contains array of data to be inserted. The key of the array should be the database table’s field name and the value should be the value to be inserted.
$data = array('title'=>'hello there','content'=>'how are you? i am fine over here.','status'=>1);
$model = Mage::getModel('mynews/mynews')->setData($data);
try {
    $insertId = $model->save()->getId();
    echo "Data successfully inserted. Insert ID: ".$insertId;
} catch (Exception $e){
 echo $e->getMessage();   
}
SELECT DATA
$item->getData() prints array of data from news table.
$item->getTitle() prints the only the title field.
Similarly, to print content, we need to write $item->getContent().
$model = Mage::getModel('mynews/mynews');
$collection = $model->getCollection();
foreach($collection as $item){
print_r($item->getData());
print_r($item->getTitle());
}
UPDATE DATA
$id is the database table row id to be updated. $data contains array of data to be updated. The key of the array should be the database table’s field name and the value should be the value to be updated.
// $id = $this->getRequest()->getParam('id');
$id = 2;
$data = array('title'=>'hello test','content'=>'test how are you?','status'=>0);
$model = Mage::getModel('mynews/mynews')->load($id)->addData($data);
try {
    $model->setId($id)->save();
    echo "Data updated successfully.";

} catch (Exception $e){
    echo $e->getMessage(); 
}
DELETE DATA
$id is the database table row id to be deleted.
// $id = $this->getRequest()->getParam('id');
$id = 3;
$model = Mage::getModel('mynews/mynews');
try {
    $model->setId($id)->delete();
    echo "Data deleted successfully.";

} catch (Exception $e){
    echo $e->getMessage(); 
}
In this way you can perform select, insert, update and delete in your custom module and in any magento code.

Wednesday 2 July 2014

How to load all the products under one category in magento?.


If we want to load all the products under one category we need to use below code is the simplest way,

 $cat_id = 5;
 $collection = Mage::getModel("catalog/product")->getCollection();
 $_category =  Mage::getModel("catalog/category")->load($cat_id);    
 $collection->addCategoryFilter($_category);

Monday 30 June 2014

How to get address collection of all customers in magento?

- To get all the address collection created by various customers in magento achieved by the following simple code,

$addressesCollection = Mage::getResourceModel('customer/address_collection');
$addressesCollection->addAttributeToSelect('*');
/* for particular address 
$addressesCollection->addFieldToFilter('id','12'); */

foreach ($addressesCollection as $address) {
echo "<pre>";
print_r($address->getData());
}

Thursday 5 June 2014

Jquery $.ajax method

-Here is the sample code for $.ajax calling method in php

var id = $('#someinput_id').val();
$.ajax({
 url: admin_url,
 type: 'POST', 
 dataType: 'html', 
 data: { 
id: id,
name: 'Ravichandran',
date: "<?php echo date('d-m-Y', strtotime(now())) ?>", 

 },
 success:function( data ){  
 $('#some_id').html(data); 
 }
 });

Wednesday 4 June 2014

How to use select, update, delete and insert custom queries in magento

- We can Perform select, update, delete and insert queries through php file by following steps.


require_once '../../app/Mage.php';
Mage::app('default');


        Select query to get the value form table           

$connection = Mage::getSingleton('core/resource')
->getConnection('core_read');
$select = $connection->select()
->from('tablename', array('*')) // select * from tablename or use array('id','name') selected values
->where('id=?',1)               // where id =1
->group('name');         // group by name
$rowsArray = $connection->fetchAll($select); // return all rows
$rowArray =$connection->fetchRow($select);   //return row


             Insert query                  

$connection = Mage::getSingleton('core/resource')
->getConnection('core_write');
$connection->beginTransaction();
$fields = array();
$fields['name']= 'test';
$fields['age']='25';
$connection->insert('tablename', $fields);

$connection->commit();

            update query                 

$connection = Mage::getSingleton('core/resource')
->getConnection('core_write');
$connection->beginTransaction();
$fields = array();
$fields['name'] = 'jony';

//for single parameter
$where = $connection->quoteInto('id =?', '1');

/* For mulitple parameters
$where = $connection->quoteInto('id =?', '1')
                 .$connection->quoteInto('name =?', 'Mathi') ;
*/

$connection->update('tablename', $fields, $where);

$connection->commit();


                delete query                    

$condition = array($connection->quoteInto('id=?', '1'));

$connection->delete('tablename', $condition);




Thursday 29 May 2014

Magento - Remove or rename add new save continue delete button from magento admin


If you don’t want to show the ‘Add New’ button in the Grid. The Add New button is present in top right corner of Grid Page.

Rename ‘Add New’ button

Here are the steps to rename the ‘Add New’ text to anything you required (for example, ‘Add Report’):-

- Go to YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.php
- Add the following code in the constructor of this file:-

$this->_addButtonLabel = Mage::helper('yourmodulename')->__('Add Report');

Remove ‘Add New’ button

Here are the steps to remove the ‘Add New’ button:-

- Go to YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.php
- Add the following code in the constructor of this file (it should be just below the call to parent constructor):-

parent::__construct();
$this->_removeButton('add');


In edit.php
parent::__construct();
$this->_removeButton('delete');// Removes the delete button from edit page
$this->_removeButton('save');
$this->_removeButton('back');

in grid.php
parent::__construct();
$this->_removeButton('add');