Adding Custom Field to Magento

Sometimes you require adding an additional field in the check-out process. It could be for instance a tracking code for a Business-to-Business application, or a special reduction code.

Here is a step-by-step procedure how you can achieve just that, add a custom field to Magento check-out and display it in the user profile and on the invoices.

1. First, install a Magento module from Magento Connect named “customer-order-comment”

This component allows to add the custom field to the check-out page.

http://www.magentocommerce.com/magento-connect/TanRambun/extension/1036/customer-order-comment

2. To make it work in 1.4.1.0 or above, you need to add a column to the sales_flat_order table. To do so, you can execute the following SQL command:

ALTER TABLE `sales_flat_order` ADD `biebersdorf_customerordercomment` TEXT NOT NULL

3. After doing this, sometimes you have to clear the Magento cache to get the orders to start storing comments.

4. In the folder /app/code/community/Biebersdorf/CustomerOrderComment/design/ you will find the template files as examples for the latest version of the magento default theme. Copy them to the right places of your design theme folder or add the needed changes to your existing template files.

5. You have to add a HTML textarea field in your copies of the files app/design/frontend/default/default/template/checkout/onepage/agreements.phtml and app/design/frontend/default/default/template/checkout/multishipping/agreements.phtml. For Onepage Checkout place the new textarea inside the form (opening and closing form tags) to be sure the comment variable is submitted.

Don’t forget to “enable Terms and Conditions” in configuration for the templates to get shown. You can enable it here: System > Configuration > Sales > Checkout > Checkout Options > Enable Terms and Conditions. To display the saved order comments you have to customize the templates where you like the comments to be shown

6. For changing your admin templates without modifying the original files, you will have to setup a new admin theme (highly recommended).

———————-till now it’s only visible in the orders but not in the invoice————————

7. Then to make this comment appear in the invoice too, copy the following code in

app\design\adminhtml\default\your_theme\template\sales\order\invoice\create\form.phtml

<div>

<div>

<h4><?php echo Mage::helper(’sales’)->__(‘Ref Commercial’) ?></h4>

</div>

<fieldset>

<div><?php echo nl2br($this->helper(‘biebersdorfcustomerordercomment’)->htmlEscape($_order->getBiebersdorfCustomerordercomment())) ?></div>

</fieldset>

</div>

————now it will appear in the invoice generated by magento from the backend——–

8. Now to make it appear in the PDF printout of the invoice –

You will have to override app\code\core\Mage\Sales\Model\Order\Pdf\Invoice.php and in the function make these changes:

Public function insertR($page, $order)

{

$page->drawText($order->getBiebersdorfCustomerordercomment(), 33, $this->y – 33, ‘UTF-8′);

$this->y -= 50;

}

And in the function

public function getPdf($invoices = array())

{

//already written code

//call the comment field and draw it in the Pdf invoice according //to Zend Pdf design like the following

$page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));

$page->drawText(Mage::helper(‘biebersdorfcustomerordercomment’)->__(‘Ref Commercial’), 35, $this->y, ‘UTF-8′);

$page->setFillColor(new Zend_Pdf_Color_GrayScale(0));

$this->insertR($page, $order);

//already written code

}

9. And finally you have to override app\code\core\Mage\Adminhtml\Block\Page.php in order to implement new template files for the adminhtml.

Write this line in the function

__construct()

Mage::getDesign()->setTheme(‘mytheme’);

And you are done.

Related Posts:

Tags: ,

Leave a Reply

CommentLuv Enabled

Notify me of followup comments via e-mail. You can also subscribe without commenting.