Saturday, June 13, 2009

Keep the track, who has attained the order in last?

1.You need to do following steps to add Updated By Field in Order Section.
Go to the database :
Our Main table for order is sales_order , and for Order we are having entity_type_id 11 in our database table eav_attribute.
So, I have added one attribute 'updated_by' in eav_attribute table with entity_type_id 11.
Now, go to the table sales_order and add one column 'updated_by' (type is varchar, length can be any)

Go to file app/code/core/Mage/Sales/Model/Order.php
In this file , all operations for Order can be done.
$this->setData('field_name','field_value'); , this function has been called when you want to update or add any record.
So, to set any value to any field, you can use setData() function.
Here , I have created one function setUpdatedAdmin() to update 'updated_by' field.
Function is like ,
public function setUpdatedAdmin()
{
$user = Mage::getSingleton('admin/session')->getUser();
$this->setData('updated_by', $user->getUsername());
return $this;
}

Mage::getSingleton('admin/session')->getUser(), get all details of current logged in user in admin panel. $user->getUsername() retrieve current logged in user's name, this is magento's default function. By using setData() (magento's default), we have set the value for updated_by field.
Now, the question is where to call this function ? , so on every operation for Order , _beforeSave() function of app/code/core/Mage/Sales/Model/Order.php has been called every time.
So, we can use setUpdatedAdmin() in _beforeSave() function.

protected function _beforeSave()
{
parent::_beforeSave();
$this->_checkState();
if (!$this->getId())
{
$store = $this->getStore();
$name = array($store->getWebsite()->getName(),$store->getGroup()->getName(),$store->getName());
$this->setStoreName(implode("\n", $name));
}
$this->setUpdatedAdmin();
return $this;
}

After some R & D, I have found getUser() for current user, & setData() for set field value, other logic is mine :).

Friday, June 5, 2009

Magento Debugging - It's like an adventure sometime

I am working on magento since last 6 months.
I really enjoyed whatever i have learned and done in magento for some sort of customization.
I was just thinking that, many developers are there who are facing similar problems for customization and changes in magento.

So, Here I am going to share every experience in magento that can help you and me both.