Magento Code Snippets

Add field to Magento Order

 

How to add extra field to Magento "order" And "quote" Entity?

Since Magento follows EAV (Entity-Attribute-Value) model of tables structure, most of the Magento Entities can be modified with extra attributes. Here is a snippet of PHP code which adds one extra attribute to "quote" and "order" Entity.

The name of the attribute is "party_name".

<?php

//add_party_name.php

require_once('app/Mage.php');

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));



$installer = new Mage_Sales_Model_Mysql4_Setup;

$attribute  = array(

   'type'          => 'varchar',

   'backend_type'  => 'text',

   'frontend_input' => 'text',

   'is_user_defined' => true,

   'label'         => 'Party Name',

   'visible'       => true,

   'required'      => false,

   'user_defined'  => true,

   'searchable'    => true,

   'filterable'    => true,

   'comparable'    => true,

   'default'       => ""

);

$installer->addAttribute('quote', 'party_name', $attribute);

$installer->addAttribute('order', 'party_name', $attribute);

$installer->run("ALTER TABLE fydw_sales_flat_order_grid ADD COLUMN party_name VARCHAR(255) NOT NULL;");

$installer->endSetup();

echo "Done.";

exit;

?>


Execute the above script "add_party_name.php" from the root folder of Magento installation.
Example:  http://www.your-magento-store.com/add_party_name.php

Following code snippet can be used to populate the attribute "party_name":

<?php

//set_party_name.php

error_reporting(E_ALL | E_STRICT);

ini_set('display_errors', 1);

require_once 'app/Mage.php';

umask(0);

Mage::app('default');



$order_increment_id = "";

if isset($_GET['order_increment_id']) { $order_increment_id = $_GET['order_increment_id']; }  



$storeId = 0;

//for default store, change as per your store, you can get it from Admin section while creating store



$transaction = Mage::getModel('core/resource_transaction');



$order = Mage::getModel('sales/order')

->setIncrementId($order_increment_id)

->setStoreId($storeId)

->setQuoteId(0)

->setGlobal_currency_code('INR')

->setBase_currency_code('INR')

->setStore_currency_code('INR')

->setOrder_currency_code('INR');

//Set your store currency USD or any other



$order->setParty_name($party_name);



$transaction->addObject($order);

$transaction->addCommitCallback(array($order, 'place'));

$transaction->addCommitCallback(array($order, 'save'));

$transaction->save();

exit;

?>


Example: 

http://www.your-magento-store.com/set_party_name.php?order_increment_id=XXXXXXXX 
where XXXXXXXX = your order increment id

If you want to remove the above attribute, you can execute following snippet of PHP code.

<?php

//remove_party_name.php

require_once('app/Mage.php');

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));



$installer = new Mage_Sales_Model_Mysql4_Setup;

// Remove party_name attribute

$installer->removeAttribute('order', 'party_name');

$installer->endSetup();

exit;

?>

0 comments:

Post a Comment