Home > Magento > Adding Customer Comments on Invoice PDFs in Magento (using OneStepCheckout)

Adding Customer Comments on Invoice PDFs in Magento (using OneStepCheckout)

January 18th, 2010

I’ve recently installed OneStepCheckout (http://www.onestepcheckout.com/) on a couple of Magento installations. The extension is very nice, really simple to integrate and I expect to see better conversion rates on the checkout process.

One cool thing is that it comes with the option of activating Customer Order Comments – it adds a textarea field on the checkout page, and a box with the customer comments in the admin, when viewing the order.

However, one of my clients requested I added these comments in the invoice PDF’s. So, here’s how to do it:

Step 1
Copy app/code/core/Mage/Sales/Model/Order/Pdf/Invoice.php to app/code/local/Mage/Sales/Model/Order/Pdf/Invoice.php

Step 2
Open the new file and create a new method:

	function insertOscComments(&$page, $order) {
		if( !$order->getOnestepcheckoutCustomercomment() ) { return; }
		$this->y -= 20;
		$page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
		$page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
		$page->setLineWidth(0.5);
		$page->drawRectangle(25, $this->y, 570, $this->y - 20);
		$page->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 1));
		$page->drawRectangle(25, $this->y - 20, 570, $this->y - 40);

		$page->setFillColor(new Zend_Pdf_Color_RGB(0.1, 0.1, 0.1));
		$page->drawText(Mage::helper('onestepcheckout')->__('Customer Comments'), 35, $this->y - 13, 'UTF-8');
		$page->drawText($order->getOnestepcheckoutCustomercomment(), 33, $this->y - 33, 'UTF-8');
		$this->y -= 50;
	}

Step 3
At the end of method getPdf add a call to the new method you created:

  /* Add totals */
  $this->insertTotals($page, $invoice);

  /* Add OneStepCheckout Customer Comments */
  $this->insertOscComments($page, $order);
}

And that’s all you need.

Share or bookmark this page:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Wikio
  • Reddit
  • Twitter
  • Identi.ca
  • Diigo
  • DZone

Cristi Magento ,

  1. Costa
    June 28th, 2010 at 08:18 | #1

    Hello,

    Thanks for posting this.
    I found one problem, when the comment is long , say 2-3 sentences, then it does not display correctly, basically it does not wrap around, and the comment gets cut off after one line, how do i fix this? thanks

  2. Cristi
    June 28th, 2010 at 16:49 | #2

    The basic idea is to see how many characters the text has. So, you would first strip unwanted stuff (html, line breaks) and then do a quick check for the lenght of your text – there’s a method for estimating the lenght of a string, widthForStringUsingFontSize (defined in Mage_Sales_Model_Order_Pdf_Abstract). I think you could use this to see how long the string is and split it on several lines – don’t forget to also update the y coordinate ($this->y).
    There are, of course, other ways of fixing – for example, decreasing the font size or decreasing the font size and splitting it on multiple lines, changing the line height and so on. Hope this helps.

  3. July 9th, 2010 at 11:40 | #3

    Hi Cristi,

    Thank you for the post, but I wonder, how would you add the comments to the “Print Order” instead being specific an “Invoice”?

    Thank you.

  4. Cristi
    July 9th, 2010 at 13:37 | #4

    I can’t seen to find the print button/action, but I assume that it should be the same thing as with the invoice. As far as I remember, at some point one was able to print an order to pdf or html. For both of them those customer comments can be added. In html is simpler and it’s almost identical to editing sales/order/view/tab/info.phtml. For the pdf, it shouldn’t be harder than adding the code from the invoice pdf. This is just the top of my head, so I might be wrong :)

  5. July 17th, 2010 at 10:16 | #5

    It’s not there by default, but I think it’s a failure from Magento… There are extensions that add it to the Order page, like:

    http://cl.ly/1gGT

    and in my Abstract.php page I did this:

    http://pastebin.com/JHsjRaEs

    It’s easy to change the 3 lines into infinite lines, just by refactoring the code, when the client pays for it, I will do it and place in the same link the correct code :)

    In the spirit of sharing open source, I hope this helps someone.

    Once again Chris, Thank you for the first push as I had no idea where to start :)

  1. No trackbacks yet.